feat(identity): register accounts and resend verification mail

This commit is contained in:
shira committed 2026-10-06 23:59:51 +03:00
1 parent 5e202ac972
commit 4436f626a3
55 files changed
+3227 -88

No files matched your search

@@ -32,19 +32,55 @@ type Handler struct {
api.UnimplementedHandler
health *HealthHandler
identity *IdentityHandler
problems *problem.Responder
}
var _ api.Handler = (*Handler)(nil)
func NewHandler(health *HealthHandler, problems *problem.Responder) *Handler {
return &Handler{health: health, problems: problems}
// HandlerOption adds the handler of a business area to Handler.
type HandlerOption func(*Handler)
// WithIdentity serves the registration operations. A nil handler leaves them
// to the embedded api.UnimplementedHandler, as when no database is
// configured.
func WithIdentity(identity *IdentityHandler) HandlerOption {
return func(handler *Handler) { handler.identity = identity }
}
func NewHandler(health *HealthHandler, problems *problem.Responder, options ...HandlerOption) *Handler {
handler := &Handler{health: health, problems: problems}
for _, option := range options {
option(handler)
}
return handler
}
func (handler *Handler) GetHealthz(ctx context.Context) (*api.HealthResponse, error) {
return handler.health.GetHealthz(ctx)
}
func (handler *Handler) RegisterAccount(
ctx context.Context,
request *api.RegistrationRequest,
params api.RegisterAccountParams,
) (api.RegisterAccountRes, error) {
if handler.identity == nil {
return handler.UnimplementedHandler.RegisterAccount(ctx, request, params)
}
return handler.identity.RegisterAccount(ctx, request, params)
}
func (handler *Handler) ResendEmailVerification(
ctx context.Context,
request *api.EmailVerificationResendRequest,
) (api.ResendEmailVerificationRes, error) {
if handler.identity == nil {
return handler.UnimplementedHandler.ResendEmailVerification(ctx, request)
}
return handler.identity.ResendEmailVerification(ctx, request)
}
// 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)
@@ -0,0 +1,119 @@
package v1
import (
"context"
"errors"
"math"
"time"
"lincodex/backend/internal/adapter/primary/http/api"
httpmiddleware "lincodex/backend/internal/adapter/primary/http/middleware"
"lincodex/backend/internal/adapter/primary/http/problem"
"lincodex/backend/internal/usecase/identity"
)
// AccountRegistration is what IdentityHandler needs from the registration
// usecase.
type AccountRegistration interface {
Register(ctx context.Context, command identity.RegisterCommand) error
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
}
func NewIdentityHandler(registration AccountRegistration) *IdentityHandler {
return &IdentityHandler{registration: registration}
}
func (handler *IdentityHandler) RegisterAccount(
ctx context.Context,
request *api.RegistrationRequest,
params api.RegisterAccountParams,
) (api.RegisterAccountRes, error) {
err := handler.registration.Register(ctx, identity.RegisterCommand{
IdempotencyKey: params.IdempotencyKey,
Email: string(request.Email),
Password: string(request.Password),
Scenario: string(request.Scenario),
ConsentTextVersion: string(request.ConsentC1.TextVersion),
ClientKey: clientKey(ctx),
})
switch {
case err == nil:
return accepted(), nil
case errors.Is(err, identity.ErrInvalidField):
return (*api.RegisterAccountBadRequest)(&invalidFieldProblem(ctx, err).Response), nil
case errors.Is(err, identity.ErrIdempotencyKeyReused):
return (*api.RegisterAccountUnprocessableEntity)(&problem.New(ctx, api.ProblemCodeIdempotencyKeyReused).Response), nil
}
if limited, ok := rateLimited(ctx, err); ok {
return limited, nil
}
return nil, err
}
func (handler *IdentityHandler) ResendEmailVerification(
ctx context.Context,
request *api.EmailVerificationResendRequest,
) (api.ResendEmailVerificationRes, error) {
err := handler.registration.ResendVerification(ctx, identity.ResendCommand{
Email: string(request.Email),
ClientKey: clientKey(ctx),
})
switch {
case err == nil:
return accepted(), nil
case errors.Is(err, identity.ErrInvalidField):
return (*api.ResendEmailVerificationBadRequest)(&invalidFieldProblem(ctx, err).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}
}
// clientKey is the limiter key of the client network address that the
// ClientIP middleware resolved. Without it, for example when the middleware
// is not mounted, the per-client limit is skipped and only the per-address
// limits apply: a shared bucket would let one client lock everyone out.
func clientKey(ctx context.Context) string {
address, ok := httpmiddleware.ClientAddr(ctx)
if !ok {
return ""
}
return httpmiddleware.LimiterKey(address)
}
func invalidFieldProblem(ctx context.Context, err error) *api.ProblemStatusCode {
field, ok := errors.AsType[*identity.InvalidFieldError](err)
if !ok {
return problem.InvalidField(ctx, api.ProblemFieldLocationBody, "")
}
location := api.ProblemFieldLocationBody
if field.Pointer == "/Idempotency-Key" {
location = api.ProblemFieldLocationHeader
}
return problem.InvalidField(ctx, location, field.Pointer)
}
func rateLimited(ctx context.Context, err error) (*api.TooManyRequestsHeaders, bool) {
limited, ok := errors.AsType[*identity.RateLimitedError](err)
if !ok {
return nil, false
}
return problem.RateLimited(ctx, wholeSeconds(limited.RetryAfter)), true
}
// wholeSeconds rounds a wait up, so a client that waits the stated time is
// not refused again for the remainder.
func wholeSeconds(wait time.Duration) int32 {
return int32(min(math.Ceil(wait.Seconds()), math.MaxInt32))
}