JSONWebToken API
An implementation of JSON Web Tokens.This was developed against draft-ietf-oauth-json-web-token-08. It makes use of node-jws.
安装
|
|
版本迁移
From v7 to v8用法API
|
|
(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. Please note that exp is only set if the payload is an object literal.
secretOrPrivateKey is a string, buffer, or object 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.
- sign with default (HMAC SHA256)
|
|
- sign with RSA SHA256
|
|
TOKEN失效
The standard for JWT defines an exp claim for expiration. The expiration is represented as a NumericDate:
A JSON numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds. This is equivalent to the IEEE Std 1003.1, 2013 Edition [POSIX.1] definition “Seconds Since the Epoch”, in which each day is accounted for by exactly 86400 seconds, other than that non-integer values can be represented. See RFC 3339 [RFC3339] for details regarding date/times in general and UTC in particular.
This means that the exp field should contain the number of seconds since the epoch.
产生一个token1小时后失效:
12345jwt.sign({exp: Math.floor(Date.now() / 1000) + (60 * 60),data: 'foobar'}, 'secret');另外一个方式产生token并且有失效机制
12345678910jwt.sign({data: 'foobar'}, 'secret', { expiresIn: 60 * 60 });//or even better:jwt.sign({data: 'foobar'}, 'secret', { expiresIn: '1h' });
验证token
token是一个JsonWebToken字符窜
(Asynchronous) If a callback is supplied, function acts asynchronously. The callback is called with the decoded payload if the signature is valid and optional expiration, audience, or issuer are valid. If not, it will be called with the error.
(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.
secretOrPublicKey
是一个字符窜或者HMAC algorithms
缓冲区中的任何一个PEM编码了RSA和ECDSA的公钥。
|
|
解码token
|
|
token is the JsonWebToken string
options:
json: force JSON.parse on the payload even if the header does not contain “typ”:”JWT”.
- complete: return an object with the decoded payload and header.
Example
|
|
token失效异常
TokenExpiredError
- Thrown error if the token is expired.
- Error object:
- name: ‘TokenExpiredError’
- message: ‘jwt expired’
- expiredAt: [ExpDate]
|
|
JsonWebTokenError
- Error object:
- name: JsonWebTokenError
- message:
- ‘jwt malformed’
- ‘jwt signature is required’
- ‘invalid signature’
- ‘jwt audience invalid. expected: [OPTIONS AUDIENCE]’
- ‘jwt issuer invalid. expected: [OPTIONS ISSUER]’
- ‘jwt id invalid. expected: [OPTIONS JWT ID]’
- ‘jwt subject invalid. expected: [OPTIONS SUBJECT]’
|
|