Documentation
¶
Overview ¶
Package auth provides JWT token management and authentication utilities. It supports token generation, validation, and claims extraction.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BlacklistManager ¶
type BlacklistManager interface {
// BlacklistToken adds a token to the blacklist
BlacklistToken(tokenID string, expiresAt time.Time) error
// IsBlacklisted checks if a token is blacklisted
IsBlacklisted(tokenID string) (bool, error)
// CleanupExpired removes expired tokens from blacklist
CleanupExpired() error
}
BlacklistManager defines the interface for token blacklisting.
type Claims ¶
type Claims struct {
UserID string `json:"user_id"`
TenantID string `json:"tenant_id"`
Roles []string `json:"roles"`
TokenID string `json:"token_id"`
jwt.RegisteredClaims
}
Claims represents JWT token claims.
func (*Claims) GetHighestRole ¶
GetHighestRole returns the highest privilege role.
func (*Claims) IsEmployee ¶
IsEmployee checks if the claims contain employee role.
type TokenInfo ¶
type TokenInfo struct {
TokenID string `json:"token_id"`
UserID string `json:"user_id"`
TenantID string `json:"tenant_id"`
Roles []string `json:"roles"`
IssuedAt time.Time `json:"issued_at"`
ExpiresAt time.Time `json:"expires_at"`
Valid bool `json:"valid"`
}
TokenInfo represents token information for debugging/logging.
func GetTokenInfo ¶
func GetTokenInfo(ctx context.Context, tokenString string, jwtManager TokenManager) *TokenInfo
GetTokenInfo extracts token information for debugging.
type TokenManager ¶
type TokenManager interface {
// GenerateToken creates a new JWT token for the user
GenerateToken(ctx context.Context, userID, tenantID string, roles []string) (string, error)
// ValidateToken validates a JWT token and returns claims
ValidateToken(ctx context.Context, tokenString string) (*Claims, error)
// RefreshToken creates a new token from an existing valid token
RefreshToken(ctx context.Context, tokenString string) (string, error)
// ExtractClaims extracts claims from a token without validation
ExtractClaims(ctx context.Context, tokenString string) (*Claims, error)
}
TokenManager defines the interface for JWT token management.
func NewJWTManager ¶
func NewJWTManager(cfg config.JWTConfig) TokenManager
NewJWTManager creates a new JWT token manager.
func NewJWTManagerWithSecret ¶
func NewJWTManagerWithSecret(secret string) TokenManager
NewJWTManagerWithSecret creates a new JWT token manager with a secret string.
type TokenValidator ¶
type TokenValidator struct {
// contains filtered or unexported fields
}
TokenValidator provides additional token validation functionality.
func NewTokenValidator ¶
func NewTokenValidator(jwtManager TokenManager, blacklistManager BlacklistManager) *TokenValidator
NewTokenValidator creates a new token validator.
func (*TokenValidator) RevokeToken ¶
func (tv *TokenValidator) RevokeToken(ctx context.Context, tokenString string) error
RevokeToken adds a token to the blacklist.
func (*TokenValidator) ValidateWithBlacklist ¶
func (tv *TokenValidator) ValidateWithBlacklist(ctx context.Context, tokenString string) (*Claims, error)
ValidateWithBlacklist validates a token and checks blacklist.