auth

package
v0.0.0-...-e13c845 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken         = errors.New("invalid token")
	ErrExpiredToken         = errors.New("token has expired")
	ErrMissingToken         = errors.New("missing token")
	ErrInvalidSigningMethod = errors.New("invalid signing method")
)

Functions

func GetRoles

func GetRoles(ctx abstract.Context) []string

func GetString

func GetString(ctx abstract.Context, key string) string

func GetUserID

func GetUserID(ctx abstract.Context) string

func GetUsername

func GetUsername(ctx abstract.Context) string

func HasAllRoles

func HasAllRoles(ctx abstract.Context, roles ...string) bool

func HasAnyRole

func HasAnyRole(ctx abstract.Context, roles ...string) bool

func HasRole

func HasRole(ctx abstract.Context, role string) bool

func NewAPIKey

func NewAPIKey(config *APIKeyConfig) abstract.Middleware

func NewBasicAuth

func NewBasicAuth(config *BasicAuthConfig) abstract.Middleware

Types

type APIKeyConfig

type APIKeyConfig struct {
	Keys         []string
	HeaderName   string
	QueryParam   string
	ContextKey   string
	SkipPaths    []string
	ValidateFunc func(key string) bool
}

type AuthMiddleware

type AuthMiddleware struct {
	// contains filtered or unexported fields
}

func New

func New(provider *JWTProvider, config *Config) *AuthMiddleware

func (*AuthMiddleware) AsMiddleware

func (m *AuthMiddleware) AsMiddleware() abstract.Middleware

func (*AuthMiddleware) Handle

func (m *AuthMiddleware) Handle(ctx abstract.Context, next func() error) error

func (*AuthMiddleware) WithRefresh

func (m *AuthMiddleware) WithRefresh(config *RefreshConfig) *AuthMiddleware

type BasicAuthConfig

type BasicAuthConfig struct {
	Users        map[string]string
	Realm        string
	ContextKey   string
	SkipPaths    []string
	ValidateFunc func(username, password string) bool
}

type Claims

type Claims struct {
	UserID   string         `json:"user_id"`
	Username string         `json:"username"`
	Roles    []string       `json:"roles,omitempty"`
	Extra    map[string]any `json:"extra,omitempty"`
	jwt.RegisteredClaims
}

func GetClaims

func GetClaims(ctx abstract.Context) *Claims

type Config

type Config struct {
	TokenLookup    string
	TokenHeader    string
	AuthScheme     string
	ContextKey     string
	SkipPaths      []string
	SkipFunc       func(ctx abstract.Context) bool
	SuccessHandler func(ctx abstract.Context) error
	ErrorHandler   func(ctx abstract.Context, err error) error
}

func DefaultConfig

func DefaultConfig() *Config

type JWTConfig

type JWTConfig struct {
	Secret          string
	SigningMethod   jwt.SigningMethod
	AccessTokenTTL  time.Duration
	RefreshTokenTTL time.Duration
	Issuer          string
	Audience        []string
}

func DefaultJWTConfig

func DefaultJWTConfig() *JWTConfig

type JWTProvider

type JWTProvider struct {
	// contains filtered or unexported fields
}

func NewJWTProvider

func NewJWTProvider(config *JWTConfig, store TokenStore) *JWTProvider

func (*JWTProvider) GenerateTokenPair

func (p *JWTProvider) GenerateTokenPair(userID, username string, roles []string, extra map[string]any) (*TokenPair, error)

func (*JWTProvider) RefreshToken

func (p *JWTProvider) RefreshToken(refreshToken string) (*TokenPair, error)

func (*JWTProvider) RevokeAllUserTokens

func (p *JWTProvider) RevokeAllUserTokens(ctx context.Context, userID string) error

func (*JWTProvider) RevokeToken

func (p *JWTProvider) RevokeToken(ctx context.Context, tokenID string) error

func (*JWTProvider) ValidateToken

func (p *JWTProvider) ValidateToken(tokenString string) (*Claims, error)

type RefreshConfig

type RefreshConfig struct {
	Enabled           bool
	Threshold         time.Duration
	RefreshHeaderName string
	MaxRefreshCount   int
}

func DefaultRefreshConfig

func DefaultRefreshConfig() *RefreshConfig

type RefreshMiddleware

type RefreshMiddleware struct {
	// contains filtered or unexported fields
}

func NewRefreshMiddleware

func NewRefreshMiddleware(provider *JWTProvider, config *RefreshConfig, blacklist TokenBlacklist) *RefreshMiddleware

func (*RefreshMiddleware) Handle

func (m *RefreshMiddleware) Handle(ctx abstract.Context, next func() error) error

func (*RefreshMiddleware) WithRefreshEndpoint

func (m *RefreshMiddleware) WithRefreshEndpoint(path string) abstract.RouteHandler

type TokenBlacklist

type TokenBlacklist interface {
	Add(ctx context.Context, token string, ttl time.Duration) error
	Exists(ctx context.Context, token string) (bool, error)
}

type TokenPair

type TokenPair struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token,omitempty"`
	TokenType    string `json:"token_type"`
	ExpiresIn    int64  `json:"expires_in"`
}

type TokenStore

type TokenStore interface {
	Set(ctx context.Context, key string, value any, ttl time.Duration) error
	Get(ctx context.Context, key string) (any, error)
	Delete(ctx context.Context, key string) error
	Exists(ctx context.Context, key string) (bool, error)
}

Jump to

Keyboard shortcuts

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