91 lines
3.1 KiB
Go
91 lines
3.1 KiB
Go
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, such
|
|
// as a planned password reset, are added here and in the email_tokens CHECK
|
|
// constraint together.
|
|
type EmailTokenPurpose string
|
|
|
|
// EmailTokenPurposeEmailVerification authorizes confirming the account email.
|
|
const EmailTokenPurposeEmailVerification EmailTokenPurpose = "email_verification" //nolint:gosec // A purpose name, not a credential.
|
|
|
|
// ParseEmailTokenPurpose accepts a known purpose.
|
|
func ParseEmailTokenPurpose(value string) (EmailTokenPurpose, error) {
|
|
purpose := EmailTokenPurpose(value)
|
|
if purpose != EmailTokenPurposeEmailVerification {
|
|
return "", fmt.Errorf("domain.ParseEmailTokenPurpose - unknown purpose: %w", ErrInvalidEmailToken)
|
|
}
|
|
return purpose, 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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),
|
|
}, nil
|
|
}
|
|
|
|
func invalidEmailToken(reason string) error {
|
|
return fmt.Errorf("domain.NewEmailToken - %s: %w", reason, ErrInvalidEmailToken)
|
|
}
|