feat(identity): confirm registration email atomically
This commit is contained in:
1 parent
8e7ce8d71c
commit
fade4e76e4
75 files changed
+4188
-403
No files matched your search
@@ -81,6 +81,17 @@ func (handler *Handler) ResendEmailVerification(
|
||||
return handler.identity.ResendEmailVerification(ctx, request)
|
||||
}
|
||||
|
||||
func (handler *Handler) ConfirmEmail(
|
||||
ctx context.Context,
|
||||
request *api.EmailVerificationRequest,
|
||||
params api.ConfirmEmailParams,
|
||||
) (api.ConfirmEmailRes, error) {
|
||||
if handler.identity == nil || handler.identity.confirmation == nil {
|
||||
return handler.UnimplementedHandler.ConfirmEmail(ctx, request, params)
|
||||
}
|
||||
return handler.identity.ConfirmEmail(ctx, request, params)
|
||||
}
|
||||
|
||||
// NewError implements api.Handler for errors returned by operations.
|
||||
func (handler *Handler) NewError(ctx context.Context, err error) *api.ProblemStatusCode {
|
||||
return handler.problems.HandlerError(ctx, err)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/internal/adapter/primary/http/api"
|
||||
@@ -19,14 +20,37 @@ type AccountRegistration interface {
|
||||
ResendVerification(ctx context.Context, command identity.ResendCommand) error
|
||||
}
|
||||
|
||||
// IdentityHandler serves the anonymous registration operations. Both answer
|
||||
// the same neutral 202 whatever the state of the address; see the contract.
|
||||
type IdentityHandler struct {
|
||||
registration AccountRegistration
|
||||
// EmailConfirmation is what IdentityHandler needs from the confirmation usecase.
|
||||
type EmailConfirmation interface {
|
||||
Confirm(ctx context.Context, command identity.ConfirmCommand) (identity.ConfirmationResult, error)
|
||||
}
|
||||
|
||||
func NewIdentityHandler(registration AccountRegistration) *IdentityHandler {
|
||||
return &IdentityHandler{registration: registration}
|
||||
// IdentityHandlerOption configures an optional identity operation.
|
||||
type IdentityHandlerOption func(*IdentityHandler)
|
||||
|
||||
// IdentityHandler serves anonymous registration, resend, and confirmation
|
||||
// operations.
|
||||
type IdentityHandler struct {
|
||||
registration AccountRegistration
|
||||
confirmation EmailConfirmation
|
||||
confirmationOrigin string
|
||||
}
|
||||
|
||||
func NewIdentityHandler(registration AccountRegistration, options ...IdentityHandlerOption) *IdentityHandler {
|
||||
handler := &IdentityHandler{registration: registration}
|
||||
for _, option := range options {
|
||||
option(handler)
|
||||
}
|
||||
return handler
|
||||
}
|
||||
|
||||
// WithEmailConfirmation serves ConfirmEmail and compares Origin with the
|
||||
// configured public application origin. The request Host is never trusted.
|
||||
func WithEmailConfirmation(confirmation EmailConfirmation, publicOrigin string) IdentityHandlerOption {
|
||||
return func(handler *IdentityHandler) {
|
||||
handler.confirmation = confirmation
|
||||
handler.confirmationOrigin = publicOrigin
|
||||
}
|
||||
}
|
||||
|
||||
func (handler *IdentityHandler) RegisterAccount(
|
||||
@@ -60,6 +84,9 @@ func (handler *IdentityHandler) ResendEmailVerification(
|
||||
ctx context.Context,
|
||||
request *api.EmailVerificationResendRequest,
|
||||
) (api.ResendEmailVerificationRes, error) {
|
||||
if handler.registration == nil {
|
||||
return nil, errors.New("IdentityHandler.ResendEmailVerification - registration is not configured")
|
||||
}
|
||||
err := handler.registration.ResendVerification(ctx, identity.ResendCommand{
|
||||
Email: string(request.Email),
|
||||
ClientKey: clientKey(ctx),
|
||||
@@ -76,6 +103,42 @@ func (handler *IdentityHandler) ResendEmailVerification(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (handler *IdentityHandler) ConfirmEmail(
|
||||
ctx context.Context,
|
||||
request *api.EmailVerificationRequest,
|
||||
params api.ConfirmEmailParams,
|
||||
) (api.ConfirmEmailRes, error) {
|
||||
if handler.confirmation == nil {
|
||||
return nil, errors.New("IdentityHandler.ConfirmEmail - confirmation is not configured")
|
||||
}
|
||||
origin, ok := params.Origin.Get()
|
||||
if !ok || strings.TrimSpace(origin) == "" || origin != handler.confirmationOrigin {
|
||||
return (*api.ConfirmEmailForbidden)(&problem.New(ctx, api.ProblemCodeCsrfCheckFailed).Response), nil
|
||||
}
|
||||
|
||||
result, err := handler.confirmation.Confirm(ctx, identity.ConfirmCommand{
|
||||
Token: string(request.Token),
|
||||
Password: request.Password,
|
||||
ClientKey: clientKey(ctx),
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
return &api.EmailVerificationResult{
|
||||
EmailVerified: api.EmailVerificationResultEmailVerifiedTrue,
|
||||
Scenario: api.AccountScenario(result.Scenario),
|
||||
OnboardingStep: api.OnboardingStep(result.OnboardingStep),
|
||||
}, nil
|
||||
case errors.Is(err, identity.ErrVerificationTokenInvalid):
|
||||
return (*api.ConfirmEmailBadRequest)(&problem.New(ctx, api.ProblemCodeVerificationTokenInvalid).Response), nil
|
||||
case errors.Is(err, identity.ErrVerificationTokenExpired):
|
||||
return (*api.ConfirmEmailBadRequest)(&problem.New(ctx, api.ProblemCodeVerificationTokenExpired).Response), nil
|
||||
}
|
||||
if limited, ok := rateLimited(ctx, err); ok {
|
||||
return limited, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func accepted() *api.IdentityRequestAccepted {
|
||||
return &api.IdentityRequestAccepted{Status: api.IdentityRequestAcceptedStatusAccepted}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user