Documentation
¶
Overview ¶
Package auth provides the core engine for initializing, configuring, and managing modular authentication plugins.
Index ¶
- Constants
- Variables
- func Plugin[P any](a *Auth) *P
- type AfterCreateSessionFunc
- type AfterCreateSessionInterceptor
- type AfterSignInFunc
- type AfterSignInInterceptor
- type AfterSignUpFunc
- type AfterSignUpInterceptor
- type AfterValidateSessionFunc
- type AfterValidateSessionInterceptor
- type Auth
- func (a *Auth) CreateSession(ctx context.Context, userID string, opts ...SessionOption) (*entity.Session, error)
- func (a *Auth) Hooks() *plugin.Hooks
- func (a *Auth) Pipeline() *plugin.Pipeline
- func (a *Auth) RevokeSession(ctx context.Context, token string) error
- func (a *Auth) SessionManager() *SessionManager
- func (a *Auth) ValidateSession(ctx context.Context, token string) (*dto.SessionData, error)
- func (a *Auth) VerifyChallenge(ctx context.Context, params dto.VerifyChallengeParams, opts ...SessionOption) (*dto.SessionData, error)
- func (a *Auth) VerifyTwoFactorChallenge(ctx context.Context, challengeToken string, code string, opts ...SessionOption) (*dto.SessionData, error)
- type BeforeCreateSessionFunc
- type BeforeCreateSessionInterceptor
- type BeforeSignInFunc
- type BeforeSignInInterceptor
- type BeforeSignUpFunc
- type BeforeSignUpInterceptor
- type BeforeValidateSessionFunc
- type BeforeValidateSessionInterceptor
- type Hooks
- type Pipeline
- type SessionConfig
- type SessionCreatedPayload
- type SessionInterceptor
- type SessionManager
- func (sm *SessionManager) CreateSession(ctx context.Context, userID string, opts ...SessionOption) (*entity.Session, error)
- func (sm *SessionManager) GetSessionByID(ctx context.Context, id string) (*entity.Session, error)
- func (sm *SessionManager) GetSessionByToken(ctx context.Context, token string) (*entity.Session, error)
- func (sm *SessionManager) ListSessionsByUserID(ctx context.Context, userID string) ([]*entity.Session, error)
- func (sm *SessionManager) ResolveUser(ctx context.Context, userID string) (*entity.User, error)
- func (sm *SessionManager) RevokeSession(ctx context.Context, token string) error
- func (sm *SessionManager) RevokeSessionsByUserID(ctx context.Context, userID string) error
- func (sm *SessionManager) ValidateSession(ctx context.Context, token string) (*dto.SessionData, error)
- type SessionOption
- type SessionOptions
- type SessionRevokedPayload
- type SignInHandler
- type SignInInterceptor
- type SignInMiddleware
- type SignUpHandler
- type SignUpInterceptor
- type SignUpMiddleware
- type UserResolver
- type ValidateSessionInterceptor
Constants ¶
const ( // EventSessionCreated is published when a new authenticated session is created. EventSessionCreated = "auth:session:created" // EventSessionRevoked is published when an active session is explicitly revoked. EventSessionRevoked = "auth:session:revoked" // EventSessionValidated is published when a session token is successfully validated. EventSessionValidated = "auth:session:validated" )
Variables ¶
var ( // NewHooks creates a new native typed lifecycle hooks and event dispatcher. NewHooks = plugin.NewHooks // WithHooks configures a custom hooks dispatcher in engine configuration. WithHooks = config.WithHooks // WithAsyncHooks configures whether lifecycle hooks are dispatched asynchronously. WithAsyncHooks = config.WithAsyncHooks )
var ( // NewPipeline creates a new synchronous execution pipeline. NewPipeline = plugin.NewPipeline // WithInterceptor registers interceptors globally in engine configuration. WithInterceptor = config.WithInterceptor // WithBeforeSignIn registers a BeforeSignIn functional interceptor. WithBeforeSignIn = config.WithBeforeSignIn // WithAfterSignIn registers an AfterSignIn functional interceptor. WithAfterSignIn = config.WithAfterSignIn // WithBeforeSignUp registers a BeforeSignUp functional interceptor. WithBeforeSignUp = config.WithBeforeSignUp // WithAfterSignUp registers an AfterSignUp functional interceptor. WithAfterSignUp = config.WithAfterSignUp // WithBeforeCreateSession registers a BeforeCreateSession functional interceptor. WithBeforeCreateSession = config.WithBeforeCreateSession // WithAfterCreateSession registers an AfterCreateSession functional interceptor. WithAfterCreateSession = config.WithAfterCreateSession // WithCallInterceptor attaches an interceptor scoped strictly to an individual call. WithCallInterceptor = plugin.WithCallInterceptor )
var ( // ErrSessionRepositoryRequired is returned when session operations are invoked without configuring a SessionRepository. ErrSessionRepositoryRequired = errors.New("auth: session repository is required") // ErrSessionManagerRequired is returned when session operations are invoked without an active SessionManager in context. ErrSessionManagerRequired = plugin.ErrSessionManagerRequired // ErrInvalidUserID is returned when an empty user ID is provided to session creation. ErrInvalidUserID = errors.New("auth: user ID cannot be empty") // ErrMFAEngineNotConfigured is returned when attempting MFA challenge verification without a registered MFAProvider plugin. ErrMFAEngineNotConfigured = domain.ErrMFAEngineNotConfigured // ErrInvalidChallengeToken is returned when an invalid challenge token is submitted. ErrInvalidChallengeToken = domain.ErrInvalidChallengeToken // ErrChallengeExpired is returned when a challenge token has exceeded its validity time. ErrChallengeExpired = domain.ErrChallengeExpired )
var ( // DefaultSessionConfig returns default session lifetime settings. DefaultSessionConfig = config.DefaultSessionConfig // WithDuration configures a custom expiration duration for the session. WithDuration = plugin.WithDuration // WithRememberMe configures extended session lifetime for remember-me requests. WithRememberMe = plugin.WithRememberMe // WithIPAddress sets the client IP address initiating the session. WithIPAddress = plugin.WithIPAddress // WithUserAgent sets the client User-Agent initiating the session. WithUserAgent = plugin.WithUserAgent // WithDeviceID sets the physical device identifier for the session. WithDeviceID = plugin.WithDeviceID // WithExtra sets a single dynamic metadata key-value in the session. WithExtra = plugin.WithExtra // WithExtraMap copies dynamic metadata key-value pairs into the session. WithExtraMap = plugin.WithExtraMap )
Functions ¶
Types ¶
type AfterCreateSessionFunc ¶ added in v1.0.0
type AfterCreateSessionFunc = plugin.AfterCreateSessionFunc
Re-exported interceptor interfaces for top-level developer experience.
type AfterCreateSessionInterceptor ¶ added in v1.0.0
type AfterCreateSessionInterceptor = plugin.AfterCreateSessionInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type AfterSignInFunc ¶ added in v1.0.0
type AfterSignInFunc = plugin.AfterSignInFunc
Re-exported interceptor interfaces for top-level developer experience.
type AfterSignInInterceptor ¶ added in v1.0.0
type AfterSignInInterceptor = plugin.AfterSignInInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type AfterSignUpFunc ¶ added in v1.0.0
type AfterSignUpFunc = plugin.AfterSignUpFunc
Re-exported interceptor interfaces for top-level developer experience.
type AfterSignUpInterceptor ¶ added in v1.0.0
type AfterSignUpInterceptor = plugin.AfterSignUpInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type AfterValidateSessionFunc ¶ added in v1.0.0
type AfterValidateSessionFunc = plugin.AfterValidateSessionFunc
Re-exported interceptor interfaces for top-level developer experience.
type AfterValidateSessionInterceptor ¶ added in v1.0.0
type AfterValidateSessionInterceptor = plugin.AfterValidateSessionInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Auth is the central authentication engine holding initialized plugins, session orchestrators, execution pipelines, and lifecycle hooks.
func New ¶
New creates and initializes a new Auth instance with the provided configuration options. It initializes all registered plugins with a shared plugin context and configures the central SessionManager if a repository is provided.
func (*Auth) CreateSession ¶ added in v1.0.0
func (a *Auth) CreateSession(ctx context.Context, userID string, opts ...SessionOption) (*entity.Session, error)
CreateSession creates and persists a new authenticated user session directly through the central SessionManager.
func (*Auth) Hooks ¶ added in v1.0.0
Hooks returns the typed lifecycle hooks and native event dispatcher.
func (*Auth) Pipeline ¶ added in v1.0.0
Pipeline returns the synchronous execution pipeline for intercepting and mutating authentication operations.
func (*Auth) RevokeSession ¶ added in v1.0.0
RevokeSession revokes an active session by token directly through the central SessionManager.
func (*Auth) SessionManager ¶ added in v1.0.0
func (a *Auth) SessionManager() *SessionManager
SessionManager returns the central SessionManager orchestrator, or nil if no SessionRepository was configured.
func (*Auth) ValidateSession ¶ added in v1.0.0
ValidateSession validates an active session token, returning combined user and session data directly through the central SessionManager.
func (*Auth) VerifyChallenge ¶ added in v1.0.0
func (a *Auth) VerifyChallenge(ctx context.Context, params dto.VerifyChallengeParams, opts ...SessionOption) (*dto.SessionData, error)
VerifyChallenge completes an active MFA challenge (TOTP, backup code, OTP), atomically revoking the challenge token, and issues an authenticated session via the central SessionManager.
func (*Auth) VerifyTwoFactorChallenge ¶ added in v1.0.0
func (a *Auth) VerifyTwoFactorChallenge(ctx context.Context, challengeToken string, code string, opts ...SessionOption) (*dto.SessionData, error)
VerifyTwoFactorChallenge is a convenient shortcut to verify a TOTP or backup code MFA challenge by token and code.
type BeforeCreateSessionFunc ¶ added in v1.0.0
type BeforeCreateSessionFunc = plugin.BeforeCreateSessionFunc
Re-exported interceptor interfaces for top-level developer experience.
type BeforeCreateSessionInterceptor ¶ added in v1.0.0
type BeforeCreateSessionInterceptor = plugin.BeforeCreateSessionInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type BeforeSignInFunc ¶ added in v1.0.0
type BeforeSignInFunc = plugin.BeforeSignInFunc
Re-exported interceptor interfaces for top-level developer experience.
type BeforeSignInInterceptor ¶ added in v1.0.0
type BeforeSignInInterceptor = plugin.BeforeSignInInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type BeforeSignUpFunc ¶ added in v1.0.0
type BeforeSignUpFunc = plugin.BeforeSignUpFunc
Re-exported interceptor interfaces for top-level developer experience.
type BeforeSignUpInterceptor ¶ added in v1.0.0
type BeforeSignUpInterceptor = plugin.BeforeSignUpInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type BeforeValidateSessionFunc ¶ added in v1.0.0
type BeforeValidateSessionFunc = plugin.BeforeValidateSessionFunc
Re-exported interceptor interfaces for top-level developer experience.
type BeforeValidateSessionInterceptor ¶ added in v1.0.0
type BeforeValidateSessionInterceptor = plugin.BeforeValidateSessionInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type Hooks ¶ added in v1.0.0
Re-exported Hooks types and constructors for top-level developer experience.
type Pipeline ¶ added in v1.0.0
Re-exported interceptor interfaces for top-level developer experience.
type SessionConfig ¶ added in v1.0.0
type SessionConfig = config.SessionConfig
Type aliases for seamless DX.
type SessionCreatedPayload ¶ added in v1.0.0
SessionCreatedPayload defines the payload published on EventSessionCreated.
func (*SessionCreatedPayload) GetExtra ¶ added in v1.0.0
func (p *SessionCreatedPayload) GetExtra() map[string]any
GetExtra returns the dynamic extra metadata map from the payload.
func (*SessionCreatedPayload) GetSession ¶ added in v1.0.0
func (p *SessionCreatedPayload) GetSession() *entity.Session
GetSession returns the Session entity from the payload.
type SessionInterceptor ¶ added in v1.0.0
type SessionInterceptor = plugin.SessionInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type SessionManager ¶ added in v1.0.0
type SessionManager struct {
// contains filtered or unexported fields
}
SessionManager coordinates session lifecycle, cryptographic generation, persistence, and event dispatching.
func NewSessionManager ¶ added in v1.0.0
func NewSessionManager(repo repository.SessionRepository, cfg config.SessionConfig, crypto plugin.CryptoUtils, pipeline *plugin.Pipeline, hooks *plugin.Hooks, userResolver ...UserResolver) *SessionManager
NewSessionManager creates a new central SessionManager instance.
func (*SessionManager) CreateSession ¶ added in v1.0.0
func (sm *SessionManager) CreateSession(ctx context.Context, userID string, opts ...SessionOption) (*entity.Session, error)
CreateSession creates and persists a new authenticated user session with cryptographic token generation, pipeline interceptor execution (with immediate abort and rollback capability), and typed lifecycle hook dispatching.
func (*SessionManager) GetSessionByID ¶ added in v1.0.0
GetSessionByID retrieves an active session by its primary key ID.
func (*SessionManager) GetSessionByToken ¶ added in v1.0.0
func (sm *SessionManager) GetSessionByToken(ctx context.Context, token string) (*entity.Session, error)
GetSessionByToken retrieves an active session by its unique token without loading the user.
func (*SessionManager) ListSessionsByUserID ¶ added in v1.0.0
func (sm *SessionManager) ListSessionsByUserID(ctx context.Context, userID string) ([]*entity.Session, error)
ListSessionsByUserID retrieves all active sessions for the specified user.
func (*SessionManager) ResolveUser ¶ added in v1.0.0
ResolveUser retrieves the user entity associated with a user ID using the configured UserResolver or SessionRepository.
func (*SessionManager) RevokeSession ¶ added in v1.0.0
func (sm *SessionManager) RevokeSession(ctx context.Context, token string) error
RevokeSession revokes an active session by token, removing it from storage and publishing EventSessionRevoked.
func (*SessionManager) RevokeSessionsByUserID ¶ added in v1.0.0
func (sm *SessionManager) RevokeSessionsByUserID(ctx context.Context, userID string) error
RevokeSessionsByUserID deletes all active sessions for the specified user.
func (*SessionManager) ValidateSession ¶ added in v1.0.0
func (sm *SessionManager) ValidateSession(ctx context.Context, token string) (*dto.SessionData, error)
ValidateSession validates an active session token, returning combined user and session data in *dto.SessionData.
type SessionOption ¶ added in v1.0.0
type SessionOption = plugin.SessionOption
Type aliases for seamless DX.
type SessionOptions ¶ added in v1.0.0
type SessionOptions = plugin.SessionOptions
Type aliases for seamless DX.
type SessionRevokedPayload ¶ added in v1.0.0
type SessionRevokedPayload struct {
Token string
SessionID string
UserID string
Extra map[string]any
}
SessionRevokedPayload defines the payload published on EventSessionRevoked.
func (*SessionRevokedPayload) GetToken ¶ added in v1.0.0
func (p *SessionRevokedPayload) GetToken() string
GetToken returns the revoked session token string.
type SignInHandler ¶ added in v1.0.0
type SignInHandler = plugin.SignInHandler
Re-exported interceptor interfaces for top-level developer experience.
type SignInInterceptor ¶ added in v1.0.0
type SignInInterceptor = plugin.SignInInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type SignInMiddleware ¶ added in v1.0.0
type SignInMiddleware = plugin.SignInMiddleware
Re-exported interceptor interfaces for top-level developer experience.
type SignUpHandler ¶ added in v1.0.0
type SignUpHandler = plugin.SignUpHandler
Re-exported interceptor interfaces for top-level developer experience.
type SignUpInterceptor ¶ added in v1.0.0
type SignUpInterceptor = plugin.SignUpInterceptor
Re-exported interceptor interfaces for top-level developer experience.
type SignUpMiddleware ¶ added in v1.0.0
type SignUpMiddleware = plugin.SignUpMiddleware
Re-exported interceptor interfaces for top-level developer experience.
type UserResolver ¶ added in v1.0.0
type UserResolver interface {
GetUserByID(ctx context.Context, userID string) (*entity.User, error)
}
UserResolver allows resolving user profiles by ID during session validation.
type ValidateSessionInterceptor ¶ added in v1.0.0
type ValidateSessionInterceptor = plugin.ValidateSessionInterceptor
Re-exported interceptor interfaces for top-level developer experience.