@lykmapipo/jwt-common 0.4.1
Helper utilities for day to day jwt usage.
merge provided options with defaults
Name | Type | Description | |
---|---|---|---|
optns | Object |
provided options | Optional |
const { withDefaults } = require('@lykmapipo/jwt-common');
withDefaults({ secret: 'xo67Rw' }) // => { secret: 'xo67Rw', ...}
Object
merged options with environment variables
encode given payload as jwt.
Name | Type | Description | |
---|---|---|---|
payload | Object |
data to encode. | |
opts | Object |
jwt sign or encoding options. | Optional |
cb | Function |
callback to invoke on success or failure. |
const { encode } = require('@lykmapipo/jwt-common');
const payload = { _id: 'xo5', permissions: ['user:read'] };
// encode with default options
encode(payload, (error, jwt) => { ... });
// encode with merged options
encode(payload, { secret: 'xo67Rw' }, (error, jwt) => { ... });
String
Error
jwt token if success or error.
decode and verify given jwt.
Name | Type | Description | |
---|---|---|---|
token | String |
jwt token to decode. | |
opts | Object |
jwt verify or decoding options. | Optional |
cb | Function |
callback to invoke on success or failure. |
const { decode } = require('@lykmapipo/jwt-common');
const token = 'eyJhbGciOiJIUz...';
// decode with default options
decode(token, (error, payload) => { ... });
// decode with provided options
decode(token, { secret: 'xo67Rw' }, (error, payload) => { ... });
Payload
Error
payload if success or error.
decode a given jwt, if expired return new jwt.
Name | Type | Description | |
---|---|---|---|
token | String |
jwt token to refresh. | |
payload | Object |
data to encode. | |
opts | Object |
jwt verify or decoding options. | Optional |
cb | Function |
callback to invoke on success or failure. |
const { refresh } = require('@lykmapipo/jwt-common');
const token = 'eyJhbGciOiJIUz...';
const payload = { _id: 'xo5', permissions: ['user:read'] };
// refresh with default options
refresh(token, payload, (error, jwt) => { ... });
// refresh with provided options
refresh(token, payload, { secret: 'xo67Rw' }, (error, jwt) => { ... });
String
Error
jwt token if success or error.
check if jwt expired without verifying if the signature is valid.
Name | Type | Description | |
---|---|---|---|
token | String |
jwt token to check for expiry. | |
opts | Object |
jwt verify or decoding options. | Optional |
cb | Function |
callback to invoke on success or failure. | Optional |
const { isExpired } = require('@lykmapipo/jwt-common');
const token = 'eyJhbGciOiJIUz...';
// isExpired with default options
isExpired(token); //=> false
// isExpired with provided options
const optns = { clockTimestamp : Math.floor(Date.now() / 1000) }
isExpired(token, optns); //=> true
Boolean
whether jwt expired.
return a function used to decode jwt to user.
Name | Type | Description | |
---|---|---|---|
opts | Object |
decoding options. | Optional |
opts.user | Functon |
Optional |
Function
jwt to user decoder
parse request headers to get jwt.
Name | Type | Description | |
---|---|---|---|
done | Function |
callback to invoke on success or failure. |
const { parseJwtFromHttpHeaders } = require('@lykmapipo/jwt-common');
parseJwtFromHttpHeaders(request, (error, jwt) => { ... });
Payload
Error
jwt if success or error.
parse request headers to get jwt.
Name | Type | Description | |
---|---|---|---|
done | Function |
callback to invoke on success or failure. |
const { parseJwtFromHttpQueryParams } = require('@lykmapipo/jwt-common');
parseJwtFromHttpQueryParams(request, (error, jwt) => { ... });
Payload
Error
jwt if success or error.
parse request headers to get jwt.
Name | Type | Description | |
---|---|---|---|
done | Function |
callback to invoke on success or failure. |
const { parseJwtFromHttpRequest } = require('@lykmapipo/jwt-common');
parseJwtFromHttpRequest(request, (error, jwt) => { ... });
Payload
Error
jwt if success or error.
create middlware to authorize request using jwt
Name | Type | Description | |
---|---|---|---|
opts | Object |
jwt verify or decoding options. | Optional |
const { jwtAuth } = require('@lykmapipo/jwt-common');
app.get('/users', jwtAuth({ secret: 'xo67Rw' }), (req, res, next) => { ... });
Function
express compactoble middleware.
create middlware to check request for jwt permissions(or scopes).
Name | Type | Description | |
---|---|---|---|
requiredScopes | Array.<String> String |
required scopes or permissions. |
const { jwtPermit } = require('@lykmapipo/jwt-common');
app.get('/users', jwtPermit('user:read'), (req, res, next) => { ... });
Function
express compactoble middleware.
Documentation generated with doxdox.