Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(registration): update webauthnUser object #11

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
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 @@ -18,6 +18,7 @@ import (
"github.com/teamhanko/passkey-server/persistence/persisters"
"net/http"
"strings"
"time"
)

type registrationHandler struct {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
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 string, 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 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
}