Skip to content

Commit

Permalink
chore: simplify getting the webauthn user
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddyDevelop committed Dec 5, 2024
1 parent 58a0397 commit 8f182ae
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions backend/flow_api/services/webauthn.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,7 @@ func (s *webauthnService) VerifyAssertionResponse(p VerifyAssertionResponseParam
return nil, ErrInvalidWebauthnCredentialMFAOnly
}

var userID uuid.UUID
// Only get the userID when it is a mfa login. For passkeys the userID will be found out in GetWebAuthnUser.
// Otherwise, a custom user handle could not be an uuid.
if p.IsMFA {
userID = sessionDataModel.UserId
}

webAuthnUser, userModel, err := s.GetWebAuthnUser(p.Tx, *credentialModel, userID)
webAuthnUser, userModel, err := s.GetWebAuthnUser(p.Tx, *credentialModel)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -350,27 +343,18 @@ func (s *webauthnService) VerifyAttestationResponse(p VerifyAttestationResponseP
return credential, nil
}

func (s *webauthnService) GetWebAuthnUser(tx *pop.Connection, credential models.WebauthnCredential, userID uuid.UUID) (webauthn.User, *models.User, error) {
var customUserHandle []byte = nil
if userID == uuid.Nil {
userID = credential.UserId
}

if credential.UserHandle != nil {
customUserHandle = []byte(credential.UserHandle.Handle)
}

user, err := s.persister.GetUserPersisterWithConnection(tx).Get(userID)
func (s *webauthnService) GetWebAuthnUser(tx *pop.Connection, credential models.WebauthnCredential) (webauthn.User, *models.User, error) {
user, err := s.persister.GetUserPersisterWithConnection(tx).Get(credential.UserId)
if err != nil {
return nil, nil, fmt.Errorf("failed to fetch user from db: %w", err)
}
if user == nil {
return nil, nil, ErrInvalidWebauthnCredential
}

if customUserHandle != nil {
if credential.UserHandle != nil {
return &webauthnUserWithCustomUserHandle{
CustomUserHandle: customUserHandle,
CustomUserHandle: []byte(credential.UserHandle.Handle),
User: *user,
}, user, nil
}
Expand Down

0 comments on commit 8f182ae

Please sign in to comment.