Documentation
¶
Index ¶
- func ResolveAuth(app forge.App) (*authsome.Auth, error)
- type Config
- type ConfigOption
- func WithAuthMiddlewareConfig(config middleware.AuthMiddlewareConfig) ConfigOption
- func WithBasePath(path string) ConfigOption
- func WithCORSEnabled(enabled bool) ConfigOption
- func WithConfig(config Config) ConfigOption
- func WithDatabase(db *bun.DB) ConfigOption
- func WithDatabaseName(name string) ConfigOption
- func WithDisableOpenAPI(disable bool) ConfigOption
- func WithGeoIPProvider(provider security.GeoIPProvider) ConfigOption
- func WithGlobalCookieConfig(config session.CookieConfig) ConfigOption
- func WithMinPasswordLength(length int) ConfigOption
- func WithPasswordPolicy(policy string) ConfigOption
- func WithPasswordRequirements(reqs validator.PasswordRequirements) ConfigOption
- func WithPlugins(plugins ...plugins.Plugin) ConfigOption
- func WithRBACEnforcement(enabled bool) ConfigOption
- func WithRateLimitConfig(config ratelimit.Config) ConfigOption
- func WithRateLimitStorage(storage ratelimit.Storage) ConfigOption
- func WithRefreshTokens(enabled bool, accessTTL, refreshTTL time.Duration) ConfigOption
- func WithRequireConfig(require bool) ConfigOption
- func WithRequireEmailVerification(require bool) ConfigOption
- func WithSecret(secret string) ConfigOption
- func WithSecurityConfig(config security.Config) ConfigOption
- func WithSessionConfig(config session.Config) ConfigOption
- func WithSessionCookieEnabled(enabled bool) ConfigOption
- func WithSessionCookieMaxAge(seconds int) ConfigOption
- func WithSessionCookieName(name string) ConfigOption
- func WithSessionTTL(defaultTTL, rememberTTL time.Duration) ConfigOption
- func WithSlidingWindowSessions(enabled bool, renewalThreshold ...time.Duration) ConfigOption
- func WithTrustedOrigins(origins []string) ConfigOption
- func WithUserConfig(config user.Config) ConfigOption
- type Extension
- func (e *Extension) Auth() *authsome.Auth
- func (e *Extension) ExcludeFromSchemas() bool
- func (e *Extension) GetBasePath() string
- func (e *Extension) GetDB() *bun.DB
- func (e *Extension) GetPluginRegistry() plugins.PluginRegistry
- func (e *Extension) GetServiceRegistry() *registry.ServiceRegistry
- func (e *Extension) Health(ctx context.Context) error
- func (e *Extension) Register(app forge.App) error
- func (e *Extension) RegisterPlugin(plugin plugins.Plugin) error
- func (e *Extension) Start(ctx context.Context) error
- func (e *Extension) Stop(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// RequireEmailVerified requires email verification for all users
RequireEmailVerified bool `yaml:"requireEmailVerified" json:"requireEmailVerified"`
// DisableOpenAPI disables the OpenAPI documentation
DisableOpenAPI bool `yaml:"disableOpenAPI" json:"disableOpenAPI"`
// BasePath is the base path where auth routes are mounted
BasePath string `yaml:"basePath" json:"basePath"`
// Database configuration - mutually exclusive options
// Database is a direct database connection (takes precedence)
Database interface{} `yaml:"-" json:"-"`
// DatabaseName is the name of the database to use from DatabaseManager
DatabaseName string `yaml:"databaseName" json:"databaseName"`
// CORS configuration
CORSEnabled bool `yaml:"corsEnabled" json:"corsEnabled"`
TrustedOrigins []string `yaml:"trustedOrigins" json:"trustedOrigins"`
// Secret for signing tokens
Secret string `yaml:"secret" json:"secret"`
// RBACEnforce enables handler-level RBAC enforcement
RBACEnforce bool `yaml:"rbacEnforce" json:"rbacEnforce"`
// SecurityConfig for IP/country restrictions
SecurityConfig *security.Config `yaml:"security" json:"security"`
// RateLimitConfig for rate limiting
RateLimitConfig *ratelimit.Config `yaml:"rateLimit" json:"rateLimit"`
// RateLimitStorage is the storage backend for rate limiting
RateLimitStorage ratelimit.Storage `yaml:"-" json:"-"`
// GeoIPProvider for country-based restrictions
GeoIPProvider security.GeoIPProvider `yaml:"-" json:"-"`
// SessionCookie configures cookie-based session management
SessionCookie *session.CookieConfig `yaml:"sessionCookie" json:"sessionCookie"`
// SessionConfig configures session behavior (TTL, sliding window, refresh tokens)
SessionConfig *session.Config `yaml:"sessionConfig" json:"sessionConfig"`
// UserConfig configures user service behavior (password requirements, etc.)
UserConfig *user.Config `yaml:"userConfig" json:"userConfig"`
// AuthMiddlewareConfig configures the authentication middleware behavior
AuthMiddlewareConfig *middleware.AuthMiddlewareConfig `yaml:"authMiddleware" json:"authMiddleware"`
// Plugins to register with AuthSome
Plugins []plugins.Plugin `yaml:"-" json:"-"`
// RequireConfig determines if configuration must be loaded from file
RequireConfig bool `yaml:"-" json:"-"`
}
Config holds the configuration for the AuthSome extension
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption is a functional option for configuring the extension
func WithAuthMiddlewareConfig ¶ added in v0.0.2
func WithAuthMiddlewareConfig(config middleware.AuthMiddlewareConfig) ConfigOption
WithAuthMiddlewareConfig sets the authentication middleware configuration This controls how the global authentication middleware behaves, including: - Session cookie name - Optional authentication (allow unauthenticated requests) - API key authentication settings - Context resolution (app/environment from headers or API key)
Example:
WithAuthMiddlewareConfig(middleware.AuthMiddlewareConfig{
SessionCookieName: "my_session",
Optional: true,
AllowAPIKeyInQuery: false, // Security best practice
AllowSessionInQuery: false, // Security best practice
Context: middleware.ContextConfig{
AutoDetectFromAPIKey: true,
AutoDetectFromConfig: true,
},
})
func WithBasePath ¶
func WithBasePath(path string) ConfigOption
WithBasePath sets the base path for routes
func WithCORSEnabled ¶ added in v0.0.2
func WithCORSEnabled(enabled bool) ConfigOption
WithCORSEnabled enables or disables CORS middleware
func WithConfig ¶
func WithConfig(config Config) ConfigOption
WithConfig sets the entire configuration
func WithDatabase ¶
func WithDatabase(db *bun.DB) ConfigOption
WithDatabase sets a direct database connection
func WithDatabaseName ¶
func WithDatabaseName(name string) ConfigOption
WithDatabaseName sets the database name to use from DatabaseManager
func WithDisableOpenAPI ¶ added in v0.0.2
func WithDisableOpenAPI(disable bool) ConfigOption
func WithGeoIPProvider ¶
func WithGeoIPProvider(provider security.GeoIPProvider) ConfigOption
WithGeoIPProvider sets the GeoIP provider
func WithGlobalCookieConfig ¶ added in v0.0.2
func WithGlobalCookieConfig(config session.CookieConfig) ConfigOption
WithGlobalCookieConfig sets the global cookie configuration for session management This configuration applies to all apps unless overridden at the app level Example:
WithGlobalCookieConfig(session.CookieConfig{
Enabled: true,
Name: "my_session",
HttpOnly: true,
SameSite: "Lax",
})
func WithMinPasswordLength ¶ added in v0.0.3
func WithMinPasswordLength(length int) ConfigOption
WithMinPasswordLength sets the minimum password length
Example:
extension.WithMinPasswordLength(12)
func WithPasswordPolicy ¶ added in v0.0.3
func WithPasswordPolicy(policy string) ConfigOption
WithPasswordPolicy is a convenience function to set common password policies Predefined policies: "weak", "medium", "strong", "enterprise"
Example:
extension.WithPasswordPolicy("strong")
func WithPasswordRequirements ¶ added in v0.0.3
func WithPasswordRequirements(reqs validator.PasswordRequirements) ConfigOption
WithPasswordRequirements sets the password requirements This controls password validation for user registration and password changes
Example:
extension.WithPasswordRequirements(validator.PasswordRequirements{
MinLength: 12,
RequireUpper: true,
RequireLower: true,
RequireNumber: true,
RequireSpecial: true,
})
func WithPlugins ¶
func WithPlugins(plugins ...plugins.Plugin) ConfigOption
WithPlugins sets the plugins to register
func WithRBACEnforcement ¶
func WithRBACEnforcement(enabled bool) ConfigOption
WithRBACEnforcement enables/disables RBAC enforcement
func WithRateLimitConfig ¶
func WithRateLimitConfig(config ratelimit.Config) ConfigOption
WithRateLimitConfig sets rate limit configuration
func WithRateLimitStorage ¶
func WithRateLimitStorage(storage ratelimit.Storage) ConfigOption
WithRateLimitStorage sets the rate limit storage backend
func WithRefreshTokens ¶ added in v0.0.3
func WithRefreshTokens(enabled bool, accessTTL, refreshTTL time.Duration) ConfigOption
WithRefreshTokens enables the refresh token pattern Short-lived access tokens are issued with long-lived refresh tokens Clients must explicitly refresh when access token expires
Example:
extension.WithRefreshTokens(true, 15*time.Minute, 30*24*time.Hour) // 15 min access tokens, 30 day refresh tokens
func WithRequireConfig ¶
func WithRequireConfig(require bool) ConfigOption
WithRequireConfig sets whether configuration must be loaded from file
func WithRequireEmailVerification ¶ added in v0.0.7
func WithRequireEmailVerification(require bool) ConfigOption
func WithSecret ¶
func WithSecret(secret string) ConfigOption
WithSecret sets the secret for token signing
func WithSecurityConfig ¶
func WithSecurityConfig(config security.Config) ConfigOption
WithSecurityConfig sets security configuration
func WithSessionConfig ¶ added in v0.0.3
func WithSessionConfig(config session.Config) ConfigOption
WithSessionConfig sets the full session configuration This controls session behavior including TTL, sliding window, and refresh tokens
Example:
extension.WithSessionConfig(session.Config{
DefaultTTL: 24 * time.Hour,
RememberTTL: 7 * 24 * time.Hour,
EnableSlidingWindow: true,
SlidingRenewalAfter: 5 * time.Minute,
EnableRefreshTokens: true,
RefreshTokenTTL: 30 * 24 * time.Hour,
AccessTokenTTL: 15 * time.Minute,
})
func WithSessionCookieEnabled ¶ added in v0.0.2
func WithSessionCookieEnabled(enabled bool) ConfigOption
WithSessionCookieEnabled enables or disables cookie-based session management globally When enabled, authentication responses will automatically set secure HTTP cookies
func WithSessionCookieMaxAge ¶ added in v0.0.3
func WithSessionCookieMaxAge(seconds int) ConfigOption
WithSessionCookieMaxAge sets the cookie MaxAge in seconds This controls how long the browser keeps the cookie If not set, defaults to session TTL (24 hours)
Example:
extension.WithSessionCookieMaxAge(3600) // 1 hour extension.WithSessionCookieMaxAge(86400) // 24 hours
func WithSessionCookieName ¶ added in v0.0.2
func WithSessionCookieName(name string) ConfigOption
WithSessionCookieName sets the session cookie name Default: "authsome_session"
func WithSessionTTL ¶ added in v0.0.3
func WithSessionTTL(defaultTTL, rememberTTL time.Duration) ConfigOption
WithSessionTTL sets the default and "remember me" session TTL
Example:
extension.WithSessionTTL(24*time.Hour, 7*24*time.Hour)
func WithSlidingWindowSessions ¶ added in v0.0.3
func WithSlidingWindowSessions(enabled bool, renewalThreshold ...time.Duration) ConfigOption
WithSlidingWindowSessions enables automatic session renewal on each request When enabled, sessions are extended whenever the user makes a request The renewalThreshold determines how often to actually update the database (default: 5 minutes) This prevents logging out active users while minimizing database writes
Example:
extension.WithSlidingWindowSessions(true, 5*time.Minute)
func WithTrustedOrigins ¶
func WithTrustedOrigins(origins []string) ConfigOption
WithTrustedOrigins sets trusted origins for CORS and auto-enables CORS if origins provided
func WithUserConfig ¶ added in v0.0.3
func WithUserConfig(config user.Config) ConfigOption
WithUserConfig sets the full user configuration This controls user service behavior including password requirements
Example:
extension.WithUserConfig(user.Config{
PasswordRequirements: validator.PasswordRequirements{
MinLength: 12,
RequireUpper: true,
RequireLower: true,
RequireNumber: true,
RequireSpecial: true,
},
})
type Extension ¶
type Extension struct {
*forge.BaseExtension
// contains filtered or unexported fields
}
Extension implements the Forge extension interface for AuthSome
func NewExtension ¶
func NewExtension(opts ...ConfigOption) *Extension
NewExtension creates a new AuthSome extension with optional configuration
func ResolveExtension ¶
ResolveExtension resolves the AuthSome extension from a Forge app This allows you to access the extension instance after registration
func (*Extension) Auth ¶
Auth returns the AuthSome instance Use this to access AuthSome programmatically after extension is registered
func (*Extension) ExcludeFromSchemas ¶ added in v0.0.2
func (*Extension) GetBasePath ¶
GetBasePath returns the configured base path This is used by plugins to construct URLs
func (*Extension) GetDB ¶
GetDB returns the database instance This is used by plugins that need direct database access
func (*Extension) GetPluginRegistry ¶
func (e *Extension) GetPluginRegistry() plugins.PluginRegistry
GetPluginRegistry returns the plugin registry for plugin detection This is used by the dashboard plugin to detect which plugins are enabled
func (*Extension) GetServiceRegistry ¶
func (e *Extension) GetServiceRegistry() *registry.ServiceRegistry
GetServiceRegistry returns the service registry This is used by plugins that need access to core services
func (*Extension) RegisterPlugin ¶
RegisterPlugin registers a plugin before Start is called