feat(auth): add single-use totp recovery codes
Codes are stored as SHA-256 hashes and consumed with one conditional update, so concurrent use of the same code succeeds at most once. Refs #40 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
3f21361995
commit
dbea42e7a5
9 files changed
+248
No files matched your search
@@ -0,0 +1,55 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: recovery_codes.sql
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const countUnusedRecoveryCodes = `-- name: CountUnusedRecoveryCodes :one
|
||||
SELECT count(*) FROM recovery_codes WHERE user_id = $1 AND used_at IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) CountUnusedRecoveryCodes(ctx context.Context, userID uuid.UUID) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countUnusedRecoveryCodes, userID)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const deleteRecoveryCodes = `-- name: DeleteRecoveryCodes :exec
|
||||
DELETE FROM recovery_codes WHERE user_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteRecoveryCodes(ctx context.Context, userID uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deleteRecoveryCodes, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
type InsertRecoveryCodesParams struct {
|
||||
UserID uuid.UUID
|
||||
CodeHash []byte
|
||||
}
|
||||
|
||||
const useRecoveryCode = `-- name: UseRecoveryCode :execrows
|
||||
UPDATE recovery_codes SET used_at = now()
|
||||
WHERE user_id = $1 AND code_hash = $2 AND used_at IS NULL
|
||||
`
|
||||
|
||||
type UseRecoveryCodeParams struct {
|
||||
UserID uuid.UUID
|
||||
CodeHash []byte
|
||||
}
|
||||
|
||||
func (q *Queries) UseRecoveryCode(ctx context.Context, arg UseRecoveryCodeParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, useRecoveryCode, arg.UserID, arg.CodeHash)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
Reference in new issue
Block a user