koa-jsonwebtoken
Koa middleware to validate/revoke/refresh JSON Web Tokens. Sets ctx.state.user
(default) to token payload.
Install
$ npm install koa-jsonwebtoken
Retrieving the token
The token extraction strategy needs to be specified explicitly by setting opts.extractToken
. The module provides two strategies namely fromAuthorizationHeader
and fromCookies
. The token is normally provided in a HTTP header (Authorization
), but it can also be provided in a cookie by setting the opts.cookie
option to the name of the cookie that contains the token. Custom token retrieval function should match the following interface:
/** * Your custom token resolver * @param * @param * @return */
Passing the secret
A single shared secret needs to be specified explicitly in opts.secret
.
Check for revocation
In case you maintain a blacklist for the purpose of token revokation, you can specify a custom function to check for revocation by setting opts.checkRevoked
. The function should match the following interface:
/** * Your custom token revocation check function * @param * @param * @return */
Refresh token
To refresh your tokens in case they are expired and do so in a manner transparent to the user, you can set opts.refreshCookie
to the name of the cookie where the server will expect the refresh token. You also need to specify opts.doRefresh
with a custom function which will carry out the refresh logic/validate the refresh token. The function should match the following interface:
/** * Your custom token refresh function * @param * @param * @param * @param * @return */
Note: ctx.state[opts.key]
is set to the stale payload after your custom function returns. Don't modify critical payload fields if you depend on them in your application.
Example
;;// const jwt = require('koa-jsonwebtoken').default;// const fromAuthorizationHeader = require('koa-jsonwebtoken').fromAuthorizationHeader;const app = ; // Custom 401 handling if you don't want to expose koa-jsonwebtoken errors to usersapp; // Unprotected middlewareapp; // Middleware below this line is only reached if JWT token is validapp; // Protected middlewareapp; app;
Alternatively you can conditionally run the jwt
middleware under certain conditions:
;;const app = ; // Middleware below this line is only reached if JWT token is valid// unless the URL starts with '/public'app; // Unprotected middlewareapp; // Protected middlewareapp; app;
For more information on unless
exceptions, check koa-unless.
If you prefer to use another ctx key for the decoded data, just pass in key
, like so:
app;
This makes the decoded data available as ctx.state.jwtdata
.
You can specify options to control the verification as well. See node-jsonwebtoken for more options:
app;
If the JWT has an expiration (exp
), it will be checked.
This module also support tokens signed with public/private key pairs. Instead of a secret, you can specify a Buffer with the public key:
var publicKey = fs;app;
Related Modules
- jsonwebtoken — JSON Web Token signing and verification
Note that koa-jsonwebtoken exports the sign
, verify
and decode
functions from the above module as a convenience.
Test Server
$ npm install
$ npm run test
Author
Sid Jain
Credits
This code is largely based on: koa-jwt (unmaintained).