feat(identity): register accounts and resend verification mail
This commit is contained in:
1 parent
5e202ac972
commit
4436f626a3
55 files changed
+3227
-88
No files matched your search
@@ -11,6 +11,104 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const advanceUserRegistration = `-- name: AdvanceUserRegistration :one
|
||||
WITH updated AS (
|
||||
UPDATE users
|
||||
SET
|
||||
registration_version = registration_version + 1,
|
||||
last_registered_at = $1,
|
||||
updated_at = $1,
|
||||
version = version + 1
|
||||
WHERE
|
||||
users.id = $2
|
||||
AND users.version = $3
|
||||
AND users.email_verified_at IS NULL
|
||||
RETURNING
|
||||
users.id,
|
||||
users.email,
|
||||
users.email_normalized,
|
||||
users.password_hash,
|
||||
users.scenario,
|
||||
users.onboarding_step,
|
||||
users.email_verified_at,
|
||||
users.registration_version,
|
||||
users.last_registered_at,
|
||||
users.version,
|
||||
users.created_at,
|
||||
users.updated_at
|
||||
),
|
||||
|
||||
discarded_consent AS (
|
||||
DELETE FROM consent_records
|
||||
USING updated
|
||||
WHERE
|
||||
consent_records.subject_id = updated.id
|
||||
AND consent_records.purpose = 'c1'
|
||||
AND consent_records.source = 'registration'
|
||||
)
|
||||
|
||||
SELECT
|
||||
updated.id,
|
||||
updated.email,
|
||||
updated.email_normalized,
|
||||
updated.password_hash,
|
||||
updated.scenario,
|
||||
updated.onboarding_step,
|
||||
updated.email_verified_at,
|
||||
updated.registration_version,
|
||||
updated.last_registered_at,
|
||||
updated.version,
|
||||
updated.created_at,
|
||||
updated.updated_at
|
||||
FROM updated
|
||||
`
|
||||
|
||||
type AdvanceUserRegistrationParams struct {
|
||||
RegisteredAt pgtype.Timestamptz
|
||||
ID pgtype.UUID
|
||||
ExpectedVersion int64
|
||||
}
|
||||
|
||||
type AdvanceUserRegistrationRow struct {
|
||||
ID pgtype.UUID
|
||||
Email string
|
||||
EmailNormalized string
|
||||
PasswordHash string
|
||||
Scenario string
|
||||
OnboardingStep string
|
||||
EmailVerifiedAt pgtype.Timestamptz
|
||||
RegistrationVersion int64
|
||||
LastRegisteredAt pgtype.Timestamptz
|
||||
Version int64
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Starts a new registration of an unverified account without touching its
|
||||
// credentials, scenario, or onboarding step: the new registration carries
|
||||
// them on its verification token. Advances the registration version, which
|
||||
// ends every earlier token, and deletes the registration C1 of the earlier
|
||||
// registrant like UpdateUserRegistration does.
|
||||
func (q *Queries) AdvanceUserRegistration(ctx context.Context, arg AdvanceUserRegistrationParams) (AdvanceUserRegistrationRow, error) {
|
||||
row := q.db.QueryRow(ctx, advanceUserRegistration, arg.RegisteredAt, arg.ID, arg.ExpectedVersion)
|
||||
var i AdvanceUserRegistrationRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Email,
|
||||
&i.EmailNormalized,
|
||||
&i.PasswordHash,
|
||||
&i.Scenario,
|
||||
&i.OnboardingStep,
|
||||
&i.EmailVerifiedAt,
|
||||
&i.RegistrationVersion,
|
||||
&i.LastRegisteredAt,
|
||||
&i.Version,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const consumeEmailToken = `-- name: ConsumeEmailToken :one
|
||||
UPDATE email_tokens AS token
|
||||
SET consumed_at = $1
|
||||
@@ -32,7 +130,10 @@ RETURNING
|
||||
token.created_at,
|
||||
token.expires_at,
|
||||
token.consumed_at,
|
||||
token.invalidated_at
|
||||
token.invalidated_at,
|
||||
token.pending_password_hash,
|
||||
token.pending_scenario,
|
||||
token.pending_onboarding_step
|
||||
`
|
||||
|
||||
type ConsumeEmailTokenParams struct {
|
||||
@@ -63,6 +164,9 @@ func (q *Queries) ConsumeEmailToken(ctx context.Context, arg ConsumeEmailTokenPa
|
||||
&i.ExpiresAt,
|
||||
&i.ConsumedAt,
|
||||
&i.InvalidatedAt,
|
||||
&i.PendingPasswordHash,
|
||||
&i.PendingScenario,
|
||||
&i.PendingOnboardingStep,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -273,6 +377,71 @@ func (q *Queries) GetEmailTokenState(ctx context.Context, arg GetEmailTokenState
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getLatestPendingRegistration = `-- name: GetLatestPendingRegistration :one
|
||||
SELECT
|
||||
pending_password_hash,
|
||||
pending_scenario,
|
||||
pending_onboarding_step
|
||||
FROM email_tokens
|
||||
WHERE
|
||||
user_id = $1
|
||||
AND purpose = $2
|
||||
AND registration_version = $3
|
||||
AND pending_password_hash IS NOT NULL
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetLatestPendingRegistrationParams struct {
|
||||
UserID pgtype.UUID
|
||||
Purpose string
|
||||
RegistrationVersion int64
|
||||
}
|
||||
|
||||
type GetLatestPendingRegistrationRow struct {
|
||||
PendingPasswordHash pgtype.Text
|
||||
PendingScenario pgtype.Text
|
||||
PendingOnboardingStep pgtype.Text
|
||||
}
|
||||
|
||||
// The pending credentials of the newest token of a registration, for a
|
||||
// resend, which issues its token with the same credentials.
|
||||
func (q *Queries) GetLatestPendingRegistration(ctx context.Context, arg GetLatestPendingRegistrationParams) (GetLatestPendingRegistrationRow, error) {
|
||||
row := q.db.QueryRow(ctx, getLatestPendingRegistration, arg.UserID, arg.Purpose, arg.RegistrationVersion)
|
||||
var i GetLatestPendingRegistrationRow
|
||||
err := row.Scan(&i.PendingPasswordHash, &i.PendingScenario, &i.PendingOnboardingStep)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getLiveIdempotencyRecord = `-- name: GetLiveIdempotencyRecord :one
|
||||
SELECT
|
||||
scope_hash,
|
||||
fingerprint,
|
||||
created_at,
|
||||
expires_at
|
||||
FROM idempotency_records
|
||||
WHERE
|
||||
scope_hash = $1
|
||||
AND expires_at > $2
|
||||
`
|
||||
|
||||
type GetLiveIdempotencyRecordParams struct {
|
||||
ScopeHash []byte
|
||||
At pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) GetLiveIdempotencyRecord(ctx context.Context, arg GetLiveIdempotencyRecordParams) (IdempotencyRecord, error) {
|
||||
row := q.db.QueryRow(ctx, getLiveIdempotencyRecord, arg.ScopeHash, arg.At)
|
||||
var i IdempotencyRecord
|
||||
err := row.Scan(
|
||||
&i.ScopeHash,
|
||||
&i.Fingerprint,
|
||||
&i.CreatedAt,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserByEmailNormalized = `-- name: GetUserByEmailNormalized :one
|
||||
SELECT
|
||||
id,
|
||||
@@ -357,7 +526,10 @@ INSERT INTO email_tokens (
|
||||
token_hash,
|
||||
registration_version,
|
||||
created_at,
|
||||
expires_at
|
||||
expires_at,
|
||||
pending_password_hash,
|
||||
pending_scenario,
|
||||
pending_onboarding_step
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
@@ -365,18 +537,24 @@ INSERT INTO email_tokens (
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7
|
||||
$7,
|
||||
$8,
|
||||
$9,
|
||||
$10
|
||||
)
|
||||
`
|
||||
|
||||
type InsertEmailTokenParams struct {
|
||||
ID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
Purpose string
|
||||
TokenHash []byte
|
||||
RegistrationVersion int64
|
||||
CreatedAt pgtype.Timestamptz
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
ID pgtype.UUID
|
||||
UserID pgtype.UUID
|
||||
Purpose string
|
||||
TokenHash []byte
|
||||
RegistrationVersion int64
|
||||
CreatedAt pgtype.Timestamptz
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
PendingPasswordHash pgtype.Text
|
||||
PendingScenario pgtype.Text
|
||||
PendingOnboardingStep pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) InsertEmailToken(ctx context.Context, arg InsertEmailTokenParams) error {
|
||||
@@ -388,6 +566,9 @@ func (q *Queries) InsertEmailToken(ctx context.Context, arg InsertEmailTokenPara
|
||||
arg.RegistrationVersion,
|
||||
arg.CreatedAt,
|
||||
arg.ExpiresAt,
|
||||
arg.PendingPasswordHash,
|
||||
arg.PendingScenario,
|
||||
arg.PendingOnboardingStep,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -496,7 +677,12 @@ func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) error {
|
||||
|
||||
const invalidateActiveEmailTokens = `-- name: InvalidateActiveEmailTokens :execrows
|
||||
UPDATE email_tokens
|
||||
SET invalidated_at = $1
|
||||
SET
|
||||
invalidated_at = $1,
|
||||
-- An invalidated token keeps no password hash: it can never be confirmed.
|
||||
pending_password_hash = NULL,
|
||||
pending_scenario = NULL,
|
||||
pending_onboarding_step = NULL
|
||||
WHERE
|
||||
user_id = $2
|
||||
AND purpose = $3
|
||||
@@ -571,6 +757,83 @@ func (q *Queries) MarkUserEmailVerified(ctx context.Context, arg MarkUserEmailVe
|
||||
return i, err
|
||||
}
|
||||
|
||||
const purgeExpiredIdempotencyRecords = `-- name: PurgeExpiredIdempotencyRecords :execrows
|
||||
DELETE FROM idempotency_records
|
||||
WHERE scope_hash IN (
|
||||
SELECT stale.scope_hash
|
||||
FROM idempotency_records AS stale
|
||||
WHERE stale.expires_at <= $1
|
||||
ORDER BY stale.expires_at
|
||||
LIMIT $2
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
`
|
||||
|
||||
type PurgeExpiredIdempotencyRecordsParams struct {
|
||||
At pgtype.Timestamptz
|
||||
BatchLimit int32
|
||||
}
|
||||
|
||||
// Deletes at most batch_limit expired records. Locked rows are skipped, so
|
||||
// the statement never waits for a concurrent reservation.
|
||||
func (q *Queries) PurgeExpiredIdempotencyRecords(ctx context.Context, arg PurgeExpiredIdempotencyRecordsParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, purgeExpiredIdempotencyRecords, arg.At, arg.BatchLimit)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const reserveIdempotencyRecord = `-- name: ReserveIdempotencyRecord :one
|
||||
INSERT INTO idempotency_records (
|
||||
scope_hash,
|
||||
fingerprint,
|
||||
created_at,
|
||||
expires_at
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4
|
||||
)
|
||||
ON CONFLICT (scope_hash) DO UPDATE
|
||||
SET
|
||||
fingerprint = excluded.fingerprint,
|
||||
created_at = excluded.created_at,
|
||||
expires_at = excluded.expires_at
|
||||
WHERE idempotency_records.expires_at <= excluded.created_at
|
||||
RETURNING scope_hash, fingerprint, created_at, expires_at
|
||||
`
|
||||
|
||||
type ReserveIdempotencyRecordParams struct {
|
||||
ScopeHash []byte
|
||||
Fingerprint []byte
|
||||
CreatedAt pgtype.Timestamptz
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Stores the replay record of a command. A record whose retention has passed
|
||||
// is replaced, so the key is accepted as new after the retention. A live
|
||||
// record is left as it is and nothing is returned; the caller then reads it
|
||||
// with GetLiveIdempotencyRecord. Concurrent callers with the same scope wait
|
||||
// on the row of the first one until its transaction ends.
|
||||
func (q *Queries) ReserveIdempotencyRecord(ctx context.Context, arg ReserveIdempotencyRecordParams) (IdempotencyRecord, error) {
|
||||
row := q.db.QueryRow(ctx, reserveIdempotencyRecord,
|
||||
arg.ScopeHash,
|
||||
arg.Fingerprint,
|
||||
arg.CreatedAt,
|
||||
arg.ExpiresAt,
|
||||
)
|
||||
var i IdempotencyRecord
|
||||
err := row.Scan(
|
||||
&i.ScopeHash,
|
||||
&i.Fingerprint,
|
||||
&i.CreatedAt,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const revokeSession = `-- name: RevokeSession :execrows
|
||||
UPDATE sessions
|
||||
SET revoked_at = $1
|
||||
|
||||
Reference in new issue
Block a user