Skip to content

Getting Started

Installation

bash
go get github.com/cybergodev/jwt

Requires Go 1.25+.

Basic Usage

1. Create Processor

go
package main

import (
    "fmt"
    "time"

    "github.com/cybergodev/jwt"
)

func main() {
    cfg := jwt.DefaultConfig()
    cfg.SecretKey = "hmac-key-that-has-at-least-32-bytes!" // HMAC requires at least 32 bytes
    cfg.AccessTokenTTL = 15 * time.Minute
    cfg.RefreshTokenTTL = 7 * 24 * time.Hour

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

2. Issue Token

go
claims := &jwt.Claims{
    UserID:   "user123",
    Username: "alice",
    Role:     "admin",
    Permissions: []string{"read", "write"},
}

// Access token (short-lived)
accessToken, err := processor.Create(claims)
if err != nil {
    panic(err)
}

// Refresh token (long-lived)
refreshToken, err := processor.CreateRefresh(claims)
if err != nil {
    panic(err)
}

3. Validate Token

go
parsed, valid, err := processor.Validate(accessToken)
if err != nil {
    // Handle error: expired, invalid signature, etc.
    panic(err)
}
if valid {
    fmt.Println("UserID:", parsed.UserID)
    fmt.Println("Role:", parsed.Role)
    fmt.Println("ExpiresAt:", parsed.ExpiresAt.Time)
}

4. Refresh Token

go
newAccessToken, err := processor.Refresh(refreshToken)
if err != nil {
    panic(err)
}
fmt.Println("New Access Token:", newAccessToken)

5. Revoke Token

go
// Add token to blacklist
err := processor.Revoke(accessToken)
if err != nil {
    panic(err)
}

// Check if revoked
revoked, err := processor.IsRevoked(accessToken)
if err != nil {
    panic(err)
}
fmt.Println("Revoked:", revoked) // true

More Features

The steps above cover the core token lifecycle. CyberGo JWT also provides the following features — click into each guide for detailed usage:

FeatureDescriptionGuide
Signing AlgorithmsHMAC, RSA, RSA-PSS, ECDSA — 12 algorithms across 4 familiesSigning Algorithms
Custom ClaimsDefine business-specific fields via the CustomClaims interfaceCustom Claims
Token Refresh & RotationTwo-tier token TTL design, reuse vs. one-time rotation strategiesToken Refresh & Rotation
Token BlacklistRevocation, built-in memory store, and Redis custom backendsToken Blacklist
Rate LimitingToken bucket algorithm to prevent abuse of issuance endpointsRate Limiting
ConfigurationIssuer/audience validation, clock skew, mandatory expiration, input validationConfiguration
Error Handling19 sentinel error categories and errors.Is matchingError Handling
Testing & Clock InjectionFixedClock for deterministic, sleep-free time control in testsTesting & Clock Injection

Next Steps