feat(identity): register accounts and resend verification mail

This commit is contained in:
shira committed 2026-10-06 23:59:51 +03:00
1 parent 5e202ac972
commit 4436f626a3
55 files changed
+3227 -88

No files matched your search

@@ -67,6 +67,7 @@ type IdentityRepositories interface {
Users() UserRepository
EmailTokens() EmailTokenRepository
Sessions() SessionRepository
IdempotencyRecords() IdempotencyRepository
}
// UserRepository stores accounts. Lookups by email use the normalized form.
@@ -97,6 +98,18 @@ type UserRepository interface {
// deletes nothing.
UpdateRegistration(ctx context.Context, user domain.User) (domain.User, error)
// AdvanceRegistration starts a new registration of an unverified account
// without changing its password hash, scenario, or onboarding step: the
// new registration carries them on its token (see
// domain.PendingRegistration). It applies only when the stored version
// still equals expectedVersion and the email is unverified, advances
// Version and RegistrationVersion by one in the same statement, sets the
// registration time, and deletes the C1 records with the source
// registration of the earlier registration like UpdateRegistration does;
// the caller grants the new C1 strictly after it. Otherwise it returns
// ErrUserVersionConflict.
AdvanceRegistration(ctx context.Context, id uuid.UUID, expectedVersion int64, at time.Time) (domain.User, error)
// MarkEmailVerified sets the verification time when the account is still
// unverified and still has registrationVersion, and returns the stored
// account. Otherwise it returns ErrUserVersionConflict. A confirmation
@@ -124,6 +137,11 @@ type EmailTokenRepository interface {
// ErrTokenHashConflict for a duplicate hash.
Insert(ctx context.Context, token domain.EmailToken) error
// LatestPending returns the pending credentials of the newest token that
// the registration version of the user has, or false when none carries
// any. A resend copies them to its new token.
LatestPending(ctx context.Context, userID uuid.UUID, registrationVersion int64) (domain.PendingRegistration, bool, error)
// InvalidateActive marks every unconsumed, not yet invalidated token of
// the user and purpose as invalidated at the given time, and returns how
// many it marked.
@@ -174,3 +192,51 @@ type SessionRepository interface {
// how many it revoked.
RevokeAllForUser(ctx context.Context, userID uuid.UUID, at time.Time) (int64, error)
}
// IdempotencyRetention is how long the replay record of a command with an
// Idempotency-Key lives, as the public contract states. After it the key may
// be accepted as new.
const IdempotencyRetention = 24 * time.Hour
// IdempotencyDigestSize is the size of the HMAC-SHA-256 values that identify
// and fingerprint a command.
const IdempotencyDigestSize = 32
// IdempotencyRecord is the replay record of a committed command. Both digests
// are HMAC-SHA-256 values computed with a server secret; the record holds
// neither the key, the email, nor the request body.
type IdempotencyRecord struct {
// Scope identifies the operation, the key, and the principal; for an
// anonymous operation the principal is the normalized email.
Scope [IdempotencyDigestSize]byte
// Fingerprint identifies the request that the key was first used with.
Fingerprint [IdempotencyDigestSize]byte
CreatedAt time.Time
ExpiresAt time.Time
}
// IdempotencyRepository stores the replay records of commands. Reserve runs
// in the transaction of the command it guards, so a stored record always
// belongs to a committed command.
type IdempotencyRepository interface {
// Reserve stores record unless a live record with the same scope exists.
// For a stored record it returns it with created true. For a live record
// it returns that record with created false and stores nothing; the
// caller compares the fingerprints to decide between a replay and a
// reused key. A concurrent Reserve with the same scope waits until the
// transaction of the first ends. A record past ExpiresAt is replaced.
// Every method that takes a time rejects the zero time.
Reserve(ctx context.Context, record IdempotencyRecord) (stored IdempotencyRecord, created bool, err error)
// Find returns the live record with the scope at the given time, or
// ErrIdempotencyRecordNotFound.
Find(ctx context.Context, scope [IdempotencyDigestSize]byte, at time.Time) (IdempotencyRecord, error)
// PurgeExpired deletes at most limit records that expired at or before
// the given time and returns how many it deleted. It never waits for a
// locked record.
PurgeExpired(ctx context.Context, at time.Time, limit int32) (int64, error)
}
// ErrIdempotencyRecordNotFound reports that no live record has the scope.
var ErrIdempotencyRecordNotFound = errors.New("idempotency record not found")
@@ -0,0 +1,39 @@
package outbound
import (
"context"
"errors"
"time"
)
// ErrInvalidRateLimitRule reports a rule without a positive window or limit,
// or a take without a key or a rule.
var ErrInvalidRateLimitRule = errors.New("invalid rate limit rule")
// RateLimitRule allows at most Limit takes per fixed Window. The window of a
// key starts with its first take and ends Window later.
type RateLimitRule struct {
Window time.Duration
Limit int64
}
// RateLimitDecision is the outcome of Take.
type RateLimitDecision struct {
// Allowed reports that the take was counted against every rule.
Allowed bool
// RetryAfter is, for a refused take, how long until every rule that
// refused it admits another one. It is zero for an allowed take.
RetryAfter time.Duration
}
// RateLimiter counts requests of one caller key against fixed windows. The
// counters are ephemeral: a restart or a flush of the store resets them, so
// a limiter is an abuse brake, never a record of what happened.
type RateLimiter interface {
// Take counts one request of key against every rule at once. It counts
// nothing when any rule refuses, so refused requests do not extend a
// window. The key is an opaque string that must not hold personal data
// in clear form; callers pass a keyed digest. A store failure returns
// an error and the caller decides how to fail.
Take(ctx context.Context, key string, rules ...RateLimitRule) (RateLimitDecision, error)
}