jwt

package
v0.23.30 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package jwt provides JWT token generation and validation services.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New("invalid token")
	ErrExpiredToken = errors.New("token has expired")
)

Functions

func GenerateRandomToken

func GenerateRandomToken(length int) (string, error)

GenerateRandomToken generates a cryptographically secure random token.

Types

type Claims

type Claims struct {
	jwt.RegisteredClaims
	UserID          uuid.UUID `json:"user_id"`
	Email           string    `json:"email"`
	IsPlatformAdmin bool      `json:"is_platform_admin,omitempty"`
}

Claims represents the JWT claims for access tokens.

type Service

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

Service handles JWT token operations.

func NewService

func NewService(secret string, accessTTLSeconds, refreshTTLSeconds int) *Service

NewService creates a new JWT service.

func NewServiceWithIssuer

func NewServiceWithIssuer(secret string, accessTTLSeconds, refreshTTLSeconds int, issuer string) *Service

NewServiceWithIssuer creates a new JWT service with a custom issuer.

func (*Service) AccessTokenTTL

func (s *Service) AccessTokenTTL() time.Duration

AccessTokenTTL returns the access token TTL.

func (*Service) GenerateAccessToken

func (s *Service) GenerateAccessToken(userID uuid.UUID, email string, isPlatformAdmin bool) (string, int, error)

GenerateAccessToken generates only an access token (useful for token refresh).

func (*Service) GenerateTokenPair

func (s *Service) GenerateTokenPair(userID uuid.UUID, email string, isPlatformAdmin bool) (*TokenPair, error)

GenerateTokenPair generates a new access and refresh token pair.

func (*Service) RefreshTokenTTL

func (s *Service) RefreshTokenTTL() time.Duration

RefreshTokenTTL returns the refresh token TTL.

func (*Service) ValidateAccessToken

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

ValidateAccessToken validates an access token and returns the claims.

type TokenPair

type TokenPair struct {
	AccessToken  string
	RefreshToken string
	ExpiresIn    int // seconds until access token expires
}

TokenPair represents an access and refresh token pair.

Jump to

Keyboard shortcuts

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