diff options
| author | James Elliott <james-d-elliott@users.noreply.github.com> | 2023-11-30 19:45:24 +1100 |
|---|---|---|
| committer | James Elliott <james-d-elliott@users.noreply.github.com> | 2024-03-04 20:29:12 +1100 |
| commit | e4e878f05f8ae1e1784b3ac190459b2d506f796c (patch) | |
| tree | ed8f5b927156300dddff33f3e14bc732803ea405 /internal/storage/sql_provider.go | |
| parent | 61c30b373f8c5ee14321e82c8d7210aae7d260c3 (diff) | |
build(deps): use go.uber.org/mock
Use the new go.uber.org/mock which is currently maintained.
Signed-off-by: James Elliott <james-d-elliott@users.noreply.github.com>
Diffstat (limited to 'internal/storage/sql_provider.go')
| -rw-r--r-- | internal/storage/sql_provider.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/internal/storage/sql_provider.go b/internal/storage/sql_provider.go index 66f8a7795..cb86e137c 100644 --- a/internal/storage/sql_provider.go +++ b/internal/storage/sql_provider.go @@ -6,6 +6,7 @@ import ( "database/sql" "errors" "fmt" + "strconv" "strings" "time" @@ -59,6 +60,9 @@ func NewSQLProvider(config *schema.Configuration, name, driverName, dataSourceNa sqlUpdateTOTPConfigRecordSignIn: fmt.Sprintf(queryFmtUpdateTOTPConfigRecordSignIn, tableTOTPConfigurations), sqlUpdateTOTPConfigRecordSignInByUsername: fmt.Sprintf(queryFmtUpdateTOTPConfigRecordSignInByUsername, tableTOTPConfigurations), + sqlInsertTOTPHistory: fmt.Sprintf(queryFmtInsertTOTPHistory, tableTOTPHistory), + sqlSelectTOTPHistory: fmt.Sprintf(queryFmtSelectTOTPHistory, tableTOTPHistory), + sqlInsertWebAuthnUser: fmt.Sprintf(queryFmtInsertWebAuthnUser, tableWebAuthnUsers), sqlSelectWebAuthnUser: fmt.Sprintf(queryFmtSelectWebAuthnUser, tableWebAuthnUsers), @@ -193,6 +197,10 @@ type SQLProvider struct { sqlUpdateTOTPConfigRecordSignIn string sqlUpdateTOTPConfigRecordSignInByUsername string + // Table: totp_history. + sqlInsertTOTPHistory string + sqlSelectTOTPHistory string + // Table: webauthn_users. sqlInsertWebAuthnUser string sqlSelectWebAuthnUser string @@ -546,6 +554,30 @@ func (p *SQLProvider) LoadTOTPConfiguration(ctx context.Context, username string return config, nil } +// SaveTOTPHistory saves a TOTP history item in the storage provider. +func (p *SQLProvider) SaveTOTPHistory(ctx context.Context, username string, step uint64) (err error) { + signature := p.hmacSignature([]byte(strconv.Itoa(int(step))), []byte(username)) + + if _, err = p.db.ExecContext(ctx, p.sqlInsertTOTPHistory, username, signature); err != nil { + return fmt.Errorf("error inserting TOTP history for user '%s': %w", username, err) + } + + return nil +} + +// ExistsTOTPHistory checks if a TOTP history item exists in the storage provider. +func (p *SQLProvider) ExistsTOTPHistory(ctx context.Context, username string, step uint64, since time.Time) (exists bool, err error) { + var count int + + signature := p.hmacSignature([]byte(strconv.Itoa(int(step))), []byte(username)) + + if err = p.db.SelectContext(ctx, &count, p.sqlSelectTOTPHistory, username, signature, since); err != nil { + return false, fmt.Errorf("error checking if TOTP history exists: %w", err) + } + + return count != 0, nil +} + // LoadTOTPConfigurations load a set of TOTP configurations from the storage provider. func (p *SQLProvider) LoadTOTPConfigurations(ctx context.Context, limit, page int) (configs []model.TOTPConfiguration, err error) { configs = make([]model.TOTPConfiguration, 0, limit) |
