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
@@ -97,6 +97,14 @@ func (store *IdentityStore) Sessions() outbound.SessionRepository {
|
||||
return sessionRepository{queries: store.queries}
|
||||
}
|
||||
|
||||
func (store *IdentityStore) IdempotencyRecords() outbound.IdempotencyRepository {
|
||||
return idempotencyRepository{queries: store.queries}
|
||||
}
|
||||
|
||||
func (unit *UnitOfWork) IdempotencyRecords() outbound.IdempotencyRepository {
|
||||
return idempotencyRepository{queries: unit.queries}
|
||||
}
|
||||
|
||||
func (unit *UnitOfWork) Users() outbound.UserRepository {
|
||||
return userRepository{queries: unit.queries}
|
||||
}
|
||||
@@ -226,6 +234,26 @@ func (repository userRepository) UpdateRegistration(ctx context.Context, user do
|
||||
return userFromRow(sqlc.User(row))
|
||||
}
|
||||
|
||||
func (repository userRepository) AdvanceRegistration(
|
||||
ctx context.Context,
|
||||
id uuid.UUID,
|
||||
expectedVersion int64,
|
||||
at time.Time,
|
||||
) (domain.User, error) {
|
||||
if err := requireTime("userRepository.AdvanceRegistration", at, domain.ErrInvalidUser); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
row, err := repository.queries.AdvanceUserRegistration(ctx, sqlc.AdvanceUserRegistrationParams{
|
||||
ID: pgUUID(id),
|
||||
ExpectedVersion: expectedVersion,
|
||||
RegisteredAt: pgTime(at),
|
||||
})
|
||||
if err != nil {
|
||||
return domain.User{}, fmt.Errorf("userRepository.AdvanceRegistration - update user: %w", mapNoRows(err, outbound.ErrUserVersionConflict))
|
||||
}
|
||||
return userFromRow(sqlc.User(row))
|
||||
}
|
||||
|
||||
func (repository userRepository) MarkEmailVerified(
|
||||
ctx context.Context,
|
||||
id uuid.UUID,
|
||||
@@ -318,7 +346,7 @@ func (repository emailTokenRepository) Insert(ctx context.Context, token domain.
|
||||
if token.ID == uuid.Nil || token.TokenHash.IsZero() {
|
||||
return fmt.Errorf("emailTokenRepository.Insert - token was not built by domain.NewEmailToken: %w", domain.ErrInvalidEmailToken)
|
||||
}
|
||||
err := repository.queries.InsertEmailToken(ctx, sqlc.InsertEmailTokenParams{
|
||||
params := sqlc.InsertEmailTokenParams{
|
||||
ID: pgUUID(token.ID),
|
||||
UserID: pgUUID(token.UserID),
|
||||
Purpose: string(token.Purpose),
|
||||
@@ -326,13 +354,62 @@ func (repository emailTokenRepository) Insert(ctx context.Context, token domain.
|
||||
RegistrationVersion: token.RegistrationVersion,
|
||||
CreatedAt: pgTime(token.CreatedAt),
|
||||
ExpiresAt: pgTime(token.ExpiresAt),
|
||||
})
|
||||
if err != nil {
|
||||
}
|
||||
if pending := token.Pending; pending != nil {
|
||||
params.PendingPasswordHash = pgtype.Text{String: pending.PasswordHash.PHC(), Valid: true}
|
||||
params.PendingScenario = pgtype.Text{String: string(pending.Scenario), Valid: true}
|
||||
params.PendingOnboardingStep = pgtype.Text{String: string(pending.OnboardingStep), Valid: true}
|
||||
}
|
||||
if err := repository.queries.InsertEmailToken(ctx, params); err != nil {
|
||||
return fmt.Errorf("emailTokenRepository.Insert - insert email token: %w", mapIdentityError(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository emailTokenRepository) LatestPending(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
registrationVersion int64,
|
||||
) (domain.PendingRegistration, bool, error) {
|
||||
row, err := repository.queries.GetLatestPendingRegistration(ctx, sqlc.GetLatestPendingRegistrationParams{
|
||||
UserID: pgUUID(userID),
|
||||
Purpose: string(domain.EmailTokenPurposeEmailVerification),
|
||||
RegistrationVersion: registrationVersion,
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.PendingRegistration{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return domain.PendingRegistration{}, false, fmt.Errorf("emailTokenRepository.LatestPending - select pending registration: %w", mapIdentityError(err))
|
||||
}
|
||||
pending, err := pendingFromColumns(row.PendingPasswordHash, row.PendingScenario, row.PendingOnboardingStep)
|
||||
if err != nil {
|
||||
return domain.PendingRegistration{}, false, err
|
||||
}
|
||||
return *pending, true, nil
|
||||
}
|
||||
|
||||
// pendingFromColumns decodes the pending credentials of a token row; nil when
|
||||
// the token carries none.
|
||||
func pendingFromColumns(hash, scenario, step pgtype.Text) (*domain.PendingRegistration, error) {
|
||||
if !hash.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
passwordHash, err := domain.NewPasswordHash(hash.String)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("postgres.pendingFromColumns - decode password hash: %w", err)
|
||||
}
|
||||
parsedScenario, err := domain.ParseScenario(scenario.String)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("postgres.pendingFromColumns - decode scenario: %w", err)
|
||||
}
|
||||
parsedStep, err := domain.ParseOnboardingStep(step.String)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("postgres.pendingFromColumns - decode onboarding step: %w", err)
|
||||
}
|
||||
return &domain.PendingRegistration{PasswordHash: passwordHash, Scenario: parsedScenario, OnboardingStep: parsedStep}, nil
|
||||
}
|
||||
|
||||
func (repository emailTokenRepository) InvalidateActive(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
@@ -416,7 +493,12 @@ func emailTokenFromRow(row sqlc.EmailToken) (domain.EmailToken, error) {
|
||||
if err != nil {
|
||||
return domain.EmailToken{}, err
|
||||
}
|
||||
pending, err := pendingFromColumns(row.PendingPasswordHash, row.PendingScenario, row.PendingOnboardingStep)
|
||||
if err != nil {
|
||||
return domain.EmailToken{}, err
|
||||
}
|
||||
return domain.EmailToken{
|
||||
Pending: pending,
|
||||
ID: uuid.UUID(row.ID.Bytes),
|
||||
UserID: uuid.UUID(row.UserID.Bytes),
|
||||
Purpose: purpose,
|
||||
|
||||
Reference in new issue
Block a user