jwt

package
v0.0.46 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package jwt provides functions for generating and validating JSON Web Tokens (JWTs).

It supports HS256, RS256, and ES256 algorithms.

Usage:

1. Initialize the package using InitializeJWT, providing key management options. 2. Generate JWTs using GenerateToken. 3. Validate JWTs using ValidateToken.

Example:

// Initialization keys, pubKeys, err := jwt.InitializeJWT(true, []jwt.JWTAlgorithm{jwt.HS256, jwt.RS256, jwt.ES256}, nil, nil)

if err != nil {
    // Handle error
}

// Token Generation token, exp, err := jwt.GenerateToken(jwt.HS256, "UTC", "1h", "issuer", "subject", "audience", "data")

if err != nil {
    // Handle error
}

// Token Validation valid, err := jwt.ValidateToken(token, "issuer", "audience", "UTC")

if err != nil {
    // Handle error
}

Index

Constants

View Source
const (
	// HS256 is the HMAC SHA-256 algorithm, using a shared secret.
	HS256 = JWTAlgorithm("HS256")
	// RS256 is the RSA Signature with SHA-256 algorithm, using a public/private key pair.
	RS256 = JWTAlgorithm("RS256")
	// ES256 is the ECDSA Signature with P-256 and SHA-256 algorithm, using a public/private key pair.
	ES256 = JWTAlgorithm("ES256")
)

Variables

This section is empty.

Functions

func GenerateToken

func GenerateToken(algorithm JWTAlgorithm, timeZone string, timeoutPeriod string, issuer string, subject string, audience string, data string) (string, int64, error)

GenerateToken generates a JWT token with the specified algorithm and claims. It returns the encoded token, expiration time, and any error encountered.

func GetAudience

func GetAudience(tokenString string) (string, error)

Get the audience of the token

func GetExpirationTime

func GetExpirationTime(tokenString string) (int64, error)

Get the expiration time of the token

func GetJwtID added in v0.0.30

func GetJwtID(tokenString string) (string, error)

Get the JWT ID of the token

func GetSubject added in v0.0.33

func GetSubject(tokenString string) (string, error)

Get the subject of the token

func InitializeJWT added in v0.0.22

func InitializeJWT(generateKeys bool, algorithms []JWTAlgorithm, existingKeys AlgoKeysStruct, existingPubKeys PubKeysStruct) (AlgoKeysStruct, PubKeysStruct, error)

InitializeJWT initializes the JWT package with keys for generating and validating tokens. If generateKeys is true, new keys are generated for the specified algorithms. If generateKeys is false, existingKeys and existingPubKeys must be provided.

func ValidateToken

func ValidateToken(targetTokenString, expectedIssuer, expectedAudience, timezone string) (bool, error)

ValidateToken validates a JWT token against the provided issuer and audience. It returns true if the token is valid, and false otherwise, along with any error encountered.

Types

type AlgoKeysStruct added in v0.0.22

type AlgoKeysStruct map[JWTAlgorithm]string

AlgoKeysStruct maps JWT algorithms to their respective private keys (base64 encoded).

type JWTAlgorithm added in v0.0.22

type JWTAlgorithm string

JWTAlgorithm is the algorithm used for JWT token generation

type JWTToken

type JWTToken struct {
	Header    jwtTokenHeader  `json:"header"`    // JWT header
	Payload   jwtTokenPayload `json:"payload"`   // JWT payload
	Signature string          `json:"signature"` // JWT signature (base64 encoded)
}

JWTToken represents a parsed or generated JWT.

type PubKeysStruct added in v0.0.22

type PubKeysStruct map[JWTAlgorithm]string

PubKeysStruct maps JWT algorithms to their respective public keys (base64 encoded).

Jump to

Keyboard shortcuts

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