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

feat: add otp secret persister #1613

Merged
merged 1 commit into from
Sep 2, 2024
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
1 change: 1 addition & 0 deletions backend/persistence/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type User struct {
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
Username *Username `has_one:"username" json:"username,omitempty"`
OTPSecret *OTPSecret `has_one:"otp_secret" json:"-"`
PasswordCredential *PasswordCredential `has_one:"password_credentials" json:"-"`
}

Expand Down
56 changes: 56 additions & 0 deletions backend/persistence/otp_secret_persister.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package persistence

import (
"fmt"
"github.com/gobuffalo/pop/v6"
"github.com/teamhanko/hanko/backend/persistence/models"
)

type OTPSecretPersister interface {
Create(models.OTPSecret) error
Update(*models.OTPSecret) error
Delete(*models.OTPSecret) error
}

type otpSecretPersister struct {
db *pop.Connection
}

func NewOTPSecretPersister(db *pop.Connection) OTPSecretPersister {
return &otpSecretPersister{db: db}
}

func (p *otpSecretPersister) Create(secret models.OTPSecret) error {
vErr, err := p.db.ValidateAndCreate(&secret)
if err != nil {
return fmt.Errorf("failed to store otp secret credential: %w", err)
}

if vErr != nil && vErr.HasAny() {
return fmt.Errorf("otp secret object validation failed: %w", vErr)
}

return nil
}

func (p *otpSecretPersister) Update(secret *models.OTPSecret) error {
vErr, err := p.db.ValidateAndUpdate(secret)
if err != nil {
return fmt.Errorf("failed to update otp secret: %w", err)
}

if vErr != nil && vErr.HasAny() {
return fmt.Errorf("otp secret object validation failed: %w", vErr)
}

return nil
}

func (p *otpSecretPersister) Delete(secret *models.OTPSecret) error {
err := p.db.Destroy(secret)
if err != nil {
return fmt.Errorf("failed to delete otp secret: %w", err)
}

return nil
}
8 changes: 8 additions & 0 deletions backend/persistence/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ func (p *persister) GetUsernamePersisterWithConnection(tx *pop.Connection) Usern
return NewUsernamePersister(tx)
}

func (p *persister) GetOTPSecretPersister() OTPSecretPersister {
return NewOTPSecretPersister(p.DB)
}

func (p *persister) GetOTPSecretPersisterWithConnection(tx *pop.Connection) OTPSecretPersister {
return NewOTPSecretPersister(tx)
}

func (p *persister) GetWebauthnCredentialPersister() WebauthnCredentialPersister {
return NewWebauthnCredentialPersister(p.DB)
}
Expand Down
4 changes: 3 additions & 1 deletion backend/persistence/user_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func (p *userPersister) Get(id uuid.UUID) (*models.User, error) {
"WebauthnCredentials",
"WebauthnCredentials.Transports",
"Username",
"PasswordCredential"}
"PasswordCredential",
"OTPSecret",
}

err := p.db.EagerPreload(eagerPreloadFields...).Find(&user, id)
if err != nil && errors.Is(err, sql.ErrNoRows) {
Expand Down