save things in MySQL

This commit is contained in:
2026-08-25 06:20:39 -07:00
parent 891f089d31
commit fdc121b6eb
4 changed files with 86 additions and 29 deletions
+14 -14
View File
@@ -5,7 +5,6 @@ import (
"database/sql"
"encoding/base64"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/go-webauthn/webauthn/webauthn"
)
@@ -17,6 +16,7 @@ type InMem struct {
sessions map[string]webauthn.SessionData
log Logger
db* sql.DB
}
func (i *InMem) GenSessionID() (string, error) {
@@ -30,11 +30,12 @@ func (i *InMem) GenSessionID() (string, error) {
}
func NewInMem(log Logger) *InMem {
func NewInMem(log Logger, db *sql.DB) *InMem {
return &InMem{
users: make(map[string]PasskeyUser),
sessions: make(map[string]webauthn.SessionData),
log: log,
db: db,
}
}
@@ -70,21 +71,20 @@ func (i *InMem) GetOrCreateUser(userName string) PasskeyUser {
}
func (i *InMem) SaveUser(user PasskeyUser) {
// Store user in MySQL database
db, err := sql.Open("mysql", "root:password@/oworganizer")
if err != nil {
panic(err)
}
// See "Important settings" section.
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
username := user.WebAuthnName()
rpid := "oworganizer.com"
kid := base64.StdEncoding.EncodeToString(user.WebAuthnCredentials()[0].Descriptor().CredentialID)
aaguid := base64.StdEncoding.EncodeToString(user.WebAuthnCredentials()[0].Authenticator.AAGUID)
publicKey := base64.StdEncoding.EncodeToString(user.WebAuthnCredentials()[0].PublicKey)
attestationType := user.WebAuthnCredentials()[0].AttestationType
attestationFormat := user.WebAuthnCredentials()[0].AttestationFormat
attestation := user.WebAuthnCredentials()[0].Attestation.ClientDataJSON
insert, err := db.Query("INSERT INTO users (username, key) VALUES(" + user.WebAuthnName(), user.WebAuthnCredentials())
if err !=nil {
_, err = i.db.Exec("INSERT INTO users (username, rpid, kid, aaguid, public_key, attestation_type, attestation_format, attestation) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
username, rpid, kid, aaguid, publicKey, attestationType, attestationFormat, attestation)
if err != nil {
panic(err.Error())
}
defer insert.Close()
fmt.Println("Yay, values added!")
i.log.Printf("[DEBUG] SaveUser: %v", user.WebAuthnName())