auth0 /node-jsonwebtoken

JsonWebToken implementation for node.js http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html

★ 18,183⑂ 1,294👁 246⑃ 341● 661▤ 0
JavaScriptMITCreated 2013-07-01
+0
日增长
18,183
stars
1,294
forks
246
watchers
341
prs
661
issues
0
releases

README

jsonwebtoken

| Build                                                                                                                               | Dependency                                                                                                         |
|-----------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|
|  |  |


An implementation of JSON Web Tokens.

This was developed against draft-ietf-oauth-json-web-token-08. It makes use of node-jws

 Install

bash
$ npm install jsonwebtoken


 Migration notes

 From v8 to v9
 From v7 to v8

 Usage

 jwt.sign(payload, secretOrPrivateKey, [options, callback])

(Asynchronous) If a callback is supplied, the callback is called with the err or the JWT.

(Synchronous) Returns the JsonWebToken as string

payload could be an object literal, buffer or string representing valid JSON. 
 Please _note_ that exp or any other claim is only set if the payload is an object literal. Buffer or string payloads are not checked for JSON validity.

 If payload is not a buffer or a string, it will be coerced into a string using JSON.stringify.

secretOrPrivateKey is a string (utf-8 encoded), buffer, object, or KeyObject containing either the secret for HMAC algorithms or the PEM
encoded private key for RSA and ECDSA. In case of a private key with passphrase an object { key, passphrase } can be used (based on crypto documentation), in this case be sure you pass the algorithm option.
When signing with RSA algorithms the minimum modulus length is 2048 except when the allowInsecureKeySizes option is set to true. Private keys below this size will be rejected with an error.

options:

 algorithm (default: HS256)
 expiresIn: expressed in seconds or a string describing a time span vercel/ms. 
   Eg: 60, "2 days", "10h", "7d". A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default ("120" is equal to "120ms").
 notBefore: expressed in seconds or a string describing a time span vercel/ms. 
   Eg: 60, "2 days", "10h", "7d". A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default ("120" is equal to "120ms").
 audience
 issuer
 jwtid
 subject
 noTimestamp
 header
 keyid
 mutatePayload: if true, the sign function will modify the payload object directly. This is useful if you need a raw reference to the payload after claims have been applied to it but before it has been encoded into a token.
 allowInsecureKeySizes: if true allows private keys with a modulus below 2048 to be used for RSA
 allowInvalidAsymmetricKeyTypes: if true, allows asymmetric keys which do not match the specified algorithm. This

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON