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
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
// 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
|
|
}
|