Documentation
¶
Index ¶
- Constants
- Variables
- func APIKeyMiddleware(store APIKeyStore, requiredScopes []string) func(http.Handler) http.Handler
- func CSRFMiddleware(cfg CSRFConfig) func(http.Handler) http.Handler
- func GenerateOpenAPISpec(info OpenAPIInfo) map[string]any
- func ServeAdminUI() http.Handler
- func ServeAuthJS() http.Handler
- func ServeAuthUI() http.Handler
- func ServeSSE(hub *SseHub, channel string) http.HandlerFunc
- func VerifyWebhookSignature(secret string, body []byte, sigHeader string) error
- type APIKeyRecord
- type APIKeyService
- type APIKeyStore
- type Auth
- func (a *Auth) DeleteAccount(ctx context.Context, in DeleteAccountInput) error
- func (a *Auth) Login(ctx context.Context, in LoginInput) (User, AuthTokens, error)
- func (a *Auth) Logout(ctx context.Context, refreshToken string) error
- func (a *Auth) Me(ctx context.Context, accessToken string) (User, error)
- func (a *Auth) Refresh(ctx context.Context, refreshToken string) (AuthTokens, error)
- func (a *Auth) Register(ctx context.Context, in RegisterInput) (User, AuthTokens, error)
- func (a *Auth) Service() *Service
- func (a *Auth) UpdateProfile(ctx context.Context, in UpdateProfileInput) (User, error)
- type AuthTokens
- type CSRFConfig
- type ChangeEmailRequestInput
- type ChangePasswordInput
- type Config
- type ConfirmEmailChangeInput
- type DeleteAccountInput
- type EmailChangeStore
- type EmailVerificationInput
- type EmailVerificationStore
- type Event
- type EventBus
- type ForgotPasswordInput
- type HTTPMailerTransport
- type IDP
- type IDPClient
- type IDPConfig
- type LinkedAccountStore
- type LoginInput
- type MCPServer
- type MagicLinkSendInput
- type MagicLinkStore
- type MagicLinkVerifyInput
- type MailMessage
- type MailTemplateData
- type MailTemplater
- type MailerTransport
- type MemoryLinkedAccounts
- func (m *MemoryLinkedAccounts) Delete(_ context.Context, id string) error
- func (m *MemoryLinkedAccounts) FindByProvider(_ context.Context, provider, providerID string) (OAuthLinkedAccount, error)
- func (m *MemoryLinkedAccounts) ListForUser(_ context.Context, userID string) ([]OAuthLinkedAccount, error)
- func (m *MemoryLinkedAccounts) Save(_ context.Context, link OAuthLinkedAccount) error
- type MemoryMetadataStore
- type MemoryRolesPermissionsStore
- func (s *MemoryRolesPermissionsStore) AddPermissionToRole(_ context.Context, role, permission string) error
- func (s *MemoryRolesPermissionsStore) AddRoleToUser(_ context.Context, userID, role string, tenantID string) error
- func (s *MemoryRolesPermissionsStore) CreateRole(_ context.Context, role string, permissions []string) error
- func (s *MemoryRolesPermissionsStore) DeleteRole(_ context.Context, role string) error
- func (s *MemoryRolesPermissionsStore) GetPermissionsForRole(_ context.Context, role string) ([]string, error)
- func (s *MemoryRolesPermissionsStore) GetPermissionsForUser(ctx context.Context, userID, tenantID string) ([]string, error)
- func (s *MemoryRolesPermissionsStore) GetRolesForUser(_ context.Context, userID, tenantID string) ([]string, error)
- func (s *MemoryRolesPermissionsStore) RemovePermissionFromRole(_ context.Context, role, permission string) error
- func (s *MemoryRolesPermissionsStore) RemoveRoleFromUser(_ context.Context, userID, role string, tenantID string) error
- func (s *MemoryRolesPermissionsStore) UserHasPermission(ctx context.Context, userID, permission, tenantID string) (bool, error)
- type MemorySessionStore
- func (s *MemorySessionStore) CreateSession(_ context.Context, session Session) (Session, error)
- func (s *MemorySessionStore) DeleteExpiredSessions(_ context.Context, now time.Time) (int, error)
- func (s *MemorySessionStore) GetSessionByID(_ context.Context, sessionID string) (Session, error)
- func (s *MemorySessionStore) GetSessionByRefreshTokenHash(_ context.Context, tokenHash string) (Session, error)
- func (s *MemorySessionStore) ListSessionsForUser(_ context.Context, userID, tenantID string) ([]Session, error)
- func (s *MemorySessionStore) RevokeSessionByID(_ context.Context, sessionID string) error
- func (s *MemorySessionStore) UpdateSession(_ context.Context, session Session) error
- type MemoryTelemetryStore
- type MemoryTenantStore
- func (s *MemoryTenantStore) AssociateUserWithTenant(_ context.Context, userID, tenantID string) error
- func (s *MemoryTenantStore) CreateTenant(_ context.Context, tenant Tenant) (Tenant, error)
- func (s *MemoryTenantStore) DeleteTenant(_ context.Context, id string) error
- func (s *MemoryTenantStore) DisassociateUserFromTenant(_ context.Context, userID, tenantID string) error
- func (s *MemoryTenantStore) GetAllTenants(_ context.Context) ([]Tenant, error)
- func (s *MemoryTenantStore) GetTenantByID(_ context.Context, id string) (Tenant, error)
- func (s *MemoryTenantStore) GetTenantsForUser(_ context.Context, userID string) ([]Tenant, error)
- func (s *MemoryTenantStore) GetUsersForTenant(_ context.Context, tenantID string) ([]string, error)
- func (s *MemoryTenantStore) UpdateTenant(_ context.Context, id string, update Tenant) error
- type MemoryUserStore
- func (s *MemoryUserStore) ApplyEmailChange(_ context.Context, userID, tenantID string) error
- func (s *MemoryUserStore) ClearEmailChangeToken(_ context.Context, userID, tenantID string) error
- func (s *MemoryUserStore) ClearEmailVerificationToken(_ context.Context, userID, tenantID string) error
- func (s *MemoryUserStore) ClearMagicLinkToken(_ context.Context, userID, tenantID string) error
- func (s *MemoryUserStore) ClearResetToken(_ context.Context, userID, tenantID string) error
- func (s *MemoryUserStore) ClearSMSCode(_ context.Context, userID, tenantID string) error
- func (s *MemoryUserStore) CreateUser(_ context.Context, user User) (User, error)
- func (s *MemoryUserStore) DeleteUser(_ context.Context, userID, tenantID string) error
- func (s *MemoryUserStore) GetUserByEmail(_ context.Context, email, tenantID string) (User, error)
- func (s *MemoryUserStore) GetUserByEmailChangeTokenHash(_ context.Context, tokenHash string) (User, error)
- func (s *MemoryUserStore) GetUserByEmailVerificationTokenHash(_ context.Context, tokenHash string) (User, error)
- func (s *MemoryUserStore) GetUserByID(_ context.Context, id, tenantID string) (User, error)
- func (s *MemoryUserStore) GetUserByMagicLinkTokenHash(_ context.Context, tokenHash string) (User, error)
- func (s *MemoryUserStore) GetUserByResetTokenHash(_ context.Context, tokenHash string) (User, error)
- func (s *MemoryUserStore) GetUserBySMSCodeHash(_ context.Context, userID, tenantID, codeHash string) (User, error)
- func (s *MemoryUserStore) MarkEmailVerified(_ context.Context, userID, tenantID string, verified bool) error
- func (s *MemoryUserStore) UpdateEmailChangeToken(_ context.Context, userID, tenantID, pendingEmail, tokenHash string, ...) error
- func (s *MemoryUserStore) UpdateEmailVerificationToken(_ context.Context, userID, tenantID, tokenHash string, expiry time.Time) error
- func (s *MemoryUserStore) UpdateMagicLinkToken(_ context.Context, userID, tenantID, tokenHash string, expiry time.Time) error
- func (s *MemoryUserStore) UpdatePassword(_ context.Context, userID, tenantID, passwordHash string) error
- func (s *MemoryUserStore) UpdateProfile(_ context.Context, userID, tenantID, firstName, lastName string) (User, error)
- func (s *MemoryUserStore) UpdateResetToken(_ context.Context, userID, tenantID, tokenHash string, expiry time.Time) error
- func (s *MemoryUserStore) UpdateSMSCode(_ context.Context, userID, tenantID, codeHash string, expiry time.Time) error
- func (s *MemoryUserStore) UpdateTOTPSecret(_ context.Context, userID, tenantID, secret string, enabled bool) error
- type OAuthLinkedAccount
- type OAuthPendingMeta
- type OAuthProvider
- type OAuthService
- func (s *OAuthService) AuthorizeURL(providerName, state string) (string, error)
- func (s *OAuthService) ExchangeCode(ctx context.Context, providerName, code string) (OAuthUserInfo, error)
- func (s *OAuthService) HandleCallback(ctx context.Context, authSvc *Service, linkedAccounts LinkedAccountStore, ...) (User, AuthTokens, error)
- type OAuthUserInfo
- type OpenAPIInfo
- type Option
- func WithIssuer(issuer string) Option
- func WithLogger(fn func(format string, args ...any)) Option
- func WithMetadataProvider(store UserMetadataStore) Option
- func WithRBACProvider(store RolesPermissionsStore) Option
- func WithRequire2FA(enabled bool) Option
- func WithSecret(secret string) Option
- func WithSessionStore(store SessionStore) Option
- func WithTenantProvider(store TenantStore) Option
- func WithTokenClaimsBuilder(fn func(ctx context.Context, user User) (map[string]any, error)) Option
- func WithTokenTTLs(access, refresh time.Duration) Option
- func WithUserStore(store UserStore) Option
- type PendingLinkStore
- type PublicTenant
- type PublicUser
- type RegisterInput
- type ResetPasswordInput
- type RolesPermissionsStore
- type SMSCodeSendInput
- type SMSCodeVerifyInput
- type SMSStore
- type Service
- func (s *Service) AddUserToTenant(ctx context.Context, userID, tenantID string) error
- func (s *Service) AssignRole(ctx context.Context, userID, role, tenantID string) error
- func (s *Service) ChangePassword(ctx context.Context, in ChangePasswordInput) error
- func (s *Service) CleanupExpiredSessions(ctx context.Context) (int, error)
- func (s *Service) ConfirmEmailChange(ctx context.Context, in ConfirmEmailChangeInput) error
- func (s *Service) CreateRole(ctx context.Context, role string, permissions []string) error
- func (s *Service) CreateTenant(ctx context.Context, name string, config map[string]any) (Tenant, error)
- func (s *Service) DeleteAccount(ctx context.Context, in DeleteAccountInput) error
- func (s *Service) DisableTOTP(ctx context.Context, userID, tenantID string) error
- func (s *Service) ForgotPassword(ctx context.Context, in ForgotPasswordInput) (string, error)
- func (s *Service) GetMetadata(ctx context.Context, userID string) (map[string]any, error)
- func (s *Service) ListSessions(ctx context.Context, userID, tenantID string) ([]Session, error)
- func (s *Service) Login(ctx context.Context, in LoginInput) (User, AuthTokens, error)
- func (s *Service) Logout(ctx context.Context, refreshToken string) error
- func (s *Service) Me(ctx context.Context, accessToken string) (User, error)
- func (s *Service) Refresh(ctx context.Context, refreshToken string) (AuthTokens, error)
- func (s *Service) Register(ctx context.Context, in RegisterInput) (User, AuthTokens, error)
- func (s *Service) RequestEmailChange(ctx context.Context, in ChangeEmailRequestInput) (string, error)
- func (s *Service) ResetPassword(ctx context.Context, in ResetPasswordInput) error
- func (s *Service) RevokeSessionByID(ctx context.Context, sessionID string) error
- func (s *Service) SendMagicLink(ctx context.Context, in MagicLinkSendInput) (string, error)
- func (s *Service) SendSMSCode(ctx context.Context, in SMSCodeSendInput) (string, error)
- func (s *Service) SendVerificationEmailToken(ctx context.Context, in EmailVerificationInput) (string, error)
- func (s *Service) SetupTOTP(ctx context.Context, userID, tenantID string) (string, error)
- func (s *Service) UpdateMetadata(ctx context.Context, userID string, metadata map[string]any) error
- func (s *Service) UpdateProfile(ctx context.Context, in UpdateProfileInput) (User, error)
- func (s *Service) UserHasPermission(ctx context.Context, userID, permission, tenantID string) (bool, error)
- func (s *Service) VerifyEmail(ctx context.Context, in VerifyEmailInput) error
- func (s *Service) VerifyMagicLink(ctx context.Context, in MagicLinkVerifyInput) (User, AuthTokens, error)
- func (s *Service) VerifySMSCode(ctx context.Context, in SMSCodeVerifyInput) (User, AuthTokens, error)
- func (s *Service) VerifyTOTP(ctx context.Context, userID, tenantID, code string) (User, AuthTokens, error)
- func (s *Service) VerifyTOTPSetup(ctx context.Context, userID, tenantID, secret, code string) error
- type ServiceOption
- type Session
- type SessionAdminStore
- type SessionLookupStore
- type SessionStore
- type SseDistributor
- type SseHub
- type SseMessage
- type TOTPStore
- type TelemetryEvent
- type TelemetryFilter
- type TelemetryStore
- type Tenant
- type TenantStore
- type UpdateProfileInput
- type User
- type UserAccountStore
- type UserMetadataStore
- type UserPasswordStore
- type UserStore
- type VerifyEmailInput
- type WebhookDispatcher
- type WebhookEndpoint
- type WebhookPayload
Constants ¶
const ( SessionCheckOnAllCalls = "allcalls" SessionCheckOnRefresh = "refresh" SessionCheckOnNone = "none" )
const ( EmailVerificationModeNone = "none" EmailVerificationModeLazy = "lazy" EmailVerificationModeStrict = "strict" )
Email verification modes accepted by Config.EmailVerificationMode.
none : Register marks the address verified, so verification never comes up
on the self-registration path. An address left unverified by some
other route (admin provisioning, a data import, a custom UserStore
whose column defaults to false) is still refused at login. This is
the default and is exactly how the service behaved before the mode
existed.
lazy : Register leaves the address unverified and login is allowed anyway.
This is the only mode in which an unverified address may log in.
strict : Register leaves the address unverified and login is refused until
the address is confirmed.
Variables ¶
var ( ErrInvalidCredentials = errors.New("auth: invalid credentials") ErrUserExists = errors.New("auth: user already exists") ErrInvalidToken = errors.New("auth: invalid token") ErrSessionNotFound = errors.New("auth: session not found") ErrSessionRevoked = errors.New("auth: session revoked") ErrWeakPassword = errors.New("auth: password is too weak") ErrFeatureNotSupported = errors.New("auth: feature not supported by configured stores") ErrEmailNotVerified = errors.New("auth: email not verified") ErrInvalidCode = errors.New("auth: invalid code") ErrTwoFactorRequired = errors.New("auth: two-factor authentication required") ErrAlreadyExists = errors.New("auth: resource already exists") ErrTenantNotFound = errors.New("auth: tenant not found") ErrRoleNotFound = errors.New("auth: role not found") )
Functions ¶
func APIKeyMiddleware ¶
func CSRFMiddleware ¶
func CSRFMiddleware(cfg CSRFConfig) func(http.Handler) http.Handler
CSRFMiddleware validates mutating browser requests using the double-submit pattern.
func GenerateOpenAPISpec ¶
func GenerateOpenAPISpec(info OpenAPIInfo) map[string]any
GenerateOpenAPISpec returns an OpenAPI 3.0 spec for awesome-go-auth endpoints.
func ServeAdminUI ¶
ServeAdminUI returns an http.Handler serving the embedded admin dashboard HTML.
func ServeAuthJS ¶
ServeAuthJS returns an http.Handler serving the embedded auth.js browser SDK.
func ServeAuthUI ¶
ServeAuthUI returns an http.Handler serving the embedded auth UI HTML (login/register).
Types ¶
type APIKeyRecord ¶
type APIKeyRecord struct {
ID string
Prefix string
Name string
ServiceID string
KeyHash string
Scopes []string
AllowedIPs []string
IsActive bool
ExpiresAt *time.Time
LastUsedAt *time.Time
}
APIKeyRecord stores hashed key material and policy constraints.
type APIKeyService ¶
type APIKeyService struct{}
func NewAPIKeyService ¶
func NewAPIKeyService() *APIKeyService
func (*APIKeyService) Create ¶
func (s *APIKeyService) Create(ctx context.Context, store APIKeyStore, name, serviceID string, scopes, allowedIPs []string, expiresAt *time.Time) (rawKey string, record APIKeyRecord, err error)
func (*APIKeyService) Verify ¶
func (s *APIKeyService) Verify(ctx context.Context, store APIKeyStore, rawKey string, ip string, requiredScopes []string) (APIKeyRecord, error)
type APIKeyStore ¶
type APIKeyStore interface {
Save(ctx context.Context, key APIKeyRecord) error
FindByPrefix(ctx context.Context, prefix string) (APIKeyRecord, error)
Revoke(ctx context.Context, id string) error
UpdateLastUsed(ctx context.Context, id string, when time.Time) error
}
APIKeyStore persists and resolves API keys.
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Auth is the top-level entrypoint configured with functional options.
func (*Auth) DeleteAccount ¶
func (a *Auth) DeleteAccount(ctx context.Context, in DeleteAccountInput) error
DeleteAccount delegates to Service.DeleteAccount.
func (*Auth) Login ¶
func (a *Auth) Login(ctx context.Context, in LoginInput) (User, AuthTokens, error)
Login delegates to Service.Login.
func (*Auth) Register ¶
func (a *Auth) Register(ctx context.Context, in RegisterInput) (User, AuthTokens, error)
Register delegates to Service.Register.
func (*Auth) UpdateProfile ¶
UpdateProfile delegates to Service.UpdateProfile.
type AuthTokens ¶
AuthTokens are the issued access/refresh pair.
type CSRFConfig ¶
type CSRFConfig struct {
APIPrefix string
CookieName string
HeaderName string
CookieSecure bool
CookieSameSite http.SameSite
}
CSRFConfig configures cookie+header double-submit CSRF protection.
func DefaultCSRFConfig ¶
func DefaultCSRFConfig(apiPrefix string) CSRFConfig
DefaultCSRFConfig returns defaults compatible with browser cookie auth flows.
type ChangeEmailRequestInput ¶
type ChangePasswordInput ¶
type Config ¶
type Config struct {
Secret string
Issuer string
AccessTokenTTL time.Duration
RefreshTokenTTL time.Duration
SessionCheckOn string
ClockSkew time.Duration
MinPasswordLen int
ResetTokenTTL time.Duration
MagicLinkTTL time.Duration
SMSCodeTTL time.Duration
EmailVerificationTTL time.Duration
EmailVerificationMode string
EmailChangeTTL time.Duration
TempTokenTTL time.Duration
Require2FA bool
BuildTokenClaims func(ctx context.Context, user User) (map[string]any, error)
Logger func(format string, args ...any)
}
Config contains security and token settings for the auth service.
func DefaultConfig ¶
DefaultConfig returns secure defaults for development and production bootstrap.
type ConfirmEmailChangeInput ¶
type ConfirmEmailChangeInput struct {
Token string
}
type DeleteAccountInput ¶
type EmailChangeStore ¶
type EmailChangeStore interface {
UpdateEmailChangeToken(ctx context.Context, userID, tenantID, pendingEmail, tokenHash string, expiry time.Time) error
GetUserByEmailChangeTokenHash(ctx context.Context, tokenHash string) (User, error)
ApplyEmailChange(ctx context.Context, userID, tenantID string) error
ClearEmailChangeToken(ctx context.Context, userID, tenantID string) error
}
EmailChangeStore persists pending email change flow.
type EmailVerificationInput ¶
type EmailVerificationStore ¶
type EmailVerificationStore interface {
UpdateEmailVerificationToken(ctx context.Context, userID, tenantID, tokenHash string, expiry time.Time) error
GetUserByEmailVerificationTokenHash(ctx context.Context, tokenHash string) (User, error)
MarkEmailVerified(ctx context.Context, userID, tenantID string, verified bool) error
ClearEmailVerificationToken(ctx context.Context, userID, tenantID string) error
}
EmailVerificationStore persists verification flow.
type Event ¶
type Event struct {
Name string
UserID string
TenantID string
Timestamp time.Time
Data map[string]any
}
Event is a generic auth domain event payload.
type EventBus ¶
type EventBus struct {
// contains filtered or unexported fields
}
EventBus is a tiny in-process pub/sub for auth events.
func NewEventBus ¶
func NewEventBus() *EventBus
type ForgotPasswordInput ¶
type HTTPMailerTransport ¶
type HTTPMailerTransport struct {
EndpointURL string
Secret string
// contains filtered or unexported fields
}
HTTPMailerTransport sends mail via any HTTP endpoint (no SMTP needed).
func NewHTTPMailerTransport ¶
func NewHTTPMailerTransport(endpointURL, secret string) *HTTPMailerTransport
NewHTTPMailerTransport creates a transport that POSTs to a webhook-style HTTP endpoint.
func (*HTTPMailerTransport) Send ¶
func (t *HTTPMailerTransport) Send(ctx context.Context, msg MailMessage) error
type IDP ¶
type IDP struct {
// contains filtered or unexported fields
}
IDP is an OIDC Identity Provider.
type LinkedAccountStore ¶
type LinkedAccountStore interface {
Save(ctx context.Context, link OAuthLinkedAccount) error
FindByProvider(ctx context.Context, provider, providerID string) (OAuthLinkedAccount, error)
ListForUser(ctx context.Context, userID string) ([]OAuthLinkedAccount, error)
Delete(ctx context.Context, id string) error
}
LinkedAccountStore persists OAuth provider associations per user.
type LoginInput ¶
LoginInput is the payload used for user authentication.
type MCPServer ¶
type MCPServer struct {
// contains filtered or unexported fields
}
MCPServer exposes auth configuration as MCP tools for AI editors. It speaks JSON-RPC 2.0 over HTTP and implements the Model Context Protocol tools/list and tools/call methods.
func NewMCPServer ¶
NewMCPServer creates an MCP server wrapping an auth service.
type MagicLinkSendInput ¶
type MagicLinkStore ¶
type MagicLinkStore interface {
UpdateMagicLinkToken(ctx context.Context, userID, tenantID, tokenHash string, expiry time.Time) error
GetUserByMagicLinkTokenHash(ctx context.Context, tokenHash string) (User, error)
ClearMagicLinkToken(ctx context.Context, userID, tenantID string) error
}
MagicLinkStore persists magic link state.
type MagicLinkVerifyInput ¶
type MagicLinkVerifyInput struct {
Token string
}
type MailMessage ¶
MailMessage is a single email to be sent.
type MailTemplateData ¶
type MailTemplateData struct {
AppName string
UserName string
Token string
URL string
Code string
Extra map[string]any
}
MailTemplateData holds the common variables injected into mail templates.
type MailTemplater ¶
type MailTemplater struct {
// contains filtered or unexported fields
}
MailTemplater renders localized mail templates.
func NewMailTemplater ¶
func NewMailTemplater(appName string) *MailTemplater
NewMailTemplater creates a templater with built-in en and it templates.
func (*MailTemplater) Render ¶
func (m *MailTemplater) Render(locale, name string, data MailTemplateData) (subject, body string, err error)
Render produces the subject and HTML body for the given locale and template name.
type MailerTransport ¶
type MailerTransport interface {
Send(ctx context.Context, msg MailMessage) error
}
MailerTransport delivers email messages.
type MemoryLinkedAccounts ¶
type MemoryLinkedAccounts struct {
// contains filtered or unexported fields
}
MemoryLinkedAccounts is an in-memory implementation of LinkedAccountStore, suitable for development, testing, and embedded deployments.
func NewMemoryLinkedAccounts ¶
func NewMemoryLinkedAccounts() *MemoryLinkedAccounts
NewMemoryLinkedAccounts creates an in-memory LinkedAccountStore.
func (*MemoryLinkedAccounts) Delete ¶
func (m *MemoryLinkedAccounts) Delete(_ context.Context, id string) error
func (*MemoryLinkedAccounts) FindByProvider ¶
func (m *MemoryLinkedAccounts) FindByProvider(_ context.Context, provider, providerID string) (OAuthLinkedAccount, error)
func (*MemoryLinkedAccounts) ListForUser ¶
func (m *MemoryLinkedAccounts) ListForUser(_ context.Context, userID string) ([]OAuthLinkedAccount, error)
func (*MemoryLinkedAccounts) Save ¶
func (m *MemoryLinkedAccounts) Save(_ context.Context, link OAuthLinkedAccount) error
type MemoryMetadataStore ¶
type MemoryMetadataStore struct {
// contains filtered or unexported fields
}
MemoryMetadataStore is an in-memory implementation of UserMetadataStore.
func NewMemoryMetadataStore ¶
func NewMemoryMetadataStore() *MemoryMetadataStore
func (*MemoryMetadataStore) ClearMetadata ¶
func (s *MemoryMetadataStore) ClearMetadata(_ context.Context, userID string) error
func (*MemoryMetadataStore) GetMetadata ¶
func (*MemoryMetadataStore) UpdateMetadata ¶
type MemoryRolesPermissionsStore ¶
type MemoryRolesPermissionsStore struct {
// contains filtered or unexported fields
}
MemoryRolesPermissionsStore is an in-memory RBAC store.
func NewMemoryRolesPermissionsStore ¶
func NewMemoryRolesPermissionsStore() *MemoryRolesPermissionsStore
func (*MemoryRolesPermissionsStore) AddPermissionToRole ¶
func (s *MemoryRolesPermissionsStore) AddPermissionToRole(_ context.Context, role, permission string) error
func (*MemoryRolesPermissionsStore) AddRoleToUser ¶
func (*MemoryRolesPermissionsStore) CreateRole ¶
func (*MemoryRolesPermissionsStore) DeleteRole ¶
func (s *MemoryRolesPermissionsStore) DeleteRole(_ context.Context, role string) error
func (*MemoryRolesPermissionsStore) GetPermissionsForRole ¶
func (*MemoryRolesPermissionsStore) GetPermissionsForUser ¶
func (*MemoryRolesPermissionsStore) GetRolesForUser ¶
func (*MemoryRolesPermissionsStore) RemovePermissionFromRole ¶
func (s *MemoryRolesPermissionsStore) RemovePermissionFromRole(_ context.Context, role, permission string) error
func (*MemoryRolesPermissionsStore) RemoveRoleFromUser ¶
func (*MemoryRolesPermissionsStore) UserHasPermission ¶
type MemorySessionStore ¶
type MemorySessionStore struct {
// contains filtered or unexported fields
}
MemorySessionStore is a thread-safe in-memory implementation of SessionStore.
func NewMemorySessionStore ¶
func NewMemorySessionStore() *MemorySessionStore
func (*MemorySessionStore) CreateSession ¶
func (*MemorySessionStore) DeleteExpiredSessions ¶
func (*MemorySessionStore) GetSessionByID ¶
func (*MemorySessionStore) GetSessionByRefreshTokenHash ¶
func (*MemorySessionStore) ListSessionsForUser ¶
func (*MemorySessionStore) RevokeSessionByID ¶
func (s *MemorySessionStore) RevokeSessionByID(_ context.Context, sessionID string) error
func (*MemorySessionStore) UpdateSession ¶
func (s *MemorySessionStore) UpdateSession(_ context.Context, session Session) error
type MemoryTelemetryStore ¶
type MemoryTelemetryStore struct {
// contains filtered or unexported fields
}
MemoryTelemetryStore is an in-memory implementation of TelemetryStore.
func NewMemoryTelemetryStore ¶
func NewMemoryTelemetryStore() *MemoryTelemetryStore
NewMemoryTelemetryStore creates an in-memory telemetry store.
func (*MemoryTelemetryStore) Query ¶
func (m *MemoryTelemetryStore) Query(_ context.Context, f TelemetryFilter) ([]TelemetryEvent, error)
func (*MemoryTelemetryStore) Record ¶
func (m *MemoryTelemetryStore) Record(_ context.Context, event TelemetryEvent) error
type MemoryTenantStore ¶
type MemoryTenantStore struct {
// contains filtered or unexported fields
}
MemoryTenantStore is an in-memory tenant/membership store.
func NewMemoryTenantStore ¶
func NewMemoryTenantStore() *MemoryTenantStore
func (*MemoryTenantStore) AssociateUserWithTenant ¶
func (s *MemoryTenantStore) AssociateUserWithTenant(_ context.Context, userID, tenantID string) error
func (*MemoryTenantStore) CreateTenant ¶
func (*MemoryTenantStore) DeleteTenant ¶
func (s *MemoryTenantStore) DeleteTenant(_ context.Context, id string) error
func (*MemoryTenantStore) DisassociateUserFromTenant ¶
func (s *MemoryTenantStore) DisassociateUserFromTenant(_ context.Context, userID, tenantID string) error
func (*MemoryTenantStore) GetAllTenants ¶
func (s *MemoryTenantStore) GetAllTenants(_ context.Context) ([]Tenant, error)
func (*MemoryTenantStore) GetTenantByID ¶
func (*MemoryTenantStore) GetTenantsForUser ¶
func (*MemoryTenantStore) GetUsersForTenant ¶
func (*MemoryTenantStore) UpdateTenant ¶
type MemoryUserStore ¶
type MemoryUserStore struct {
// contains filtered or unexported fields
}
MemoryUserStore is a thread-safe in-memory implementation of UserStore.
func NewMemoryUserStore ¶
func NewMemoryUserStore() *MemoryUserStore
func (*MemoryUserStore) ApplyEmailChange ¶
func (s *MemoryUserStore) ApplyEmailChange(_ context.Context, userID, tenantID string) error
func (*MemoryUserStore) ClearEmailChangeToken ¶
func (s *MemoryUserStore) ClearEmailChangeToken(_ context.Context, userID, tenantID string) error
func (*MemoryUserStore) ClearEmailVerificationToken ¶
func (s *MemoryUserStore) ClearEmailVerificationToken(_ context.Context, userID, tenantID string) error
func (*MemoryUserStore) ClearMagicLinkToken ¶
func (s *MemoryUserStore) ClearMagicLinkToken(_ context.Context, userID, tenantID string) error
func (*MemoryUserStore) ClearResetToken ¶
func (s *MemoryUserStore) ClearResetToken(_ context.Context, userID, tenantID string) error
func (*MemoryUserStore) ClearSMSCode ¶
func (s *MemoryUserStore) ClearSMSCode(_ context.Context, userID, tenantID string) error
func (*MemoryUserStore) CreateUser ¶
func (*MemoryUserStore) DeleteUser ¶
func (s *MemoryUserStore) DeleteUser(_ context.Context, userID, tenantID string) error
func (*MemoryUserStore) GetUserByEmail ¶
func (*MemoryUserStore) GetUserByEmailChangeTokenHash ¶
func (*MemoryUserStore) GetUserByEmailVerificationTokenHash ¶
func (*MemoryUserStore) GetUserByID ¶
func (*MemoryUserStore) GetUserByMagicLinkTokenHash ¶
func (*MemoryUserStore) GetUserByResetTokenHash ¶
func (*MemoryUserStore) GetUserBySMSCodeHash ¶
func (*MemoryUserStore) MarkEmailVerified ¶
func (*MemoryUserStore) UpdateEmailChangeToken ¶
func (*MemoryUserStore) UpdateEmailVerificationToken ¶
func (*MemoryUserStore) UpdateMagicLinkToken ¶
func (*MemoryUserStore) UpdatePassword ¶
func (s *MemoryUserStore) UpdatePassword(_ context.Context, userID, tenantID, passwordHash string) error
func (*MemoryUserStore) UpdateProfile ¶
func (*MemoryUserStore) UpdateResetToken ¶
func (*MemoryUserStore) UpdateSMSCode ¶
func (*MemoryUserStore) UpdateTOTPSecret ¶
type OAuthLinkedAccount ¶
type OAuthLinkedAccount struct {
ID string
UserID string
Provider string
ProviderID string
CreatedAt time.Time
}
OAuthLinkedAccount stores a linked provider<->user association.
type OAuthPendingMeta ¶
type OAuthPendingMeta struct {
Provider string
RedirectURL string
TenantID string
UserID string // non-empty when linking to existing account
}
OAuthPendingMeta is the metadata stored during OAuth flow.
type OAuthProvider ¶
type OAuthProvider struct {
Name string
ClientID string
ClientSecret string
RedirectURL string
AuthURL string
TokenURL string
UserInfoURL string
Scopes []string
}
OAuthProvider holds configuration for a single OAuth 2.0 provider.
func GitHubProvider ¶
func GitHubProvider(clientID, clientSecret, redirectURL string) OAuthProvider
GitHubProvider builds a pre-configured GitHub OAuth2 provider.
func GoogleProvider ¶
func GoogleProvider(clientID, clientSecret, redirectURL string) OAuthProvider
GoogleProvider builds a pre-configured Google OAuth2 provider.
type OAuthService ¶
type OAuthService struct {
// contains filtered or unexported fields
}
OAuthService handles OAuth 2.0 authorization code flow.
func NewOAuthService ¶
func NewOAuthService(providers ...OAuthProvider) *OAuthService
NewOAuthService creates an OAuthService for the given providers.
func (*OAuthService) AuthorizeURL ¶
func (s *OAuthService) AuthorizeURL(providerName, state string) (string, error)
AuthorizeURL builds the redirect URL for the given provider and state token.
func (*OAuthService) ExchangeCode ¶
func (s *OAuthService) ExchangeCode(ctx context.Context, providerName, code string) (OAuthUserInfo, error)
ExchangeCode exchanges an authorization code for user info.
func (*OAuthService) HandleCallback ¶
func (s *OAuthService) HandleCallback( ctx context.Context, authSvc *Service, linkedAccounts LinkedAccountStore, info OAuthUserInfo, tenantID string, linkToUserID string, ) (User, AuthTokens, error)
HandleCallback resolves/creates a user after OAuth callback. If linkToUserID is non-empty the provider is linked to that existing account.
type OAuthUserInfo ¶
type OAuthUserInfo struct {
ProviderID string
Provider string
Email string
Name string
AvatarURL string
Raw map[string]any
}
OAuthUserInfo is the normalized profile returned after token exchange.
type OpenAPIInfo ¶
OpenAPIInfo holds metadata for the generated OpenAPI spec.
type Option ¶
type Option func(*authBuilder) error
Option configures Auth initialization.
func WithLogger ¶
WithLogger provides optional library logging callback.
func WithMetadataProvider ¶
func WithMetadataProvider(store UserMetadataStore) Option
WithMetadataProvider wires optional metadata support.
func WithRBACProvider ¶
func WithRBACProvider(store RolesPermissionsStore) Option
WithRBACProvider wires optional role/permission support.
func WithRequire2FA ¶
WithRequire2FA enables mandatory second-factor login checks.
func WithSecret ¶
WithSecret configures the JWT signing secret.
func WithSessionStore ¶
func WithSessionStore(store SessionStore) Option
WithSessionStore sets a custom SessionStore implementation.
func WithTenantProvider ¶
func WithTenantProvider(store TenantStore) Option
WithTenantProvider wires optional tenant support.
func WithTokenClaimsBuilder ¶
WithTokenClaimsBuilder customizes token claims creation.
func WithTokenTTLs ¶
WithTokenTTLs configures access and refresh TTL.
func WithUserStore ¶
WithUserStore sets a custom UserStore implementation.
type PendingLinkStore ¶
type PendingLinkStore interface {
Save(ctx context.Context, state string, meta OAuthPendingMeta, ttl time.Duration) error
Get(ctx context.Context, state string) (OAuthPendingMeta, error)
Delete(ctx context.Context, state string) error
}
PendingLinkStore holds temporary OAuth link state.
type PublicTenant ¶
type PublicTenant struct {
ID string `json:"id"`
Name string `json:"name"`
IsActive bool `json:"isActive"`
CreatedAt time.Time `json:"createdAt"`
}
PublicTenant is the response-safe projection of Tenant. The tenant Config blob is server-side configuration and is never serialised.
type PublicUser ¶
type PublicUser struct {
ID string `json:"id"`
Email string `json:"email"`
TenantID string `json:"tenantId,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
IsEmailVerified bool `json:"isEmailVerified"`
IsTOTPEnabled bool `json:"isTotpEnabled"`
Roles []string `json:"roles,omitempty"`
Permissions []string `json:"permissions,omitempty"`
Tenants []PublicTenant `json:"tenants,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
CustomClaims map[string]any `json:"customClaims,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
PublicUser is the response-safe projection of User. It is the only user shape adapters may serialise: credential material (password hash, TOTP secret, one-time token hashes and the pending email change) is deliberately absent.
func NewPublicUser ¶
func NewPublicUser(user User) PublicUser
NewPublicUser projects a User onto its response-safe representation.
type RegisterInput ¶
RegisterInput is the payload to create a user and initial session.
type ResetPasswordInput ¶
type RolesPermissionsStore ¶
type RolesPermissionsStore interface {
AddRoleToUser(ctx context.Context, userID, role string, tenantID string) error
RemoveRoleFromUser(ctx context.Context, userID, role string, tenantID string) error
GetRolesForUser(ctx context.Context, userID, tenantID string) ([]string, error)
CreateRole(ctx context.Context, role string, permissions []string) error
DeleteRole(ctx context.Context, role string) error
AddPermissionToRole(ctx context.Context, role, permission string) error
RemovePermissionFromRole(ctx context.Context, role, permission string) error
GetPermissionsForRole(ctx context.Context, role string) ([]string, error)
GetPermissionsForUser(ctx context.Context, userID, tenantID string) ([]string, error)
UserHasPermission(ctx context.Context, userID, permission, tenantID string) (bool, error)
}
RolesPermissionsStore manages RBAC assignments and permissions.
type SMSCodeSendInput ¶
type SMSCodeVerifyInput ¶
type SMSStore ¶
type SMSStore interface {
UpdateSMSCode(ctx context.Context, userID, tenantID, codeHash string, expiry time.Time) error
GetUserBySMSCodeHash(ctx context.Context, userID, tenantID, codeHash string) (User, error)
ClearSMSCode(ctx context.Context, userID, tenantID string) error
}
SMSStore persists sms code state.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the main entry point for authentication operations.
func NewService ¶
func NewService(cfg Config, users UserStore, sessions SessionStore, opts ...ServiceOption) (*Service, error)
NewService builds an auth service with validated configuration.
func (*Service) AddUserToTenant ¶
func (*Service) AssignRole ¶
func (*Service) ChangePassword ¶
func (s *Service) ChangePassword(ctx context.Context, in ChangePasswordInput) error
func (*Service) CleanupExpiredSessions ¶
func (*Service) ConfirmEmailChange ¶
func (s *Service) ConfirmEmailChange(ctx context.Context, in ConfirmEmailChangeInput) error
func (*Service) CreateRole ¶
func (*Service) CreateTenant ¶
func (*Service) DeleteAccount ¶
func (s *Service) DeleteAccount(ctx context.Context, in DeleteAccountInput) error
func (*Service) DisableTOTP ¶
func (*Service) ForgotPassword ¶
func (*Service) GetMetadata ¶
func (*Service) ListSessions ¶
func (*Service) Login ¶
func (s *Service) Login(ctx context.Context, in LoginInput) (User, AuthTokens, error)
func (*Service) Register ¶
func (s *Service) Register(ctx context.Context, in RegisterInput) (User, AuthTokens, error)
func (*Service) RequestEmailChange ¶
func (*Service) ResetPassword ¶
func (s *Service) ResetPassword(ctx context.Context, in ResetPasswordInput) error
func (*Service) RevokeSessionByID ¶
func (*Service) SendMagicLink ¶
func (*Service) SendSMSCode ¶
func (*Service) SendVerificationEmailToken ¶
func (*Service) UpdateMetadata ¶
func (*Service) UpdateProfile ¶
func (*Service) UserHasPermission ¶
func (*Service) VerifyEmail ¶
func (s *Service) VerifyEmail(ctx context.Context, in VerifyEmailInput) error
func (*Service) VerifyMagicLink ¶
func (s *Service) VerifyMagicLink(ctx context.Context, in MagicLinkVerifyInput) (User, AuthTokens, error)
func (*Service) VerifySMSCode ¶
func (s *Service) VerifySMSCode(ctx context.Context, in SMSCodeVerifyInput) (User, AuthTokens, error)
func (*Service) VerifyTOTP ¶
type ServiceOption ¶
type ServiceOption func(*Service)
func WithMetadataStore ¶
func WithMetadataStore(store UserMetadataStore) ServiceOption
func WithRolesPermissionsStore ¶
func WithRolesPermissionsStore(store RolesPermissionsStore) ServiceOption
func WithTenantStore ¶
func WithTenantStore(store TenantStore) ServiceOption
type Session ¶
type Session struct {
ID string
UserID string
TenantID string
RefreshTokenHash string
CreatedAt time.Time
ExpiresAt time.Time
RevokedAt *time.Time
}
Session stores refresh-token bound session metadata.
type SessionAdminStore ¶
type SessionAdminStore interface {
ListSessionsForUser(ctx context.Context, userID, tenantID string) ([]Session, error)
RevokeSessionByID(ctx context.Context, sessionID string) error
DeleteExpiredSessions(ctx context.Context, now time.Time) (int, error)
}
SessionAdminStore exposes session listing and cleanup primitives.
type SessionLookupStore ¶
type SessionLookupStore interface {
GetSessionByID(ctx context.Context, sessionID string) (Session, error)
}
SessionLookupStore resolves sessions by session ID.
type SessionStore ¶
type SessionStore interface {
CreateSession(ctx context.Context, session Session) (Session, error)
GetSessionByRefreshTokenHash(ctx context.Context, tokenHash string) (Session, error)
UpdateSession(ctx context.Context, session Session) error
}
SessionStore persists refresh-token sessions.
type SseDistributor ¶
type SseDistributor interface {
Publish(ctx context.Context, channel string, msg SseMessage) error
Subscribe(ctx context.Context, channel string) (<-chan SseMessage, error)
}
SseDistributor broadcasts SSE messages to all connected clients. Implement this interface for Redis/Kafka-based multi-instance scaling.
type SseHub ¶
type SseHub struct {
// contains filtered or unexported fields
}
SseHub manages in-process SSE subscriptions.
func (*SseHub) Unsubscribe ¶
func (h *SseHub) Unsubscribe(channel string, ch <-chan SseMessage)
Unsubscribe removes a client channel from the hub.
type SseMessage ¶
SseMessage is a single SSE payload.
type TOTPStore ¶
type TOTPStore interface {
UpdateTOTPSecret(ctx context.Context, userID, tenantID, secret string, enabled bool) error
}
TOTPStore persists authenticator setup.
type TelemetryEvent ¶
type TelemetryEvent struct {
ID string
EventName string
UserID string
TenantID string
IP string
UserAgent string
Success bool
Error string
Timestamp time.Time
Meta map[string]any
}
TelemetryEvent represents a single persisted auth event for analytics.
type TelemetryFilter ¶
type TelemetryFilter struct {
UserID string
TenantID string
EventName string
Since time.Time
Until time.Time
Limit int
}
TelemetryFilter restricts query results.
type TelemetryStore ¶
type TelemetryStore interface {
Record(ctx context.Context, event TelemetryEvent) error
Query(ctx context.Context, filter TelemetryFilter) ([]TelemetryEvent, error)
}
TelemetryStore persists auth telemetry events.
type Tenant ¶
type Tenant struct {
ID string
Name string
IsActive bool
Config map[string]any
CreatedAt time.Time
}
Tenant represents an isolated workspace or organization.
type TenantStore ¶
type TenantStore interface {
CreateTenant(ctx context.Context, tenant Tenant) (Tenant, error)
GetTenantByID(ctx context.Context, id string) (Tenant, error)
GetAllTenants(ctx context.Context) ([]Tenant, error)
UpdateTenant(ctx context.Context, id string, update Tenant) error
DeleteTenant(ctx context.Context, id string) error
AssociateUserWithTenant(ctx context.Context, userID, tenantID string) error
DisassociateUserFromTenant(ctx context.Context, userID, tenantID string) error
GetTenantsForUser(ctx context.Context, userID string) ([]Tenant, error)
GetUsersForTenant(ctx context.Context, tenantID string) ([]string, error)
}
TenantStore manages tenants and user memberships.
type UpdateProfileInput ¶
type User ¶
type User struct {
ID string
Email string
PasswordHash string
TenantID string
PhoneNumber string
FirstName string
LastName string
Role string
IsEmailVerified bool
Require2FA bool
IsTOTPEnabled bool
TOTPSecret string
ResetTokenHash string
ResetTokenExpiresAt *time.Time
MagicLinkTokenHash string
MagicLinkTokenExpiresAt *time.Time
SMSCodeHash string
SMSCodeExpiresAt *time.Time
EmailVerificationTokenHash string
EmailVerificationTokenExpiry *time.Time
PendingEmail string
EmailChangeTokenHash string
EmailChangeTokenExpiry *time.Time
Metadata map[string]any
Roles []string
Permissions []string
Tenants []Tenant
CustomClaims map[string]any
CreatedAt time.Time
UpdatedAt time.Time
}
User is the canonical identity model.
type UserAccountStore ¶
type UserAccountStore interface {
UpdateProfile(ctx context.Context, userID, tenantID, firstName, lastName string) (User, error)
DeleteUser(ctx context.Context, userID, tenantID string) error
}
UserAccountStore adds account lifecycle operations.
type UserMetadataStore ¶
type UserMetadataStore interface {
GetMetadata(ctx context.Context, userID string) (map[string]any, error)
UpdateMetadata(ctx context.Context, userID string, metadata map[string]any) error
ClearMetadata(ctx context.Context, userID string) error
}
UserMetadataStore manages arbitrary key/value user metadata.
type UserPasswordStore ¶
type UserPasswordStore interface {
UpdatePassword(ctx context.Context, userID, tenantID, passwordHash string) error
UpdateResetToken(ctx context.Context, userID, tenantID, tokenHash string, expiry time.Time) error
GetUserByResetTokenHash(ctx context.Context, tokenHash string) (User, error)
ClearResetToken(ctx context.Context, userID, tenantID string) error
}
UserPasswordStore adds password lifecycle methods.
type UserStore ¶
type UserStore interface {
CreateUser(ctx context.Context, user User) (User, error)
GetUserByEmail(ctx context.Context, email, tenantID string) (User, error)
GetUserByID(ctx context.Context, id, tenantID string) (User, error)
}
UserStore persists and resolves users.
type VerifyEmailInput ¶
type VerifyEmailInput struct {
Token string
}
type WebhookDispatcher ¶
type WebhookDispatcher struct {
// contains filtered or unexported fields
}
WebhookDispatcher delivers signed webhook payloads.
func NewWebhookDispatcher ¶
func NewWebhookDispatcher(endpoints ...WebhookEndpoint) *WebhookDispatcher
NewWebhookDispatcher creates a dispatcher for the given endpoints.