package v1 import ( "context" "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 } // EmailConfirmation is what IdentityHandler needs from the confirmation usecase. type EmailConfirmation interface { Confirm(ctx context.Context, command identity.ConfirmCommand) (identity.ConfirmationResult, error) } // IdentityHandlerOption configures an optional identity operation. type IdentityHandlerOption func(*IdentityHandler) // IdentityHandler serves authentication and account identity operations. type IdentityHandler struct { registration AccountRegistration confirmation EmailConfirmation confirmationOrigin string sessions *identity.SessionService sessionOrigin 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 } } // WithSessionService serves login, current account, current session, and // logout against PostgreSQL-backed sessions. Unsafe browser operations compare // Origin exactly with the configured public origin. func WithSessionService(sessions *identity.SessionService, publicOrigin string) IdentityHandlerOption { return func(handler *IdentityHandler) { handler.sessions = sessions handler.sessionOrigin = publicOrigin } }