Web Crypto API wrapper for working with JSON Web Tokens (JWT) in browsers and workers.
npm install jwebtOr
yarn add jwebtCreate a JSON Web Token.
Options should be passed as an object with the following properties:
All values within the payload are optional. For more information on properties to use see the latest JSON Web Token (JWT) Proposed Standard.
Typically the contents of a .pem file. The header, footer and line breaks will be handled by the library. If privateKey and secret are provided, privateKey will be used.
A secret key. If privateKey and secret are provided, privateKey will be used.
An optional string used as a hint to indicate which key was used to secure the token.
A string describing the data format of the key to import. Only 'pkcs8' is supported at the moment.
Only 'RS256' is supported at the moment.
A boolean indicating whether it will be possible to export the key. In general this is not required for JWT signing and verifying.
An array indicating what can be done with the key. The string 'sign' is required within the array.
subtleCrypto: SubtleCrypto (default = window?.crypto?.subtle)
Allows the SubtleCrypto interface to be passed through in case window is not available. Useful when working with platforms such as Cloudflare Workers, where crypto.subtle can be used.
Returns a Promise which resolves to a JSON Web Token string:
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjk0NjY4ODQwMH0.c2VjcmV0import{sign}from'jwebt';constcurrentTimeInSeconds=Math.floor(Date.now()/1000);// All values in payload are optional// The token verifier should specify requirementsconstpayload={// Common propertiesiss: 'me@example.com',sub: 'me@example.com',aud: 'https://api.example.com/',// ['orArrayOfStrings'],exp: currentTimeInSeconds+(60*60),// now + 1 hournbf: currentTimeInSeconds,iat: currentTimeInSeconds,jti: 'uniqueTokenId',// Anything elseanotherValue: 'anythingGoes',};// Use privateKey or secretconstprivateKey='-----BEGIN PRIVATE KEY-----\nexample\n-----END PRIVATE KEY-----\n';constsecret='secretKey';constkeyId='optionalUniqueKeyIdentifier';constjwt=awaitsign({
payload,
privateKey,
secret,
keyId,// default values belowformat: 'pkcs8',algorithm: 'RS256',extractable: false,keyUsages: ['sign'],subtleCrypto: window.crypto.subtle,});console.log(jwt);// 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjk0NjY4ODQwMH0.c2VjcmV0'Not yet implemented.
Decode a JSON Web Token.
Options should be passed as an object with the following property:
The default behaviour is to return the decoded payload object. When complete is true it will return an object containing header, payload and signature properties.
import{decode}from'jwebt';constjwt='eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjk0NjY4ODQwMH0.c2VjcmV0';constdecodedJwt=decode(jwt,{complete: false});console.log(decodedJwt);// { 'exp': 946688400 }constcompleteJwt=decode(jwt,{complete: true});console.log(completeJwt);/*{ header: { typ: 'JWT', alg: 'RS256', }, payload: { exp: 946688400, }, signature: 'c2VjcmV0'}*/Options should be passed as an object with the following property:
Allows adjustments to the expiry time check to help identify tokens which are about to expire.
import{isExpired}from'jwebt';constjwt='eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjk0NjY4ODQwMH0.c2VjcmV0';constjwtExpired=isExpired(jwt,{expiredWithinSeconds: 5});console.log(jwtExpired);// truenpm testThis project is MIT licensed.