Documentation
¶
Index ¶
- Constants
- type Account
- type AccountDatabaseHooksConfig
- type AccountService
- type AfterCreateHook
- type AfterDeleteHook
- type AfterReadHook
- type AfterUpdateHook
- type Api
- type BasePlugin
- func (p *BasePlugin) Close() error
- func (p *BasePlugin) Config() PluginConfig
- func (p *BasePlugin) Ctx() *PluginContext
- func (p *BasePlugin) DatabaseHooks() *PluginDatabaseHooks
- func (p *BasePlugin) EventHooks() *PluginEventHooks
- func (p *BasePlugin) Init(ctx *PluginContext) error
- func (p *BasePlugin) Metadata() PluginMetadata
- func (p *BasePlugin) Migrations() []any
- func (p *BasePlugin) RateLimit() *PluginRateLimit
- func (p *BasePlugin) Routes() []PluginRoute
- func (p *BasePlugin) SetClose(fn func() error)
- func (p *BasePlugin) SetConfig(cfg PluginConfig)
- func (p *BasePlugin) SetCtx(ctx *PluginContext)
- func (p *BasePlugin) SetDatabaseHooks(hooks *PluginDatabaseHooks)
- func (p *BasePlugin) SetEventHooks(hooks *PluginEventHooks)
- func (p *BasePlugin) SetInit(fn func(ctx *PluginContext) error)
- func (p *BasePlugin) SetMetadata(meta PluginMetadata)
- func (p *BasePlugin) SetMigrations(migrations []any)
- func (p *BasePlugin) SetRateLimit(rateLimit *PluginRateLimit)
- func (p *BasePlugin) SetRoutes(routes []PluginRoute)
- type BeforeCreateHook
- type BeforeDeleteHook
- type BeforeReadHook
- type BeforeUpdateHook
- type CSRFConfig
- type ChangeEmailConfig
- type Config
- type ConfigOption
- type CustomRoute
- type CustomRouteHandler
- type CustomRouteMiddleware
- type DatabaseConfig
- type DatabaseHooksConfig
- type DefaultOAuth2ProvidersConfig
- type EmailPasswordConfig
- type EmailVerificationConfig
- type EndpointHookContext
- type EndpointHooksConfig
- type Event
- type EventBus
- type EventBusConfig
- type EventHandler
- type EventHooksConfig
- type EventPublisher
- type EventSubscriber
- type GenericOAuth2Config
- type GenericOAuth2EndpointConfig
- type IPConfig
- type KeyValueStore
- type Message
- type OAuth2Config
- type OAuth2UserInfo
- type PasswordConfig
- type Plugin
- type PluginConfig
- type PluginContext
- type PluginDatabaseHookOperations
- type PluginDatabaseHooks
- type PluginEventHookFunc
- type PluginEventHookPayload
- type PluginEventHooks
- type PluginMetadata
- type PluginMiddleware
- type PluginOption
- type PluginRateLimit
- type PluginRoute
- type PluginRouteHandler
- type PluginRouteMiddleware
- type PluginsConfig
- type ProviderType
- type PubSub
- type RateLimitConfig
- type RateLimitCustomRule
- type RateLimitCustomRuleFunc
- type RateLimitService
- type SecondaryStorage
- type SecondaryStorageConfig
- type SecondaryStorageDatabaseOptions
- type SecondaryStorageMemoryOptions
- type SecondaryStorageType
- type Session
- type SessionConfig
- type SessionDatabaseHooksConfig
- type SessionService
- type SocialProvidersConfig
- type SubscriptionID
- type TokenService
- type TrustedOriginsConfig
- type TypedPluginEventHook
- type User
- type UserConfig
- type UserDatabaseHooksConfig
- type UserService
- type Verification
- type VerificationDatabaseHooksConfig
- type VerificationService
- type VerificationType
Constants ¶
const ( EventUserSignedUp = "user.signed_up" EventUserLoggedIn = "user.logged_in" EventEmailVerified = "user.email_verified" EventPasswordChanged = "user.password_changed" EventEmailChanged = "user.email_changed" )
Built-in event types for the authentication system
const (
RateLimitAlgorithmFixedWindow = "fixed_window"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Account ¶
type Account struct {
ID string `json:"id" gorm:"primaryKey"`
UserID string `json:"user_id" gorm:"index"`
AccountID string `json:"account_id"`
ProviderID ProviderType `json:"provider_id"`
AccessToken *string `json:"access_token,omitempty"`
RefreshToken *string `json:"refresh_token,omitempty"`
IDToken *string `json:"id_token,omitempty"`
AccessTokenExpiresAt *time.Time `json:"access_token_expires_at,omitempty"`
RefreshTokenExpiresAt *time.Time `json:"refresh_token_expires_at,omitempty"`
Scope *string `json:"scope,omitempty"`
Password *string `json:"password,omitempty"` // for email/password auth
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
User User `gorm:"foreignKey:UserID"`
}
type AccountService ¶
type AfterCreateHook ¶
type AfterCreateHook[T any] func(ctx *PluginContext, entity *T) error
type AfterDeleteHook ¶
type AfterDeleteHook[T any] func(ctx *PluginContext, entity *T) error
type AfterReadHook ¶
type AfterReadHook[T any] func(ctx *PluginContext, results *[]T) error
type AfterUpdateHook ¶
type AfterUpdateHook[T any] func(ctx *PluginContext, updated *T) error
type Api ¶
type Api struct {
Users UserService
Accounts AccountService
Sessions SessionService
Verifications VerificationService
Tokens TokenService
RateLimit RateLimitService
}
type BasePlugin ¶ added in v1.3.4
type BasePlugin struct {
// contains filtered or unexported fields
}
func (*BasePlugin) Close ¶ added in v1.3.4
func (p *BasePlugin) Close() error
func (*BasePlugin) Config ¶ added in v1.3.4
func (p *BasePlugin) Config() PluginConfig
func (*BasePlugin) Ctx ¶ added in v1.3.4
func (p *BasePlugin) Ctx() *PluginContext
func (*BasePlugin) DatabaseHooks ¶ added in v1.3.4
func (p *BasePlugin) DatabaseHooks() *PluginDatabaseHooks
func (*BasePlugin) EventHooks ¶ added in v1.3.4
func (p *BasePlugin) EventHooks() *PluginEventHooks
func (*BasePlugin) Init ¶ added in v1.3.4
func (p *BasePlugin) Init(ctx *PluginContext) error
func (*BasePlugin) Metadata ¶ added in v1.3.4
func (p *BasePlugin) Metadata() PluginMetadata
func (*BasePlugin) Migrations ¶ added in v1.3.4
func (p *BasePlugin) Migrations() []any
func (*BasePlugin) RateLimit ¶ added in v1.3.4
func (p *BasePlugin) RateLimit() *PluginRateLimit
func (*BasePlugin) Routes ¶ added in v1.3.4
func (p *BasePlugin) Routes() []PluginRoute
func (*BasePlugin) SetClose ¶ added in v1.3.4
func (p *BasePlugin) SetClose(fn func() error)
func (*BasePlugin) SetConfig ¶ added in v1.3.4
func (p *BasePlugin) SetConfig(cfg PluginConfig)
func (*BasePlugin) SetCtx ¶ added in v1.3.4
func (p *BasePlugin) SetCtx(ctx *PluginContext)
func (*BasePlugin) SetDatabaseHooks ¶ added in v1.3.4
func (p *BasePlugin) SetDatabaseHooks(hooks *PluginDatabaseHooks)
func (*BasePlugin) SetEventHooks ¶ added in v1.3.4
func (p *BasePlugin) SetEventHooks(hooks *PluginEventHooks)
func (*BasePlugin) SetInit ¶ added in v1.3.4
func (p *BasePlugin) SetInit(fn func(ctx *PluginContext) error)
func (*BasePlugin) SetMetadata ¶ added in v1.3.4
func (p *BasePlugin) SetMetadata(meta PluginMetadata)
func (*BasePlugin) SetMigrations ¶ added in v1.3.4
func (p *BasePlugin) SetMigrations(migrations []any)
func (*BasePlugin) SetRateLimit ¶ added in v1.3.4
func (p *BasePlugin) SetRateLimit(rateLimit *PluginRateLimit)
func (*BasePlugin) SetRoutes ¶ added in v1.3.4
func (p *BasePlugin) SetRoutes(routes []PluginRoute)
type BeforeCreateHook ¶
type BeforeCreateHook[T any] func(ctx *PluginContext, entity *T) error
type BeforeDeleteHook ¶
type BeforeDeleteHook[T any] func(ctx *PluginContext, entity *T) error
type BeforeReadHook ¶
type BeforeReadHook[T any] func(ctx *PluginContext) error
type BeforeUpdateHook ¶
type BeforeUpdateHook[T any] func(ctx *PluginContext, existing *T, updatedData map[string]any) error
type CSRFConfig ¶
type ChangeEmailConfig ¶
type Config ¶
type Config struct {
AppName string
BaseURL string
BasePath string
Secret string
DB *gorm.DB
Database DatabaseConfig
SecondaryStorage SecondaryStorageConfig
EmailPassword EmailPasswordConfig
EmailVerification EmailVerificationConfig
User UserConfig
Session SessionConfig
CSRF CSRFConfig
SocialProviders SocialProvidersConfig
TrustedOrigins TrustedOriginsConfig
RateLimit RateLimitConfig
EndpointHooks EndpointHooksConfig
DatabaseHooks DatabaseHooksConfig
EventHooks EventHooksConfig
EventBus EventBusConfig
Plugins PluginsConfig
}
Config holds all configurable options for the GoBetterAuth library.
type ConfigOption ¶
type ConfigOption func(*Config)
type CustomRoute ¶
type CustomRoute struct {
Method string
Path string
Middleware []CustomRouteMiddleware
Handler CustomRouteHandler
}
type CustomRouteHandler ¶
type DatabaseConfig ¶
type DatabaseHooksConfig ¶
type DatabaseHooksConfig struct {
Users *UserDatabaseHooksConfig
Accounts *AccountDatabaseHooksConfig
Sessions *SessionDatabaseHooksConfig
Verifications *VerificationDatabaseHooksConfig
}
type DefaultOAuth2ProvidersConfig ¶
type DefaultOAuth2ProvidersConfig struct {
Google *OAuth2Config
GitHub *OAuth2Config
Discord *OAuth2Config
}
type EmailPasswordConfig ¶
type EmailVerificationConfig ¶
type EndpointHookContext ¶
type EndpointHooksConfig ¶
type EndpointHooksConfig struct {
Before func(ctx *EndpointHookContext) error
Response func(ctx *EndpointHookContext) error
After func(ctx *EndpointHookContext) error
}
type Event ¶
type Event struct {
ID string `json:"id"`
Type string `json:"type"`
Timestamp time.Time `json:"timestamp"`
Payload json.RawMessage `json:"payload"`
Metadata map[string]string `json:"metadata"`
}
Event represents data to be published or received via the EventBus
type EventBus ¶
type EventBus interface {
EventPublisher
EventSubscriber
}
EventBus combines publisher and subscriber functionality
type EventBusConfig ¶
type EventHandler ¶
EventHandler processes events
type EventHooksConfig ¶
type EventPublisher ¶
EventPublisher defines the interface for publishing events
type EventSubscriber ¶
type EventSubscriber interface {
Subscribe(eventType string, handler EventHandler) (SubscriptionID, error)
Unsubscribe(eventType string, id SubscriptionID)
Close() error
}
EventSubscriber defines the interface for subscribing to events
type GenericOAuth2Config ¶
type GenericOAuth2Config struct {
OAuth2Config
Endpoint GenericOAuth2EndpointConfig
}
type KeyValueStore ¶
type KeyValueStore struct {
Key string `json:"key" gorm:"primaryKey"`
Value string `json:"value"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
KeyValueStore represents the persistent key-value store table in the database. This is a domain model used for secondary storage operations.
type Message ¶
type Message struct {
UUID string
Payload []byte // Message payload (serialized data)
Metadata map[string]string
}
Message represents a message in the pub/sub system.
type OAuth2Config ¶
type OAuth2UserInfo ¶
type PasswordConfig ¶
type Plugin ¶
type Plugin interface {
Metadata() PluginMetadata
SetMetadata(meta PluginMetadata)
Config() PluginConfig
SetConfig(cfg PluginConfig)
Ctx() *PluginContext
SetCtx(ctx *PluginContext)
Init(ctx *PluginContext) error
SetInit(fn func(ctx *PluginContext) error)
Migrations() []any
SetMigrations(migrations []any)
Routes() []PluginRoute
SetRoutes(routes []PluginRoute)
RateLimit() *PluginRateLimit
SetRateLimit(rateLimit *PluginRateLimit)
DatabaseHooks() *PluginDatabaseHooks
SetDatabaseHooks(hooks *PluginDatabaseHooks)
EventHooks() *PluginEventHooks
SetEventHooks(hooks *PluginEventHooks)
Close() error
SetClose(fn func() error)
}
type PluginConfig ¶
PluginConfig holds per-plugin configuration.
type PluginContext ¶
type PluginContext struct {
Config *Config
Api *Api
EventBus EventBus
Middleware *PluginMiddleware
}
type PluginDatabaseHookOperations ¶
type PluginDatabaseHookOperations[T any] struct { BeforeCreate *BeforeCreateHook[T] AfterCreate *AfterCreateHook[T] BeforeRead *BeforeReadHook[T] AfterRead *AfterReadHook[T] BeforeUpdate *BeforeUpdateHook[T] AfterUpdate *AfterUpdateHook[T] BeforeDelete *BeforeDeleteHook[T] AfterDelete *AfterDeleteHook[T] }
type PluginDatabaseHooks ¶
type PluginDatabaseHooks map[string]PluginDatabaseHookOperations[any]
type PluginEventHookFunc ¶
type PluginEventHookFunc func(ctx *PluginContext, payload PluginEventHookPayload) error
type PluginEventHookPayload ¶
type PluginEventHookPayload any
type PluginEventHooks ¶
type PluginEventHooks map[string]PluginEventHookFunc
type PluginMetadata ¶
type PluginMiddleware ¶
type PluginMiddleware struct {
Auth func() func(http.Handler) http.Handler
OptionalAuth func() func(http.Handler) http.Handler
CorsAuth func() func(http.Handler) http.Handler
CSRF func() func(http.Handler) http.Handler
RateLimit func() func(http.Handler) http.Handler
EndpointHooks func() func(http.Handler) http.Handler
}
type PluginOption ¶ added in v1.3.4
type PluginOption func(p Plugin)
type PluginRateLimit ¶
type PluginRateLimit = RateLimitConfig
type PluginRoute ¶
type PluginRoute struct {
Method string
Path string // Relative path, /auth is auto-prefixed
Middleware []PluginRouteMiddleware
Handler PluginRouteHandler
}
type PluginRouteHandler ¶
type PluginsConfig ¶
type PluginsConfig struct {
Plugins []Plugin
}
type ProviderType ¶
type ProviderType string
const ( ProviderEmail ProviderType = "email" ProviderDiscord ProviderType = "discord" ProviderGitHub ProviderType = "github" ProviderGoogle ProviderType = "google" )
type PubSub ¶
type PubSub interface {
// Publish sends a message to the specified topic
Publish(ctx context.Context, topic string, msg *Message) error
// Subscribe returns a channel that receives messages from the specified topic.
// The channel should be closed when the subscription is cancelled or closed.
Subscribe(ctx context.Context, topic string) (<-chan *Message, error)
// Close closes the pub/sub and cleans up resources
Close() error
}
PubSub is a generic publish-subscribe interface.
type RateLimitConfig ¶
type RateLimitCustomRule ¶
type RateLimitCustomRuleFunc ¶
type RateLimitCustomRuleFunc func(req *http.Request) RateLimitCustomRule
type RateLimitService ¶
type SecondaryStorage ¶
type SecondaryStorage interface {
Get(ctx context.Context, key string) (any, error)
Set(ctx context.Context, key string, value any, ttl *time.Duration) error
Delete(ctx context.Context, key string) error
Incr(ctx context.Context, key string, ttl *time.Duration) (int, error)
}
SecondaryStorage defines an interface for secondary storage operations.
type SecondaryStorageConfig ¶
type SecondaryStorageConfig struct {
Type SecondaryStorageType
MemoryOptions *SecondaryStorageMemoryOptions
DatabaseOptions *SecondaryStorageDatabaseOptions
Storage SecondaryStorage
}
type SecondaryStorageDatabaseOptions ¶
type SecondaryStorageDatabaseOptions struct {
// CleanupInterval controls how often expired entries are cleaned up.
// If zero, the implementation should use a default.
CleanupInterval time.Duration
}
SecondaryStorageDatabaseOptions holds settings specific to the database storage.
type SecondaryStorageMemoryOptions ¶
type SecondaryStorageMemoryOptions struct {
// CleanupInterval controls how often expired entries are cleaned up.
// If zero, the implementation should use a default.
CleanupInterval time.Duration
}
SecondaryStorageMemoryOptions holds settings specific to the in-memory storage.
type SecondaryStorageType ¶
type SecondaryStorageType string
const ( SecondaryStorageTypeMemory SecondaryStorageType = "memory" SecondaryStorageTypeDatabase SecondaryStorageType = "database" SecondaryStorageTypeCustom SecondaryStorageType = "custom" )
type Session ¶
type Session struct {
ID string `json:"id" gorm:"primaryKey"`
UserID string `json:"user_id" gorm:"index"`
Token string `json:"token" gorm:"uniqueIndex"`
ExpiresAt time.Time `json:"expires_at"`
IPAddress *string `json:"ip_address,omitempty"`
UserAgent *string `json:"user_agent,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type SessionConfig ¶
type SessionService ¶
type SocialProvidersConfig ¶
type SocialProvidersConfig struct {
Default DefaultOAuth2ProvidersConfig
Generic map[string]GenericOAuth2Config
}
type SubscriptionID ¶ added in v1.3.1
type SubscriptionID uint64
SubscriptionID identifies a specific event handler subscription for removal
type TokenService ¶
type TrustedOriginsConfig ¶
type TrustedOriginsConfig struct {
Origins []string
}
type TypedPluginEventHook ¶
type TypedPluginEventHook[T any] func(ctx *PluginContext, payload T) error
type UserConfig ¶
type UserConfig struct {
ChangeEmail ChangeEmailConfig
}
type UserDatabaseHooksConfig ¶
type UserService ¶
type Verification ¶
type Verification struct {
ID string `json:"id" gorm:"primaryKey"`
UserID *string `json:"user_id,omitempty"`
Identifier string `json:"identifier"` // email or other identifier
Token string `json:"token"`
Type VerificationType `json:"type"`
ExpiresAt time.Time `json:"expires_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type VerificationDatabaseHooksConfig ¶
type VerificationDatabaseHooksConfig struct {
BeforeCreate func(verification *Verification) error
AfterCreate func(verification Verification) error
}
type VerificationService ¶
type VerificationService interface {
CreateVerification(verif *Verification) error
GetVerificationByToken(token string) (*Verification, error)
DeleteVerification(id string) error
IsExpired(verif *Verification) bool
}
type VerificationType ¶
type VerificationType string
const ( TypeEmailVerification VerificationType = "email_verification" TypePasswordReset VerificationType = "password_reset" TypeEmailChange VerificationType = "email_change" )