Skip to content

Commit

Permalink
fix(registration): update webauthnUser object
Browse files Browse the repository at this point in the history
* updates the webauthn user object on registration
* updates username, display name and icon

Related to: #10
  • Loading branch information
Stefan Jacobi committed Nov 9, 2023
1 parent 416ca39 commit f139bcc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions server/api/dto/request/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type UpdateCredentialsDto struct {

type InitRegistrationDto struct {
UserId string `json:"user_id" validate:"required,uuid4"`
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"`
}
19 changes: 18 additions & 1 deletion server/api/handler/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/teamhanko/passkey-server/persistence/persisters"
"net/http"
"strings"
"time"
)

type registrationHandler struct {
Expand Down Expand Up @@ -57,7 +58,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
Expand All @@ -71,6 +72,8 @@ func (r *registrationHandler) Init(ctx echo.Context) error {
}

internalUserDto = intern.NewWebauthnUser(*webauthnUser)
} else {
internalUserDto, err = r.updateWebauthnUser(userModel, webauthnUser, webauthnUserPersister)
}

t := true
Expand Down Expand Up @@ -205,3 +208,17 @@ func (r *registrationHandler) GetWebauthnUser(userId uuid.UUID, tenantId uuid.UU

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
}
17 changes: 14 additions & 3 deletions server/persistence/persisters/webauthn_user_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type WebauthnUserPersister interface {
Get(id uuid.UUID) (*models.WebauthnUser, error)
GetByUserId(userId uuid.UUID, tenantId uuid.UUID) (*models.WebauthnUser, error)
Delete(webauthnUser *models.WebauthnUser) error
Update(webauthnUser *models.WebauthnUser) error
}

type webauthnUserPersister struct {
Expand All @@ -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)
}

Expand Down Expand Up @@ -76,3 +74,16 @@ func (p *webauthnUserPersister) GetByUserId(userId uuid.UUID, 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
}

0 comments on commit f139bcc

Please sign in to comment.