auth

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package auth provides the core engine for initializing, configuring, and managing modular authentication plugins.

Index

Constants

View Source
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

View Source
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
)
View Source
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
)
View Source
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
)
View Source
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

func Plugin

func Plugin[P any](a *Auth) *P

Plugin retrieves a registered plugin instance of type P from the Auth engine in a strongly-typed manner. It panics if the specified plugin has not been registered during engine initialization.

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

func New(opts ...config.Option) (*Auth, error)

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

func (a *Auth) Hooks() *plugin.Hooks

Hooks returns the typed lifecycle hooks and native event dispatcher.

func (*Auth) Pipeline added in v1.0.0

func (a *Auth) Pipeline() *plugin.Pipeline

Pipeline returns the synchronous execution pipeline for intercepting and mutating authentication operations.

func (*Auth) RevokeSession added in v1.0.0

func (a *Auth) RevokeSession(ctx context.Context, token string) error

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

func (a *Auth) ValidateSession(ctx context.Context, token string) (*dto.SessionData, error)

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

type Hooks = plugin.Hooks

Re-exported Hooks types and constructors for top-level developer experience.

type Pipeline added in v1.0.0

type Pipeline = plugin.Pipeline

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

type SessionCreatedPayload struct {
	Session *entity.Session
	Extra   map[string]any
}

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

func (sm *SessionManager) GetSessionByID(ctx context.Context, id string) (*entity.Session, error)

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

func (sm *SessionManager) ResolveUser(ctx context.Context, userID string) (*entity.User, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL