JWT::JWKS
Inherits Reference < Object
JWKS (JSON Web Key Set) helper for JWT validation with support for OIDC discovery
Supports:
- Fetching OIDC metadata from /.well-known/openid-configuration
- Fetching and caching JWKS keys
- Local JWT validation (for service-to-service tokens)
- Remote JWT validation via JWKS
Example:
# Initialize with optional local keys
jwks = JWT::JWKS.new(
local_keys: {"local_key_id" => "secret"},
local_algorithm: JWT::Algorithm::HS256
)
# Validate a token and check scopes
payload = jwks.validate(token, issuer: "https://example.com", audience: "my-app")
if payload
scopes = JWT::JWKS.extract_scopes(payload)
if scopes.includes?("read")
# User has required scope
end
end
Constants
Allowed algorithms for JWKS validation (interoperable with real-world JWKS endpoints) "none" is explicitly excluded for security
Default cache TTL (10 minutes)
Default leeway for time-based claims (60 seconds)
Constructors
Initialize JWKS validator
@param local_keys Optional hash of kid => key for local JWT validation @param local_algorithm Algorithm to use for local keys @param cache_ttl Cache TTL for OIDC metadata and JWKS (default: 10 minutes) @param leeway Clock skew leeway for time-based claims (default: 60 seconds)
Class methods
Extract roles from JWT payload
Checks "roles" (Azure AD), "realm_access.roles" (Keycloak realm roles), "resource_access" (Keycloak client roles), and "groups" (Okta) claims
Extract scopes from JWT payload
Checks "scp" (Entra/Azure AD), "scope" (standard), and "permissions" (Auth0) claims Handles both space-delimited strings and arrays
Validate roles in a JWT payload
@param payload JWT payload @param required_roles Required roles (checks for "roles" claim) @return true if all required roles are present
Validate scopes in a JWT payload
@param payload JWT payload @param required_scopes Required scopes (checks for "scp" claim) @return true if all required scopes are present
Instance methods
Fetch JWKS from a jwks_uri
@param jwks_uri JWKS URI @param force_refresh Force refresh even if cached (used for key rotation) @return JWKS key set
Fetch OIDC metadata for an issuer
@param issuer Issuer URL (e.g., "https://login.microsoftonline.com/{tenant}/v2.0") @return OIDC metadata
Validate a JWT token
This method will:
- Try to validate using local keys if provided
- Fall back to JWKS validation if not a local token
@param token JWT token string @param issuer Expected issuer (for OIDC metadata lookup) @param audience Expected audience(s) for validation @param validate_claims Whether to validate standard claims (exp, nbf, etc.) @return Validated payload or nil if validation fails
Example:
payload = jwks.validate(token, issuer: "https://example.com", audience: "my-app")
if payload
# Check scopes
scopes = JWT::JWKS.extract_scopes(payload)
if scopes.includes?("read")
# Token is valid with required scope
end
end