Merge pull request #1930 from seefs001/feature/passkey

fix: passkey model type
This commit is contained in:
Seefs
2025-09-30 12:52:32 +08:00
committed by GitHub

View File

@@ -1,6 +1,7 @@
package model package model
import ( import (
"encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -21,10 +22,10 @@ var (
type PasskeyCredential struct { type PasskeyCredential struct {
ID int `json:"id" gorm:"primaryKey"` ID int `json:"id" gorm:"primaryKey"`
UserID int `json:"user_id" gorm:"uniqueIndex;not null"` UserID int `json:"user_id" gorm:"uniqueIndex;not null"`
CredentialID []byte `json:"credential_id" gorm:"type:blob;uniqueIndex;not null"` CredentialID string `json:"credential_id" gorm:"type:varchar(512);uniqueIndex;not null"` // base64 encoded
PublicKey []byte `json:"public_key" gorm:"type:blob;not null"` PublicKey string `json:"public_key" gorm:"type:text;not null"` // base64 encoded
AttestationType string `json:"attestation_type" gorm:"type:varchar(255)"` AttestationType string `json:"attestation_type" gorm:"type:varchar(255)"`
AAGUID []byte `json:"aaguid" gorm:"type:blob"` AAGUID string `json:"aaguid" gorm:"type:varchar(512)"` // base64 encoded
SignCount uint32 `json:"sign_count" gorm:"default:0"` SignCount uint32 `json:"sign_count" gorm:"default:0"`
CloneWarning bool `json:"clone_warning"` CloneWarning bool `json:"clone_warning"`
UserPresent bool `json:"user_present"` UserPresent bool `json:"user_present"`
@@ -78,14 +79,18 @@ func (p *PasskeyCredential) ToWebAuthnCredential() webauthn.Credential {
BackupState: p.BackupState, BackupState: p.BackupState,
} }
credID, _ := base64.StdEncoding.DecodeString(p.CredentialID)
pubKey, _ := base64.StdEncoding.DecodeString(p.PublicKey)
aaguid, _ := base64.StdEncoding.DecodeString(p.AAGUID)
return webauthn.Credential{ return webauthn.Credential{
ID: p.CredentialID, ID: credID,
PublicKey: p.PublicKey, PublicKey: pubKey,
AttestationType: p.AttestationType, AttestationType: p.AttestationType,
Transport: p.TransportList(), Transport: p.TransportList(),
Flags: flags, Flags: flags,
Authenticator: webauthn.Authenticator{ Authenticator: webauthn.Authenticator{
AAGUID: p.AAGUID, AAGUID: aaguid,
SignCount: p.SignCount, SignCount: p.SignCount,
CloneWarning: p.CloneWarning, CloneWarning: p.CloneWarning,
Attachment: protocol.AuthenticatorAttachment(p.Attachment), Attachment: protocol.AuthenticatorAttachment(p.Attachment),
@@ -99,10 +104,10 @@ func NewPasskeyCredentialFromWebAuthn(userID int, credential *webauthn.Credentia
} }
passkey := &PasskeyCredential{ passkey := &PasskeyCredential{
UserID: userID, UserID: userID,
CredentialID: credential.ID, CredentialID: base64.StdEncoding.EncodeToString(credential.ID),
PublicKey: credential.PublicKey, PublicKey: base64.StdEncoding.EncodeToString(credential.PublicKey),
AttestationType: credential.AttestationType, AttestationType: credential.AttestationType,
AAGUID: credential.Authenticator.AAGUID, AAGUID: base64.StdEncoding.EncodeToString(credential.Authenticator.AAGUID),
SignCount: credential.Authenticator.SignCount, SignCount: credential.Authenticator.SignCount,
CloneWarning: credential.Authenticator.CloneWarning, CloneWarning: credential.Authenticator.CloneWarning,
UserPresent: credential.Flags.UserPresent, UserPresent: credential.Flags.UserPresent,
@@ -119,10 +124,10 @@ func (p *PasskeyCredential) ApplyValidatedCredential(credential *webauthn.Creden
if credential == nil || p == nil { if credential == nil || p == nil {
return return
} }
p.CredentialID = credential.ID p.CredentialID = base64.StdEncoding.EncodeToString(credential.ID)
p.PublicKey = credential.PublicKey p.PublicKey = base64.StdEncoding.EncodeToString(credential.PublicKey)
p.AttestationType = credential.AttestationType p.AttestationType = credential.AttestationType
p.AAGUID = credential.Authenticator.AAGUID p.AAGUID = base64.StdEncoding.EncodeToString(credential.Authenticator.AAGUID)
p.SignCount = credential.Authenticator.SignCount p.SignCount = credential.Authenticator.SignCount
p.CloneWarning = credential.Authenticator.CloneWarning p.CloneWarning = credential.Authenticator.CloneWarning
p.UserPresent = credential.Flags.UserPresent p.UserPresent = credential.Flags.UserPresent
@@ -157,8 +162,9 @@ func GetPasskeyByCredentialID(credentialID []byte) (*PasskeyCredential, error) {
return nil, ErrFriendlyPasskeyNotFound return nil, ErrFriendlyPasskeyNotFound
} }
credIDStr := base64.StdEncoding.EncodeToString(credentialID)
var credential PasskeyCredential var credential PasskeyCredential
if err := DB.Where("credential_id = ?", credentialID).First(&credential).Error; err != nil { if err := DB.Where("credential_id = ?", credIDStr).First(&credential).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
common.SysLog(fmt.Sprintf("GetPasskeyByCredentialID: passkey not found for credential ID length %d", len(credentialID))) common.SysLog(fmt.Sprintf("GetPasskeyByCredentialID: passkey not found for credential ID length %d", len(credentialID)))
return nil, ErrFriendlyPasskeyNotFound return nil, ErrFriendlyPasskeyNotFound