jwt

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidToken is returned when token validation fails
	ErrInvalidToken = errors.New("invalid token")

	// ErrExpiredToken is returned when token has expired
	ErrExpiredToken = errors.New("token has expired")

	// ErrInvalidClaims is returned when token claims are invalid
	ErrInvalidClaims = errors.New("invalid token claims")

	// ErrWeakSecretKey is returned when the secret key is too short
	ErrWeakSecretKey = errors.New("secret key must be at least 32 bytes")
)

Functions

This section is empty.

Types

type Claims

type Claims struct {
	UserID   string `json:"user_id"`
	Username string `json:"username"`
	Email    string `json:"email"`
	Role     string `json:"role"`
	jwt.RegisteredClaims
}

Claims represents JWT claims for authentication

type Config

type Config struct {
	// Secret key for signing tokens
	SecretKey []byte

	// Issuer of the token
	Issuer string

	// Access token expiration duration
	AccessTokenExpiry time.Duration

	// Refresh token expiration duration
	RefreshTokenExpiry time.Duration
}

Config represents JWT configuration

func DefaultConfig

func DefaultConfig(secretKey string) (*Config, error)

DefaultConfig returns a Config with sensible defaults. The secretKey must be at least 32 bytes long.

type Service

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

Service provides JWT token operations

func NewService

func NewService(cfg *Config) *Service

NewService creates a new JWT service

func (*Service) GenerateAccessToken

func (s *Service) GenerateAccessToken(userID, username, email, role string) (string, error)

GenerateAccessToken generates a new access token

func (*Service) GenerateRefreshToken

func (s *Service) GenerateRefreshToken(userID string) (string, error)

GenerateRefreshToken generates a new refresh token

func (*Service) GetExpiryTime

func (s *Service) GetExpiryTime(tokenString string) (time.Time, error)

GetExpiryTime returns the expiry time from a token

func (*Service) IsExpired

func (s *Service) IsExpired(tokenString string) bool

IsExpired checks if a token is expired

func (*Service) ValidateToken

func (s *Service) ValidateToken(tokenString string) (*Claims, error)

ValidateToken validates a JWT token and returns the claims

Jump to

Keyboard shortcuts

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