feat(sync): paginate Android sync with change tokens
This commit is contained in:
1 parent
0ff94f9b10
commit
97c237be80
76 files changed
+3401
-851
No files matched your search
@@ -87,6 +87,7 @@ func EnsureSchema(ctx context.Context, pool *pgxpool.Pool, options SchemaOptions
|
||||
role TEXT NOT NULL DEFAULT 'user',
|
||||
totp_secret TEXT NOT NULL DEFAULT '',
|
||||
totp_enabled BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
last_used_step BIGINT NOT NULL DEFAULT -1,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
last_login_at TIMESTAMPTZ
|
||||
@@ -95,6 +96,7 @@ func EnsureSchema(ctx context.Context, pool *pgxpool.Pool, options SchemaOptions
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS role TEXT NOT NULL DEFAULT 'user';
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS totp_secret TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS totp_enabled BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS last_used_step BIGINT NOT NULL DEFAULT -1;
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ NOT NULL DEFAULT now();
|
||||
ALTER TABLE auth_users ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT now();
|
||||
-- last_login_at is nullable (NULL means "never logged in" - registration
|
||||
@@ -220,8 +222,9 @@ func ensureUsernameCaseIndex(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
// TOTPState is the private provisioning state for one account. Secret must
|
||||
// never be included in an API response.
|
||||
type TOTPState struct {
|
||||
Secret string
|
||||
Enabled bool
|
||||
Secret string
|
||||
Enabled bool
|
||||
LastUsedStep int64
|
||||
}
|
||||
|
||||
// GetTOTPState reads the stored TOTP provisioning state for an account.
|
||||
@@ -230,7 +233,7 @@ func (s *Store) GetTOTPState(ctx context.Context, userID int64) (TOTPState, erro
|
||||
return TOTPState{}, err
|
||||
}
|
||||
var state TOTPState
|
||||
err := s.pool.QueryRow(ctx, `SELECT COALESCE(totp_secret, ''), COALESCE(totp_enabled, false) FROM auth_users WHERE id = $1`, userID).Scan(&state.Secret, &state.Enabled)
|
||||
err := s.pool.QueryRow(ctx, `SELECT COALESCE(totp_secret, ''), COALESCE(totp_enabled, false), COALESCE(last_used_step, -1) FROM auth_users WHERE id = $1`, userID).Scan(&state.Secret, &state.Enabled, &state.LastUsedStep)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return TOTPState{}, nil
|
||||
}
|
||||
@@ -246,7 +249,7 @@ func (s *Store) SetTOTPSecret(ctx context.Context, userID int64, secret string)
|
||||
if err := s.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
tag, err := s.pool.Exec(ctx, `UPDATE auth_users SET totp_secret = $1, totp_enabled = false, updated_at = now() WHERE id = $2 AND NOT totp_enabled`, secret, userID)
|
||||
tag, err := s.pool.Exec(ctx, `UPDATE auth_users SET totp_secret = $1, totp_enabled = false, last_used_step = -1, updated_at = now() WHERE id = $2 AND NOT totp_enabled`, secret, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set TOTP secret: %w", err)
|
||||
}
|
||||
@@ -262,16 +265,39 @@ func (s *Store) SetTOTPSecret(ctx context.Context, userID int64, secret string)
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnableTOTP marks a previously provisioned secret as active.
|
||||
func (s *Store) EnableTOTP(ctx context.Context, userID int64) error {
|
||||
// EnableTOTP marks a previously provisioned secret as active and records the
|
||||
// step used to confirm it, preventing that code from being reused at login.
|
||||
func (s *Store) EnableTOTP(ctx context.Context, userID int64, secret string, step int64) (bool, error) {
|
||||
if err := s.validate(); err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
_, err := s.pool.Exec(ctx, `UPDATE auth_users SET totp_enabled = true, updated_at = now() WHERE id = $1 AND totp_secret <> ''`, userID)
|
||||
tag, err := s.pool.Exec(ctx, `
|
||||
UPDATE auth_users
|
||||
SET totp_enabled = true, last_used_step = $1, updated_at = now()
|
||||
WHERE id = $2 AND totp_secret = $3 AND NOT totp_enabled AND last_used_step < $1
|
||||
`, step, userID, secret)
|
||||
if err != nil {
|
||||
return fmt.Errorf("enable TOTP: %w", err)
|
||||
return false, fmt.Errorf("enable TOTP: %w", err)
|
||||
}
|
||||
return nil
|
||||
return tag.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
// ConsumeTOTPStep advances an account's accepted TOTP step only once. The
|
||||
// expected secret and enabled flag are part of the update so a concurrent
|
||||
// disable or re-enrollment cannot consume a code against stale state.
|
||||
func (s *Store) ConsumeTOTPStep(ctx context.Context, userID int64, secret string, step int64) (bool, error) {
|
||||
if err := s.validate(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
tag, err := s.pool.Exec(ctx, `
|
||||
UPDATE auth_users
|
||||
SET last_used_step = $1, updated_at = now()
|
||||
WHERE id = $2 AND totp_enabled AND totp_secret = $3 AND last_used_step < $1
|
||||
`, step, userID, secret)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("consume TOTP step: %w", err)
|
||||
}
|
||||
return tag.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
// DisableTOTP clears both the active and pending TOTP secret.
|
||||
@@ -279,7 +305,7 @@ func (s *Store) DisableTOTP(ctx context.Context, userID int64) error {
|
||||
if err := s.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := s.pool.Exec(ctx, `UPDATE auth_users SET totp_secret = '', totp_enabled = false, updated_at = now() WHERE id = $1`, userID)
|
||||
_, err := s.pool.Exec(ctx, `UPDATE auth_users SET totp_secret = '', totp_enabled = false, last_used_step = -1, updated_at = now() WHERE id = $1`, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("disable TOTP: %w", err)
|
||||
}
|
||||
@@ -382,6 +408,22 @@ func (s *Store) UpdateCredentials(ctx context.Context, id int64, username, email
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpgradePasswordHash replaces an old verifier only if it is still current,
|
||||
// so a concurrent password change cannot be overwritten by a login rehash.
|
||||
func (s *Store) UpgradePasswordHash(ctx context.Context, id int64, oldHash, newHash string) (bool, error) {
|
||||
if err := s.validate(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
tag, err := s.pool.Exec(ctx, `
|
||||
UPDATE auth_users SET password_hash = $1, updated_at = now()
|
||||
WHERE id = $2 AND password_hash = $3
|
||||
`, newHash, id, oldHash)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("upgrade password hash: %w", err)
|
||||
}
|
||||
return tag.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
// ListUsers returns the non-secret account fields used by the shared user
|
||||
// management screen.
|
||||
func (s *Store) ListUsers(ctx context.Context) ([]User, error) {
|
||||
|
||||
Reference in new issue
Block a user