Documentation
¶
Overview ¶
Package auth provides authentication for minimaldoc-server. Supports local email/password and OAuth 2.0/OIDC providers.
Index ¶
- func GenerateAPIKey() (string, error)
- func GenerateRefreshToken(userID, secret string, expiry time.Duration) (string, error)
- func GenerateSessionToken() (string, error)
- func GenerateToken(userID, email, role, siteID, secret string, expiry time.Duration) (string, error)
- func GenerateVerificationToken() (string, error)
- func HashAPIKey(key string) string
- func HashPassword(password string, cost int) (string, error)
- func HashSessionToken(token string) string
- func ValidateRefreshToken(tokenString, secret string) (string, error)
- func VerifyPassword(password, hash string) bool
- type Claims
- type LocalProvider
- func (p *LocalProvider) Authenticate(ctx context.Context, credentials map[string]string) (*UserInfo, error)
- func (p *LocalProvider) CreateUser(ctx context.Context, siteID, email, password, name, role string) (*UserInfo, error)
- func (p *LocalProvider) GetAuthURL(state string) string
- func (p *LocalProvider) HandleCallback(ctx context.Context, code string) (*UserInfo, error)
- func (p *LocalProvider) Name() string
- func (p *LocalProvider) UpdatePassword(ctx context.Context, userID, newPassword string) error
- type OAuthProvider
- type Provider
- type ProviderRegistry
- type UserInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateAPIKey ¶
GenerateAPIKey creates a cryptographically secure API key.
func GenerateRefreshToken ¶
GenerateRefreshToken creates a longer-lived refresh token.
func GenerateSessionToken ¶
GenerateSessionToken creates a random session token.
func GenerateToken ¶
func GenerateToken(userID, email, role, siteID, secret string, expiry time.Duration) (string, error)
GenerateToken creates a new JWT token.
func GenerateVerificationToken ¶
GenerateVerificationToken creates a token for email verification.
func HashAPIKey ¶
HashAPIKey creates a SHA-256 hash of an API key for storage.
func HashPassword ¶
HashPassword creates a bcrypt hash of the password.
func HashSessionToken ¶
HashSessionToken creates a hash of a session token for storage.
func ValidateRefreshToken ¶
ValidateRefreshToken parses and validates a refresh token.
func VerifyPassword ¶
VerifyPassword compares a password with its hash.
Types ¶
type Claims ¶
type Claims struct {
jwt.RegisteredClaims
UserID string `json:"uid"`
Email string `json:"email"`
Role string `json:"role"`
SiteID string `json:"sid"`
}
Claims represents JWT token claims.
func ValidateToken ¶
ValidateToken parses and validates a JWT token.
type LocalProvider ¶
type LocalProvider struct {
// contains filtered or unexported fields
}
LocalProvider implements email/password authentication.
func NewLocalProvider ¶
func NewLocalProvider(db *sql.DB, bcryptCost int) *LocalProvider
NewLocalProvider creates a new local authentication provider.
func (*LocalProvider) Authenticate ¶
func (p *LocalProvider) Authenticate(ctx context.Context, credentials map[string]string) (*UserInfo, error)
Authenticate validates email/password credentials.
func (*LocalProvider) CreateUser ¶
func (p *LocalProvider) CreateUser(ctx context.Context, siteID, email, password, name, role string) (*UserInfo, error)
CreateUser creates a new user with email/password.
func (*LocalProvider) GetAuthURL ¶
func (p *LocalProvider) GetAuthURL(state string) string
GetAuthURL is not applicable for local auth.
func (*LocalProvider) HandleCallback ¶
HandleCallback is not applicable for local auth.
func (*LocalProvider) Name ¶
func (p *LocalProvider) Name() string
Name returns the provider identifier.
func (*LocalProvider) UpdatePassword ¶
func (p *LocalProvider) UpdatePassword(ctx context.Context, userID, newPassword string) error
UpdatePassword changes a user's password.
type OAuthProvider ¶
type OAuthProvider struct {
// contains filtered or unexported fields
}
OAuthProvider implements OAuth 2.0 / OIDC authentication.
func NewOAuthProvider ¶
func NewOAuthProvider(cfg config.OAuthProvider) *OAuthProvider
NewOAuthProvider creates a new OAuth provider.
func (*OAuthProvider) Authenticate ¶
func (p *OAuthProvider) Authenticate(ctx context.Context, credentials map[string]string) (*UserInfo, error)
Authenticate is not used for OAuth (redirects are used instead).
func (*OAuthProvider) GetAuthURL ¶
func (p *OAuthProvider) GetAuthURL(state string) string
GetAuthURL returns the URL to redirect for OAuth login.
func (*OAuthProvider) HandleCallback ¶
HandleCallback processes the OAuth callback and returns user info.
func (*OAuthProvider) Name ¶
func (p *OAuthProvider) Name() string
Name returns the provider identifier.
type Provider ¶
type Provider interface {
// Name returns the provider identifier.
Name() string
// Authenticate validates credentials and returns user info.
Authenticate(ctx context.Context, credentials map[string]string) (*UserInfo, error)
// GetAuthURL returns the URL to redirect for OAuth login (OAuth providers only).
GetAuthURL(state string) string
// HandleCallback processes the OAuth callback and returns user info.
HandleCallback(ctx context.Context, code string) (*UserInfo, error)
}
Provider defines the interface for authentication providers.
type ProviderRegistry ¶
type ProviderRegistry struct {
// contains filtered or unexported fields
}
ProviderRegistry manages authentication providers.
func NewRegistry ¶
func NewRegistry() *ProviderRegistry
NewRegistry creates a new provider registry.
func (*ProviderRegistry) Get ¶
func (r *ProviderRegistry) Get(name string) (Provider, error)
Get retrieves a provider by name.
func (*ProviderRegistry) List ¶
func (r *ProviderRegistry) List() []string
List returns all registered provider names.
func (*ProviderRegistry) Register ¶
func (r *ProviderRegistry) Register(p Provider)
Register adds a provider to the registry.
type UserInfo ¶
type UserInfo struct {
ID string
Email string
Name string
AvatarURL string
EmailVerified bool
Provider string // "local", "google", "github", "cognito", etc.
ProviderID string // ID from OAuth provider
Role string // admin, editor, viewer
}
UserInfo contains authenticated user information.