jwt

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateAccessToken

func GenerateAccessToken(
	user *UserClaims,
	key string,
	expiresAt time.Time,
	customClaimsFunc CustomClaimsFunc,
) (string, time.Time, error)

GenerateAccessToken generates a JWT access token for a user

func GenerateRefreshToken

func GenerateRefreshToken(
	userID uint64,
	sessionID uint64,
	key string,
	expiresAt time.Time,
) (string, error)

GenerateRefreshToken generates a new refresh token JWT with the given session ID

func JwtClaimsToUser

func JwtClaimsToUser(claims *UserClaims) *fs.User

JwtClaimsToUser converts jwt.UserClaims to fs.User

Types

type AccessTokenClaims

type AccessTokenClaims struct {
	jwt.RegisteredClaims

	User *UserClaims `json:"user"`
}

AccessTokenClaims represents the claims in an access token JWT

func ParseAccessToken

func ParseAccessToken(tokenString, key string) (*AccessTokenClaims, error)

ParseAccessToken parses and validates an access token

type Config

type Config struct {
	Key                    string
	AccessTokenExpiration  time.Duration
	RefreshTokenExpiration time.Duration
	CustomClaimsFunc       CustomClaimsFunc
}

Config holds configuration for token generation

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default token configuration

type CustomClaimsFunc

type CustomClaimsFunc func(claims *AccessTokenClaims) (jwt.Claims, error)

CustomClaimsFunc is a function that allows customization of JWT claims

func WrapCustomClaimsFunc

func WrapCustomClaimsFunc(c fs.Context, fn func() fs.JwtCustomClaimsFunc) CustomClaimsFunc

WrapCustomClaimsFunc wraps fs.JwtCustomClaimsFunc to jwt.CustomClaimsFunc

type JWTTokens

type JWTTokens struct {
	AccessToken           string     `json:"token"`
	AccessTokenExpiresAt  time.Time  `json:"expires"`
	RefreshToken          string     `json:"refresh_token,omitempty"`
	RefreshTokenExpiresAt *time.Time `json:"refresh_token_expires,omitempty"`
}

JWTTokens represents both access and refresh tokens

type RefreshTokenClaims

type RefreshTokenClaims struct {
	jwt.RegisteredClaims

	UserID    uint64 `json:"uid"`
	SessionID uint64 `json:"sid"` // Session ID from database
}

RefreshTokenClaims represents the claims in a refresh token JWT

func ParseRefreshToken

func ParseRefreshToken(tokenString, key string) (*RefreshTokenClaims, error)

ParseRefreshToken parses and validates a refresh token

type UserClaims

type UserClaims struct {
	ID                   uint64   `json:"id,omitempty"`
	Username             string   `json:"username,omitempty"`
	Email                string   `json:"email,omitempty"`
	FirstName            string   `json:"first_name,omitempty"`
	LastName             string   `json:"last_name,omitempty"`
	Active               bool     `json:"active,omitempty"`
	Provider             string   `json:"provider,omitempty"`
	ProviderProfileImage string   `json:"provider_profile_image,omitempty"`
	RoleIDs              []uint64 `json:"role_ids,omitempty"`
}

UserClaims represents the minimal user data needed for JWT generation

func UserToJwtClaims

func UserToJwtClaims(user *fs.User) *UserClaims

UserToJwtClaims converts an fs.User to jwt.UserClaims

Jump to

Keyboard shortcuts

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