Documentation
¶
Index ¶
- Variables
- func GenerateAPIKey(prefix string) (string, error)
- func GetUserEmail(ctx context.Context) string
- func GetUserID(ctx context.Context) string
- func GetUserRoles(ctx context.Context) []string
- func HashAPIKey(key string) string
- func RequireRole(roles ...string) func(http.Handler) http.Handler
- type APIKey
- type APIKeyManager
- type JWTConfig
- type JWTValidator
- func (v *JWTValidator) GenerateTokenPair(ctx context.Context, userID, email string, roles []string) (TokenPair, error)
- func (v *JWTValidator) InvalidateToken(ctx context.Context, tokenString string) error
- func (v *JWTValidator) Middleware() func(http.Handler) http.Handler
- func (v *JWTValidator) ValidateAccessToken(ctx context.Context, tokenString string) (*TokenClaims, error)
- func (v *JWTValidator) ValidateRefreshToken(ctx context.Context, tokenString string) (*TokenClaims, error)
- type TokenClaims
- type TokenPair
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func GenerateAPIKey ¶
GenerateAPIKey creates a new API key.
func GetUserEmail ¶
GetUserEmail extracts user email from context.
func GetUserRoles ¶
GetUserRoles extracts user roles from context.
func HashAPIKey ¶
HashAPIKey creates a hash of the API key for storage.
Types ¶
type APIKey ¶
type APIKey struct {
ID string
Name string
Prefix string
Hash string
UserID string
Scopes []string
CreatedAt time.Time
ExpiresAt *time.Time
LastUsedAt *time.Time
RateLimit int
}
APIKey represents an API key.
type APIKeyManager ¶
type APIKeyManager struct {
// contains filtered or unexported fields
}
APIKeyManager manages API keys.
func NewAPIKeyManager ¶
func NewAPIKeyManager() *APIKeyManager
NewAPIKeyManager creates a new API key manager.
func (*APIKeyManager) CreateKey ¶
func (m *APIKeyManager) CreateKey(ctx context.Context, userID, name string, scopes []string, rateLimit int) (string, *APIKey, error)
CreateKey creates a new API key.
func (*APIKeyManager) RevokeKey ¶
func (m *APIKeyManager) RevokeKey(keyID string) error
RevokeKey revokes an API key.
func (*APIKeyManager) ValidateKey ¶
func (m *APIKeyManager) ValidateKey(key string) (*APIKey, bool)
ValidateKey validates an API key.
type JWTConfig ¶
type JWTConfig struct {
SecretKey string
PrivateKey *rsa.PrivateKey
PublicKey *rsa.PublicKey
AccessTokenExpiry time.Duration
RefreshTokenExpiry time.Duration
Issuer string
Audience string
}
JWTConfig holds JWT configuration.
type JWTValidator ¶
type JWTValidator struct {
// contains filtered or unexported fields
}
JWTValidator validates JWT tokens.
func NewJWTValidator ¶
func NewJWTValidator(cfg JWTConfig) *JWTValidator
NewJWTValidator creates a new JWT validator.
func (*JWTValidator) GenerateTokenPair ¶
func (v *JWTValidator) GenerateTokenPair(ctx context.Context, userID, email string, roles []string) (TokenPair, error)
GenerateTokenPair creates new access and refresh tokens.
func (*JWTValidator) InvalidateToken ¶
func (v *JWTValidator) InvalidateToken(ctx context.Context, tokenString string) error
InvalidateToken revokes a token (simple implementation - production needs blacklist).
func (*JWTValidator) Middleware ¶
func (v *JWTValidator) Middleware() func(http.Handler) http.Handler
Middleware returns HTTP middleware for JWT validation.
func (*JWTValidator) ValidateAccessToken ¶
func (v *JWTValidator) ValidateAccessToken(ctx context.Context, tokenString string) (*TokenClaims, error)
ValidateAccessToken validates an access token.
func (*JWTValidator) ValidateRefreshToken ¶
func (v *JWTValidator) ValidateRefreshToken(ctx context.Context, tokenString string) (*TokenClaims, error)
ValidateRefreshToken validates a refresh token.
type TokenClaims ¶
type TokenClaims struct {
jwt.RegisteredClaims
UserID string `json:"user_id"`
Email string `json:"email"`
Roles []string `json:"roles"`
Scope string `json:"scope"`
}
TokenClaims represents JWT claims.