From 888ecd6f3a6e49eb4f5ac742d9140278e0d1e7d5 Mon Sep 17 00:00:00 2001 From: Stefan Jacobi Date: Thu, 9 Nov 2023 21:23:17 +0100 Subject: [PATCH] fix(registration): update webauthnUser object * updates the webauthn user object on registration * updates username, display name and icon Related to: #10 --- server/api/dto/request/requests.go | 4 ++-- server/api/handler/registration.go | 19 ++++++++++++++++++- .../persisters/webauthn_user_persister.go | 17 ++++++++++++++--- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/server/api/dto/request/requests.go b/server/api/dto/request/requests.go index 84e5b94..ffb3ea0 100644 --- a/server/api/dto/request/requests.go +++ b/server/api/dto/request/requests.go @@ -23,7 +23,7 @@ type UpdateCredentialsDto struct { type InitRegistrationDto struct { UserId string `json:"user_id" validate:"required"` - Username string `json:"username" validate:"required"` - DisplayName *string `json:"display_name"` + Username string `json:"username" validate:"required,max=128"` + DisplayName *string `json:"display_name,max=128"` Icon *string `json:"icon"` } diff --git a/server/api/handler/registration.go b/server/api/handler/registration.go index 2aa05af..b74a998 100644 --- a/server/api/handler/registration.go +++ b/server/api/handler/registration.go @@ -18,6 +18,7 @@ import ( "github.com/teamhanko/passkey-server/persistence/persisters" "net/http" "strings" + "time" ) type registrationHandler struct { @@ -58,7 +59,7 @@ func (r *registrationHandler) Init(ctx echo.Context) error { webauthnSessionPersister := r.persister.GetWebauthnSessionDataPersister(tx) webauthnUser.Tenant = h.Tenant - internalUserDto, _, err := r.GetWebauthnUser(webauthnUser.UserID, webauthnUser.Tenant.ID, webauthnUserPersister) + internalUserDto, userModel, err := r.GetWebauthnUser(webauthnUser.UserID, webauthnUser.Tenant.ID, webauthnUserPersister) if err != nil { ctx.Logger().Error(err) return err @@ -72,6 +73,8 @@ func (r *registrationHandler) Init(ctx echo.Context) error { } internalUserDto = intern.NewWebauthnUser(*webauthnUser) + } else { + internalUserDto, err = r.updateWebauthnUser(userModel, webauthnUser, webauthnUserPersister) } t := true @@ -206,3 +209,17 @@ func (r *registrationHandler) GetWebauthnUser(userId string, tenantId uuid.UUID, return intern.NewWebauthnUser(*user), user, nil } + +func (r *registrationHandler) updateWebauthnUser(oldUser *models.WebauthnUser, newUser *models.WebauthnUser, persister persisters.WebauthnUserPersister) (*intern.WebauthnUser, error) { + oldUser.Name = newUser.Name + oldUser.DisplayName = newUser.DisplayName + oldUser.Icon = newUser.Icon + oldUser.UpdatedAt = time.Now() + + err := persister.Update(oldUser) + if err != nil { + return nil, err + } + + return intern.NewWebauthnUser(*oldUser), nil +} diff --git a/server/persistence/persisters/webauthn_user_persister.go b/server/persistence/persisters/webauthn_user_persister.go index 38c20bc..d7c9195 100644 --- a/server/persistence/persisters/webauthn_user_persister.go +++ b/server/persistence/persisters/webauthn_user_persister.go @@ -15,6 +15,7 @@ type WebauthnUserPersister interface { Get(id uuid.UUID) (*models.WebauthnUser, error) GetByUserId(userId string, tenantId uuid.UUID) (*models.WebauthnUser, error) Delete(webauthnUser *models.WebauthnUser) error + Update(webauthnUser *models.WebauthnUser) error } type webauthnUserPersister struct { @@ -30,12 +31,9 @@ func NewWebauthnUserPersister(database *pop.Connection) WebauthnUserPersister { func (p *webauthnUserPersister) Create(webauthnUser *models.WebauthnUser) error { vErr, err := p.database.ValidateAndCreate(webauthnUser) if err != nil { - fmt.Printf("%s", err.Error()) return fmt.Errorf("failed to store webauthn user: %w", err) } if vErr != nil && vErr.HasAny() { - fmt.Printf("%s", vErr.Error()) - fmt.Printf("Debug: %v", webauthnUser) return fmt.Errorf("webauthn user object validation failed: %w", vErr) } @@ -76,3 +74,16 @@ func (p *webauthnUserPersister) GetByUserId(userId string, tenantId uuid.UUID) ( return &weauthnUser, nil } + +func (p *webauthnUserPersister) Update(webauthnUser *models.WebauthnUser) error { + vErr, err := p.database.ValidateAndUpdate(webauthnUser) + if err != nil { + return fmt.Errorf("failed to update webauthn user: %w", err) + } + + if vErr != nil && vErr.HasAny() { + return fmt.Errorf("webauthn user object validation failed: %w", vErr) + } + + return nil +}