auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 12 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")
	ErrInvalidClaims = errors.New("invalid claims")
)

Functions

func GenerateAPIKey

func GenerateAPIKey(prefix string) (string, error)

GenerateAPIKey creates a new API key.

func GetUserEmail

func GetUserEmail(ctx context.Context) string

GetUserEmail extracts user email from context.

func GetUserID

func GetUserID(ctx context.Context) string

GetUserID extracts user ID from context.

func GetUserRoles

func GetUserRoles(ctx context.Context) []string

GetUserRoles extracts user roles from context.

func HashAPIKey

func HashAPIKey(key string) string

HashAPIKey creates a hash of the API key for storage.

func RequireRole

func RequireRole(roles ...string) func(http.Handler) http.Handler

RequireRole creates middleware that requires specific roles.

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.

type TokenPair

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

TokenPair contains access and refresh tokens.

Directories

Path Synopsis
Package adapter provides hexagonal architecture adapters for auth.
Package adapter provides hexagonal architecture adapters for auth.

Jump to

Keyboard shortcuts

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