package domain import ( "errors" "fmt" "time" "github.com/google/uuid" ) var ErrInvalidEmailToken = errors.New("invalid email token") // EmailTokenPurpose names what an email token authorizes. New purposes are // added here and in the email_tokens CHECK constraint together. type EmailTokenPurpose string const ( // EmailTokenPurposeEmailVerification authorizes confirming the account // email. EmailTokenPurposeEmailVerification EmailTokenPurpose = "email_verification" //nolint:gosec // A purpose name, not a credential. // EmailTokenPurposePasswordReset authorizes setting a new password. EmailTokenPurposePasswordReset EmailTokenPurpose = "password_reset" ) // ParseEmailTokenPurpose accepts a known purpose. func ParseEmailTokenPurpose(value string) (EmailTokenPurpose, error) { switch purpose := EmailTokenPurpose(value); purpose { case EmailTokenPurposeEmailVerification, EmailTokenPurposePasswordReset: return purpose, nil default: return "", fmt.Errorf("domain.ParseEmailTokenPurpose - unknown purpose: %w", ErrInvalidEmailToken) } } // 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. type EmailToken struct { ID uuid.UUID UserID uuid.UUID Purpose EmailTokenPurpose TokenHash SecretTokenHash RegistrationVersion int64 CreatedAt time.Time 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 // user's current registration version. type NewEmailTokenInput struct { UserID uuid.UUID Purpose EmailTokenPurpose TokenHash SecretTokenHash RegistrationVersion int64 IssuedAt time.Time Lifetime time.Duration // Pending is optional; see PendingRegistration. Pending *PendingRegistration } // NewEmailToken validates input and assigns a UUIDv7 identifier. func NewEmailToken(input NewEmailTokenInput) (EmailToken, error) { switch { case input.UserID == uuid.Nil: return EmailToken{}, invalidEmailToken("user ID must not be nil") case input.TokenHash.IsZero(): return EmailToken{}, invalidEmailToken("token hash must be set") case input.RegistrationVersion <= 0: return EmailToken{}, invalidEmailToken("registration version must be positive") case input.IssuedAt.IsZero(): return EmailToken{}, invalidEmailToken("issue time must be set") case input.Lifetime <= 0: return EmailToken{}, invalidEmailToken("lifetime must be positive") } if _, err := ParseEmailTokenPurpose(string(input.Purpose)); err != nil { return EmailToken{}, fmt.Errorf("domain.NewEmailToken - validate purpose: %w", err) } id, err := uuid.NewV7() if err != nil { return EmailToken{}, fmt.Errorf("domain.NewEmailToken - generate token ID: %w", err) } return EmailToken{ ID: id, UserID: input.UserID, Purpose: input.Purpose, TokenHash: input.TokenHash, RegistrationVersion: input.RegistrationVersion, CreatedAt: input.IssuedAt, ExpiresAt: input.IssuedAt.Add(input.Lifetime), Pending: input.Pending, }, nil } func invalidEmailToken(reason string) error { return fmt.Errorf("domain.NewEmailToken - %s: %w", reason, ErrInvalidEmailToken) }