jwt
Version, currently main branch1 version
- main branchlatestJan 14, 2026
github.com/84codes/jwt.cr
JWT (JSON Web Token) library with RS256 signature verification and JWKS support.
1 stars
0 dependents
License: Apache-2.0
Installation
# Add this to your shard.yml
dependencies:
jwt:
github: 84codes/jwt.cr
branch: mainmain is a branch, not a release, so this tracks it rather than pinning a version.
Then run:
shards installshard.yml
- Crystal
>= 1.0.0- License
- Apache-2.0
- Author
- Christina Dahlén
Dependencies
This version declares no dependencies.
README
JWT
A Crystal library for JWT verification with RS256 and automatic JWKS fetching from OIDC providers.
Features
- RS256 signature verification
- Automatic JWKS fetching and caching with OIDC discovery
- Standard claims validation (
exp,iat,nbf,iss,aud) - Thread-safe public key caching
- Automatic JWKS refresh with configurable TTL
Installation
Add this to your application's shard.yml:
dependencies:
jwt:
github: 84codes/jwt.cr
Then run:
shards install
Usage
Basic JWT Verification with JWKS
require "jwt"
# Create JWKS fetcher and start background refresh
fetcher = JWT::JWKSFetcher.new(
issuer_url: "https://auth.example.com",
default_cache_ttl: 1.hour
)
spawn { fetcher.refresh_loop }
# Configure and create verifier
config = JWT::VerifierConfig.new(
expected_issuer: "https://auth.example.com",
expected_audience: "my-api",
verify_audience: true
)
verifier = JWT::Verifier.new(config, fetcher.public_keys)
# Verify tokens
token = verifier.verify(jwt_string)
puts token.payload["sub"]
The JWKS fetcher automatically:
- Fetches
{issuer_url}/.well-known/openid-configuration - Fetches public keys from the
jwks_uri - Refreshes keys based on
Cache-Controlheaders ordefault_cache_ttl
Manual Decoding with Public Key
require "jwt"
# With verification
token = JWT::RS256Parser.decode(jwt_string, public_key_pem)
# Without verification (testing only)
token = JWT::RS256Parser.decode(jwt_string, "", verify: false)
Configuration
# VerifierConfig
config = JWT::VerifierConfig.new(
expected_issuer: "https://auth.example.com",
expected_audience: "my-api",
verify_audience: true,
time_tolerance: 200.milliseconds # Clock skew tolerance for iat validation
)
# JWKSFetcher
fetcher = JWT::JWKSFetcher.new(
issuer_url: "https://auth.example.com",
default_cache_ttl: 1.hour # Used if no Cache-Control header
)
# Stop the refresh loop gracefully
fetcher.stop
# Manually trigger a refresh
fetcher.trigger_refresh
Security
- Only RS256 algorithm accepted (prevents algorithm confusion attacks)
- Validates
exp,iat,nbftime claims - Optional
issandaudclaim validation - Supports multiple keys with
kid(Key ID) lookup - Thread-safe key caching
Testing
crystal spec
License
Apache License 2.0
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This branch
- Branch
main- Seen
- Jan 14, 2026
- Crystal
>= 1.0.0- Indexed
- yes
Dependents
No indexed shard depends on this one yet.
Repository
github.com/84codes/jwt.cr
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 15, 2026
- Synced
- Aug 14, 2026
- Versions
- 1