Files
lincodex/backend/internal/adapter/primary/http/v1/identity.go
T

63 lines
2.0 KiB
Go

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
sessions *identity.SessionService
publicOrigin string
}
func NewIdentityHandler(registration AccountRegistration, options ...IdentityHandlerOption) *IdentityHandler {
handler := &IdentityHandler{registration: registration}
for _, option := range options {
option(handler)
}
return handler
}
// WithEmailConfirmation serves ConfirmEmail. The configured public origin is
// supplied once with WithPublicOrigin; the request Host is never trusted.
func WithEmailConfirmation(confirmation EmailConfirmation) IdentityHandlerOption {
return func(handler *IdentityHandler) {
handler.confirmation = confirmation
}
}
// WithSessionService serves login, current account, current session, and
// logout against PostgreSQL-backed sessions.
func WithSessionService(sessions *identity.SessionService) IdentityHandlerOption {
return func(handler *IdentityHandler) {
handler.sessions = sessions
}
}
// WithPublicOrigin configures the exact browser Origin accepted by unsafe
// public identity operations and session CSRF checks.
func WithPublicOrigin(publicOrigin string) IdentityHandlerOption {
return func(handler *IdentityHandler) {
handler.publicOrigin = publicOrigin
}
}