wow
This commit is contained in:
+47
-26
@@ -1,7 +1,7 @@
|
||||
# jsonwebtoken
|
||||
|
||||
| **Build** | **Dependency** |
|
||||
|-----------|---------------|
|
||||
| **Build** | **Dependency** |
|
||||
|-----------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|
|
||||
| [](http://travis-ci.org/auth0/node-jsonwebtoken) | [](https://david-dm.org/auth0/node-jsonwebtoken) |
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ $ npm install jsonwebtoken
|
||||
|
||||
# Migration notes
|
||||
|
||||
* [From v8 to v9](https://github.com/auth0/node-jsonwebtoken/wiki/Migration-Notes:-v8-to-v9)
|
||||
* [From v7 to v8](https://github.com/auth0/node-jsonwebtoken/wiki/Migration-Notes:-v7-to-v8)
|
||||
|
||||
# Usage
|
||||
@@ -32,15 +33,16 @@ $ npm install jsonwebtoken
|
||||
|
||||
> If `payload` is not a buffer or a string, it will be coerced into a string using `JSON.stringify`.
|
||||
|
||||
`secretOrPrivateKey` is a string, buffer, or object containing either the secret for HMAC algorithms or the PEM
|
||||
`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](https://nodejs.org/api/crypto.html#crypto_sign_sign_private_key_output_format)), 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 [zeit/ms](https://github.com/zeit/ms).
|
||||
* `expiresIn`: expressed in seconds or a string describing a time span [vercel/ms](https://github.com/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 [zeit/ms](https://github.com/zeit/ms).
|
||||
* `notBefore`: expressed in seconds or a string describing a time span [vercel/ms](https://github.com/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`
|
||||
@@ -50,6 +52,8 @@ encoded private key for RSA and ECDSA. In case of a private key with passphrase
|
||||
* `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 option is intended only for backwards compatability and should be avoided.
|
||||
|
||||
|
||||
|
||||
@@ -73,7 +77,7 @@ Synchronous Sign with RSA SHA256
|
||||
```js
|
||||
// sign with RSA SHA256
|
||||
var privateKey = fs.readFileSync('private.key');
|
||||
var token = jwt.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256'});
|
||||
var token = jwt.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256' });
|
||||
```
|
||||
|
||||
Sign asynchronously
|
||||
@@ -125,9 +129,11 @@ jwt.sign({
|
||||
|
||||
(Synchronous) If a callback is not supplied, function acts synchronously. Returns the payload decoded if the signature is valid and optional expiration, audience, or issuer are valid. If not, it will throw the error.
|
||||
|
||||
> __Warning:__ When the token comes from an untrusted source (e.g. user input or external requests), the returned decoded payload should be treated like any other user input; please make sure to sanitize and only work with properties that are expected
|
||||
|
||||
`token` is the JsonWebToken string
|
||||
|
||||
`secretOrPublicKey` is a string or buffer containing either the secret for HMAC algorithms, or the PEM
|
||||
`secretOrPublicKey` is a string (utf-8 encoded), buffer, or KeyObject containing either the secret for HMAC algorithms, or the PEM
|
||||
encoded public key for RSA and ECDSA.
|
||||
If `jwt.verify` is called asynchronous, `secretOrPublicKey` can be a function that should fetch the secret or public key. See below for a detailed example
|
||||
|
||||
@@ -135,20 +141,26 @@ As mentioned in [this comment](https://github.com/auth0/node-jsonwebtoken/issues
|
||||
|
||||
`options`
|
||||
|
||||
* `algorithms`: List of strings with the names of the allowed algorithms. For instance, `["HS256", "HS384"]`.
|
||||
* `algorithms`: List of strings with the names of the allowed algorithms. For instance, `["HS256", "HS384"]`.
|
||||
> If not specified a defaults will be used based on the type of key provided
|
||||
> * secret - ['HS256', 'HS384', 'HS512']
|
||||
> * rsa - ['RS256', 'RS384', 'RS512']
|
||||
> * ec - ['ES256', 'ES384', 'ES512']
|
||||
> * default - ['RS256', 'RS384', 'RS512']
|
||||
* `audience`: if you want to check audience (`aud`), provide a value here. The audience can be checked against a string, a regular expression or a list of strings and/or regular expressions.
|
||||
> Eg: `"urn:foo"`, `/urn:f[o]{2}/`, `[/urn:f[o]{2}/, "urn:bar"]`
|
||||
* `complete`: return an object with the decoded `{ payload, header, signature }` instead of only the usual content of the payload.
|
||||
* `issuer` (optional): string or array of strings of valid values for the `iss` field.
|
||||
* `jwtid` (optional): if you want to check JWT ID (`jti`), provide a string value here.
|
||||
* `ignoreExpiration`: if `true` do not validate the expiration of the token.
|
||||
* `ignoreNotBefore`...
|
||||
* `subject`: if you want to check subject (`sub`), provide a value here
|
||||
* `clockTolerance`: number of seconds to tolerate when checking the `nbf` and `exp` claims, to deal with small clock differences among different servers
|
||||
* `maxAge`: the maximum allowed age for tokens to still be valid. It is expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms).
|
||||
* `maxAge`: the maximum allowed age for tokens to still be valid. It is expressed in seconds or a string describing a time span [vercel/ms](https://github.com/vercel/ms).
|
||||
> Eg: `1000`, `"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"`).
|
||||
* `clockTimestamp`: the time in seconds that should be used as the current time for all necessary comparisons.
|
||||
* `nonce`: if you want to check `nonce` claim, provide a string value here. It is used on Open ID for the ID Tokens. ([Open ID implementation notes](https://openid.net/specs/openid-connect-core-1_0.html#NonceNotes))
|
||||
|
||||
* `allowInvalidAsymmetricKeyTypes`: if true, allows asymmetric keys which do not match the specified algorithm. This option is intended only for backwards compatability and should be avoided.
|
||||
|
||||
```js
|
||||
// verify a token symmetric - synchronous
|
||||
@@ -228,12 +240,18 @@ jwt.verify(token, getKey, options, function(err, decoded) {
|
||||
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary><em></em>Need to peek into a JWT without verifying it? (Click to expand)</summary>
|
||||
|
||||
### jwt.decode(token [, options])
|
||||
|
||||
(Synchronous) Returns the decoded payload without verifying if the signature is valid.
|
||||
|
||||
> __Warning:__ This will __not__ verify whether the signature is valid. You should __not__ use this for untrusted messages. You most likely want to use `jwt.verify` instead.
|
||||
|
||||
> __Warning:__ When the token comes from an untrusted source (e.g. user input or external request), the returned decoded payload should be treated like any other user input; please make sure to sanitize and only work with properties that are expected
|
||||
|
||||
|
||||
`token` is the JsonWebToken string
|
||||
|
||||
`options`:
|
||||
@@ -253,6 +271,8 @@ console.log(decoded.header);
|
||||
console.log(decoded.payload)
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
## Errors & Codes
|
||||
Possible thrown errors during verification.
|
||||
Error is the first argument of the verification callback.
|
||||
@@ -286,7 +306,8 @@ Error object:
|
||||
|
||||
* name: 'JsonWebTokenError'
|
||||
* message:
|
||||
* 'jwt malformed'
|
||||
* 'invalid token' - the header or payload could not be parsed
|
||||
* 'jwt malformed' - the token does not have three components (delimited by a `.`)
|
||||
* 'jwt signature is required'
|
||||
* 'invalid signature'
|
||||
* 'jwt audience invalid. expected: [OPTIONS AUDIENCE]'
|
||||
@@ -335,21 +356,21 @@ jwt.verify(token, 'shhhhh', function(err, decoded) {
|
||||
|
||||
Array of supported algorithms. The following algorithms are currently supported.
|
||||
|
||||
alg Parameter Value | Digital Signature or MAC Algorithm
|
||||
----------------|----------------------------
|
||||
HS256 | HMAC using SHA-256 hash algorithm
|
||||
HS384 | HMAC using SHA-384 hash algorithm
|
||||
HS512 | HMAC using SHA-512 hash algorithm
|
||||
RS256 | RSASSA-PKCS1-v1_5 using SHA-256 hash algorithm
|
||||
RS384 | RSASSA-PKCS1-v1_5 using SHA-384 hash algorithm
|
||||
RS512 | RSASSA-PKCS1-v1_5 using SHA-512 hash algorithm
|
||||
PS256 | RSASSA-PSS using SHA-256 hash algorithm (only node ^6.12.0 OR >=8.0.0)
|
||||
PS384 | RSASSA-PSS using SHA-384 hash algorithm (only node ^6.12.0 OR >=8.0.0)
|
||||
PS512 | RSASSA-PSS using SHA-512 hash algorithm (only node ^6.12.0 OR >=8.0.0)
|
||||
ES256 | ECDSA using P-256 curve and SHA-256 hash algorithm
|
||||
ES384 | ECDSA using P-384 curve and SHA-384 hash algorithm
|
||||
ES512 | ECDSA using P-521 curve and SHA-512 hash algorithm
|
||||
none | No digital signature or MAC value included
|
||||
| alg Parameter Value | Digital Signature or MAC Algorithm |
|
||||
|---------------------|------------------------------------------------------------------------|
|
||||
| HS256 | HMAC using SHA-256 hash algorithm |
|
||||
| HS384 | HMAC using SHA-384 hash algorithm |
|
||||
| HS512 | HMAC using SHA-512 hash algorithm |
|
||||
| RS256 | RSASSA-PKCS1-v1_5 using SHA-256 hash algorithm |
|
||||
| RS384 | RSASSA-PKCS1-v1_5 using SHA-384 hash algorithm |
|
||||
| RS512 | RSASSA-PKCS1-v1_5 using SHA-512 hash algorithm |
|
||||
| PS256 | RSASSA-PSS using SHA-256 hash algorithm (only node ^6.12.0 OR >=8.0.0) |
|
||||
| PS384 | RSASSA-PSS using SHA-384 hash algorithm (only node ^6.12.0 OR >=8.0.0) |
|
||||
| PS512 | RSASSA-PSS using SHA-512 hash algorithm (only node ^6.12.0 OR >=8.0.0) |
|
||||
| ES256 | ECDSA using P-256 curve and SHA-256 hash algorithm |
|
||||
| ES384 | ECDSA using P-384 curve and SHA-384 hash algorithm |
|
||||
| ES512 | ECDSA using P-521 curve and SHA-512 hash algorithm |
|
||||
| none | No digital signature or MAC value included |
|
||||
|
||||
## Refreshing JWTs
|
||||
|
||||
|
||||
Reference in New Issue
Block a user