Skip to content

Package Functions ​

New ​

go
func New(cfg Config) (*Processor, error)

Creates a new JWT Processor. Use DefaultConfig() to get default configuration, modify required fields, and pass it in.

Parameters ​

ParameterTypeDescription
cfgConfigConfiguration

Returns ​

ReturnTypeDescription
processor*ProcessorJWT processor
errerrorError when configuration validation fails

Example ​

go
package main

import (
	"fmt"

	"github.com/cybergodev/jwt"
)

func main() {
	cfg := jwt.DefaultConfig()
	cfg.SecretKey = "hmac-key-that-has-at-least-32-bytes!"

	processor, err := jwt.New(cfg)
	if err != nil {
		panic(err)
	}
	defer processor.Close()

	fmt.Println("Processor created successfully")
}

Errors ​

ErrorTrigger Condition
ErrInvalidConfigInvalid configuration
ErrInvalidSecretKeyKey missing, under 32 bytes, weak, wrong type, or ECDSA curve mismatch
ErrInvalidSigningMethodUnsupported signing algorithm

DefaultConfig ​

go
func DefaultConfig() Config

Returns a configuration with sensible defaults.

Returns ​

ReturnTypeDescription
configConfigDefault configuration

Defaults ​

FieldDefault
AccessTokenTTL15 * time.Minute
RefreshTokenTTL7 * 24 * time.Hour
Issuer"jwt-service"
SigningMethodSigningMethodHS256
RateLimitRate100
RateLimitWindowtime.Minute
BlacklistDefaultBlacklistConfig()

Example ​

go
cfg := jwt.DefaultConfig()
cfg.SecretKey = "hmac-key-that-has-at-least-32-bytes!"
// Modify other fields as needed

DefaultBlacklistConfig ​

go
func DefaultBlacklistConfig() BlacklistConfig

Returns a blacklist configuration with sensible defaults. The common way to customize blacklist parameters is to take the defaults, override the fields that need changing, and assign the result to Config.Blacklist.

Returns ​

ReturnTypeDescription
configBlacklistConfigDefault blacklist configuration

Defaults ​

FieldDefault
CleanupInterval5 * time.Minute
MaxSize100000
EnableAutoCleanuptrue

Example ​

go
package main

import (
	"fmt"
	"time"

	"github.com/cybergodev/jwt"
)

func main() {
	blacklist := jwt.DefaultBlacklistConfig()
	blacklist.CleanupInterval = time.Minute // Override the field that needs changing; keep the rest

	fmt.Println(blacklist.CleanupInterval)   // Output: 1m0s
	fmt.Println(blacklist.MaxSize)           // Output: 100000
	fmt.Println(blacklist.EnableAutoCleanup) // Output: true
}

NewNumericDate ​

go
func NewNumericDate(t time.Time) NumericDate

Creates a NumericDate from time.Time. NewNumericDate is typically used to construct timestamp claims explicitly (such as ExpiresAt, NotBefore) on RegisteredClaims.

Parameters ​

ParameterTypeDescription
ttime.TimeTime value

Returns ​

ReturnTypeDescription
dateNumericDateJWT numeric date

Example ​

go
package main

import (
	"fmt"
	"time"

	"github.com/cybergodev/jwt"
)

func main() {
	date := jwt.NewNumericDate(time.Unix(1700000000, 0))

	b, err := date.MarshalJSON()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(b)) // Output: 1700000000
}

NewRateLimiter ​

go
func NewRateLimiter(maxRate int, window time.Duration) *RateLimiter

Creates a token bucket rate limiter.

Parameters ​

ParameterTypeDescription
maxRateintMax requests per window (defaults to 100 when ≤0)
windowtime.DurationTime window (defaults to 1 minute when ≤0)

Returns ​

ReturnTypeDescription
limiter*RateLimiterRate limiter instance

Example ​

go
package main

import (
	"fmt"
	"time"

	"github.com/cybergodev/jwt"
)

func main() {
	limiter := jwt.NewRateLimiter(5, time.Minute)
	defer limiter.Close()

	for i := 1; i <= 6; i++ {
		fmt.Printf("request %d: %v\n", i, limiter.Allow("tenant-a"))
	}

	// Output:
	// request 1: true
	// request 2: true
	// request 3: true
	// request 4: true
	// request 5: true
	// request 6: false
}

Usage Notes ​

Most scenarios only need Config.EnableRateLimit; New() then builds a built-in limiter from RateLimitRate and RateLimitWindow. Call NewRateLimiter directly in only two cases:

  • A limiter is needed independently of a Processor;
  • A limiter must be injected through Config.RateLimiter to replace the default implementation.

Ownership

Processor.Close closes the limiter injected via Config.RateLimiter. Do not share one instance across processors that may outlive each other — after one closes it, the others' Allow always returns false.

AllowN(key, n) is an extension method on *RateLimiter that checks n requests at once. It returns false when n is negative or exceeds the maximum requests allowed per window, and true when n is 0.