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
@@ -32,6 +32,29 @@ func ParseEmailTokenPurpose(value string) (EmailTokenPurpose, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// PendingRegistration holds the credentials of one registration, which wait
|
||||
// on the verification token issued for it. A registration of an unverified
|
||||
// address never changes the account row; confirming a token applies its own
|
||||
// pending credentials, and only to a caller who proves the password of that
|
||||
// registration. Nothing here is valid for login before the confirmation.
|
||||
type PendingRegistration struct {
|
||||
PasswordHash PasswordHash
|
||||
Scenario Scenario
|
||||
OnboardingStep OnboardingStep
|
||||
}
|
||||
|
||||
// NewPendingRegistration builds the pending credentials of a registration
|
||||
// with the first onboarding step of its scenario.
|
||||
func NewPendingRegistration(hash PasswordHash, scenario Scenario) (PendingRegistration, error) {
|
||||
if hash.IsZero() {
|
||||
return PendingRegistration{}, invalidEmailToken("pending password hash must be built by NewPasswordHash")
|
||||
}
|
||||
if _, err := ParseScenario(string(scenario)); err != nil {
|
||||
return PendingRegistration{}, fmt.Errorf("domain.NewPendingRegistration - validate scenario: %w", err)
|
||||
}
|
||||
return PendingRegistration{PasswordHash: hash, Scenario: scenario, OnboardingStep: scenario.InitialOnboardingStep()}, nil
|
||||
}
|
||||
|
||||
// EmailToken is the stored record of a one-time token sent by email. It holds
|
||||
// only the hash of the raw token. A token ends at most once: it is either
|
||||
// consumed or invalidated, and it stops working at ExpiresAt.
|
||||
@@ -45,6 +68,9 @@ type EmailToken struct {
|
||||
ExpiresAt time.Time
|
||||
ConsumedAt *time.Time
|
||||
InvalidatedAt *time.Time
|
||||
// Pending is set on the verification token of a registration and nil on
|
||||
// a token that carries no credentials.
|
||||
Pending *PendingRegistration
|
||||
}
|
||||
|
||||
// NewEmailTokenInput describes a token to issue. RegistrationVersion is the
|
||||
@@ -56,6 +82,8 @@ type NewEmailTokenInput struct {
|
||||
RegistrationVersion int64
|
||||
IssuedAt time.Time
|
||||
Lifetime time.Duration
|
||||
// Pending is optional; see PendingRegistration.
|
||||
Pending *PendingRegistration
|
||||
}
|
||||
|
||||
// NewEmailToken validates input and assigns a UUIDv7 identifier.
|
||||
@@ -87,6 +115,7 @@ func NewEmailToken(input NewEmailTokenInput) (EmailToken, error) {
|
||||
RegistrationVersion: input.RegistrationVersion,
|
||||
CreatedAt: input.IssuedAt,
|
||||
ExpiresAt: input.IssuedAt.Add(input.Lifetime),
|
||||
Pending: input.Pending,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -184,7 +184,15 @@ func (registration Registration) validate(location string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// User is an account. Storage reconstructs it from trusted rows; new accounts
|
||||
// User is an account. The PasswordHash, Scenario, and OnboardingStep of an
|
||||
// unverified account are not authoritative: they hold what the first
|
||||
// registration of the address stored only because the columns are mandatory.
|
||||
// A repeat registration never changes them; the credentials that count are the
|
||||
// pending ones on the verification token, and confirming that token applies
|
||||
// them. No code may authenticate with, or trust the scenario of, an
|
||||
// unverified account row.
|
||||
//
|
||||
// Storage reconstructs it from trusted rows; new accounts
|
||||
// come from NewUser and changes from its methods.
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
|
||||
Reference in new issue
Block a user