jwt

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package jwt provides JWT token generation and validation for the application.

The module covers:

  • token generation with HS256 algorithm,
  • token validation and claims extraction,
  • 24-hour token expiry,
  • integration with the users domain.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidConfig       = errors.New("invalid config")
	ErrInvalidToken        = errors.New("invalid token")
	ErrExpiredToken        = errors.New("token has expired")
	ErrRefreshTokenRevoked = errors.New("refresh token has been revoked")
)

Functions

func Module

func Module() fx.Option

Module creates and returns an FX module for the jwt package.

The module provides:

  • JWTService for token generation, validation, and refresh token management

Types

type Claims

type Claims struct {
	jwt.RegisteredClaims

	UserID int64        `json:"user_id"`
	Role   users.Role   `json:"role"`
	Status users.Status `json:"status"`
}

Claims represents the JWT token claims.

type Config

type Config struct {
	Secret     string
	AccessTTL  time.Duration
	RefreshTTL time.Duration
	Issuer     string
}

func (Config) Validate

func (c Config) Validate() error

type Repository added in v0.1.0

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

func NewRepository added in v0.1.0

func NewRepository(db *bun.DB) *Repository

func (*Repository) Create added in v0.1.0

func (r *Repository) Create(ctx context.Context, userID int64, tokenHash string, expiresAt time.Time) error

func (*Repository) FindByHash added in v0.1.0

func (r *Repository) FindByHash(ctx context.Context, tokenHash string) (*Token, error)

func (*Repository) RevokeAllForUser added in v0.1.0

func (r *Repository) RevokeAllForUser(ctx context.Context, userID int64) error

func (*Repository) RevokeByHash added in v0.1.0

func (r *Repository) RevokeByHash(ctx context.Context, tokenHash string) error

type Service

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

Service handles JWT token operations.

func NewService

func NewService(config Config, refreshRepo *Repository) *Service

NewService creates and initializes a new JWT service.

func (*Service) GenerateRefreshToken added in v0.1.0

func (s *Service) GenerateRefreshToken(ctx context.Context, userID int64) (string, error)

GenerateRefreshToken creates an opaque random refresh token, stores its hash in the database, and returns the raw token string.

func (*Service) GenerateTokenPair added in v0.1.0

func (s *Service) GenerateTokenPair(ctx context.Context, user *users.User) (string, string, error)

GenerateTokenPair creates a new access token and refresh token pair.

func (*Service) RevokeRefreshToken added in v0.1.0

func (s *Service) RevokeRefreshToken(ctx context.Context, rawToken string) error

RevokeRefreshToken revokes a raw refresh token.

func (*Service) RotateRefreshToken added in v0.1.0

func (s *Service) RotateRefreshToken(ctx context.Context, oldRawToken string, userID int64) (string, error)

RotateRefreshToken revokes the old token and generates a new one. Returns the new raw refresh token.

func (*Service) RotateTokenPair added in v0.1.0

func (s *Service) RotateTokenPair(ctx context.Context, oldRawToken string, user *users.User) (string, string, error)

RotateTokenPair revokes the old refresh token and generates a new access token + refresh token pair.

func (*Service) ValidateRefreshToken added in v0.1.0

func (s *Service) ValidateRefreshToken(ctx context.Context, rawToken string) (int64, error)

ValidateRefreshToken validates a raw refresh token. Returns the user ID if valid.

func (*Service) ValidateToken

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

ValidateToken validates and parses a JWT token. Returns the claims if valid, or an error if invalid/expired.

type Token added in v0.1.0

type Token struct {
	UserID    int64
	Value     string
	ExpiresAt time.Time
	Revoked   bool
}

Jump to

Keyboard shortcuts

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