feat(store): add totp columns, org totp policy and global scope
Orgs are created inside their own tenant scope, so signup needs no system scope. Refs #3 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
caff396264
commit
99e9b1e57d
12 files changed
+203
-31
No files matched your search
@@ -11,9 +11,20 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const anyUserExists = `-- name: AnyUserExists :one
|
||||
SELECT EXISTS (SELECT 1 FROM users)
|
||||
`
|
||||
|
||||
func (q *Queries) AnyUserExists(ctx context.Context) (bool, error) {
|
||||
row := q.db.QueryRow(ctx, anyUserExists)
|
||||
var exists bool
|
||||
err := row.Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
const createUser = `-- name: CreateUser :one
|
||||
INSERT INTO users (email, display_name, password_hash) VALUES ($1, $2, $3)
|
||||
RETURNING id, email, display_name, password_hash, disabled_at, created_at, updated_at
|
||||
RETURNING id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
@@ -33,12 +44,49 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
|
||||
&i.DisabledAt,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.TotpSecret,
|
||||
&i.TotpEnabledAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteUser = `-- name: DeleteUser :execrows
|
||||
DELETE FROM users WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteUser(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteUser, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const disableTOTP = `-- name: DisableTOTP :exec
|
||||
UPDATE users SET totp_secret = NULL, totp_enabled_at = NULL, updated_at = now() WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DisableTOTP(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, disableTOTP, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const enableTOTP = `-- name: EnableTOTP :exec
|
||||
UPDATE users SET totp_secret = $2, totp_enabled_at = now(), updated_at = now() WHERE id = $1
|
||||
`
|
||||
|
||||
type EnableTOTPParams struct {
|
||||
ID uuid.UUID
|
||||
TotpSecret []byte
|
||||
}
|
||||
|
||||
func (q *Queries) EnableTOTP(ctx context.Context, arg EnableTOTPParams) error {
|
||||
_, err := q.db.Exec(ctx, enableTOTP, arg.ID, arg.TotpSecret)
|
||||
return err
|
||||
}
|
||||
|
||||
const getUser = `-- name: GetUser :one
|
||||
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at FROM users WHERE id = $1
|
||||
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at FROM users WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
|
||||
@@ -52,12 +100,14 @@ func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
|
||||
&i.DisabledAt,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.TotpSecret,
|
||||
&i.TotpEnabledAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserByEmail = `-- name: GetUserByEmail :one
|
||||
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at FROM users WHERE email = $1
|
||||
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at FROM users WHERE email = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error) {
|
||||
@@ -71,6 +121,22 @@ func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error
|
||||
&i.DisabledAt,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.TotpSecret,
|
||||
&i.TotpEnabledAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const setPasswordHash = `-- name: SetPasswordHash :exec
|
||||
UPDATE users SET password_hash = $2, updated_at = now() WHERE id = $1
|
||||
`
|
||||
|
||||
type SetPasswordHashParams struct {
|
||||
ID uuid.UUID
|
||||
PasswordHash *string
|
||||
}
|
||||
|
||||
func (q *Queries) SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error {
|
||||
_, err := q.db.Exec(ctx, setPasswordHash, arg.ID, arg.PasswordHash)
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user