
Security News
rv Is a New Rust-Powered Ruby Version Manager Inspired by Python's uv
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
@dwbinns/jwt
Advanced tools
Verify and create JWTs using async webcrypto.
ES256 (ECDSA with P-256 and P-256) and RS256 (RSA with SHA-256)
import { importJWK, create, verify, expiresTime, expiredFraction } from "@dwbinns/jwt";
const keys = await importJWK("ES256", "kid", {
"kty": "EC",
"crv": "P-256",
"x": "f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",
"y": "x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0",
"d": "jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI"
});
const jwt = await create(keys, {sub: "me", ...expiresTime(3600)});
console.log("JWT:", jwt);
console.log(`Expired: ${expiredFraction(jwt) * 100}%`);
const claims = await verify(keys, jwt);
console.log("Subject:", claims.sub);
console.log("Expires:", new Date(claims.exp * 1e3));
try {
await verify(keys, "eyJraWQiOiJraWQiLCJhbGciOiJFUzI1NiJ9.eyJzdWIiOiJtZSJ9.invalid");
} catch (e) {
console.log("Verification error:", e.message);
}
JWT: eyJraWQiOiJraWQiLCJhbGciOiJFUzI1NiJ9.eyJzdWIiOiJtZSIsImlhdCI6MTcxODY0MDk4NywiZXhwIjoxNzE4NjQ0NTg3fQ.fNZx8X2p7AFrlN7RSnr0n3rnxVjN6raBbDZhEbiWNomRFofG2KXFX8AwRHtXTa86VNxR8wOXnNVnly80IMP2CA
Expired: 0.004611111111111112%
Subject: me
Expires: 2024-06-17T17:16:27.000Z
Verification error: JWT not valid
function parse(text)
Synchronously parse a JWT without verification
import { parse } from "@dwbinns/jwt";
console.log(parse("eyJraWQiOiJraWQiLCJhbGciOiJFUzI1NiJ9.eyJzdWIiOiJtZSIsImlhdCI6MTcxODY0MDk4NywiZXhwIjoxNzE4NjQ0NTg3fQ.fNZx8X2p7AFrlN7RSnr0n3rnxVjN6raBbDZhEbiWNomRFofG2KXFX8AwRHtXTa86VNxR8wOXnNVnly80IMP2CA"));
Returns all parts of the JWT, decoded:
{
header: { kid: 'kid', alg: 'ES256' },
claims: { sub: 'me', iat: 1718640987, exp: 1718644587 },
signature: Uint8Array(64) [
124, 214, 113, 241, 125, 169, 236, 1, 107, 148, 222,
209, 74, 122, 244, 159, 122, 231, 197, 88, 205, 234,
182, 129, 108, 54, 97, 17, 184, 150, 54, 137, 145,
22, 135, 198, 216, 165, 197, 95, 192, 48, 68, 123,
87, 77, 175, 58, 84, 220, 81, 243, 3, 151, 156,
213, 103, 151, 47, 52, 32, 195, 246, 8
],
signed: 'eyJraWQiOiJraWQiLCJhbGciOiJFUzI1NiJ9.eyJzdWIiOiJtZSIsImlhdCI6MTcxODY0MDk4NywiZXhwIjoxNzE4NjQ0NTg3fQ'
}
async function verify(keys, text, now = new Date())
Parse and verify a JWT, throwing an error if the JWT is expired, not yet valid or does not have a valid signature. Keys should be an array of keys created via one of the import functions. Returns the JWT's claims, see first example.
async function importPem(alg, kid, pem)
Import a single key from a PEM file, supplying the algorithm (alg
) and key id (kid
) and pem as a string.
Supports pkcs8 private keys (BEGIN PRIVATE KEY
) and spki public keys (BEGIN PUBLIC KEY
)
function importHostJWKS(hostname)
Import all keys from a key set for a host
async function importURLJWKS(url)
Import all keys from a key set from a URL
async function importJWKS({ keys })
Import all keys from a key set
function expiresTime(durationSeconds)
Create claims that are issued now and expire in durationSeconds
function expiredFraction(jwt, createdAt, now = new Date())
Return what fraction of a JWT has expired.
Will be less than 0 if the JWT is not yet valid and more than 1 if it has expired.
Optionally supply a createdAt
Date object representing when the JWT was created.
If supplied then clock skew will not affect the result.
If not supplied the iat
field of the JWT will by used instead.
Supply a now
Date object to check the expiry status of a JWT at some other point of time.
async function create(keys, claims)
Create a JWT using the supplied keys
and claims
. See first example.
FAQs
Verify and create JWTs using async webcrypto.
We found that @dwbinns/jwt demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.