0.0.2 • Published 6 years ago
rest-express-jwt v0.0.2
Introduction
A restful compatible jwt authorization/authentication/user-system middleware for express.
It handle ALL the secure risk like jwt intercepted/stolen/leaking/forgery
Installation
npm install -S rest-express-jwtyarn add rest-express-jwtHow to use
const express = require('express');
const cookieParser = require('cookie-parser');
const secret = 'test.8e@af!g#';
const jwtAuth = require('../rest-express-jwt').auth({
mode: 'jwt-in-cookie',
secret: secret,
});
const jwtCreate = require('../rest-express-jwt').create({
mode: 'jwt-in-cookie',
secret: secret
});
const app = express();
app.use(cookieParser());
app.get('/user-info', jwtAuth, function (req, res, next) {
console.log(req.auth);
console.log(req.jwtid);
res.send('okay');
});
app.get('/login', function (req, res, next) {
let restjwt = jwtCreate({user: 'mock-user'}, {
expiresIn: 60 * 60,
issuer: 'goolyuyi.com',
notBefore: 0
});
res.cookie('jwt', restjwt.jwt, {httpOnly: true, sameSite: 'strict', secure: true});
res.json({jwtid_digest: restjwt.jwtid_digest});
});How it works
Schema: jwt-in-header
set a cookie session with
jwtid(a big random number) when user logincreate a
jwtwhen user login,setjwt.jwtid_digest = hash(session id)- response the
jwt, user agent should keep this in memory, likelocalStorageorsessionStorage) - request with
jwtinBearer Authenticationheader for every subsequent requests - verify
hash(jwtid)===jwt.jwtid_digest
FEATURE:
- this handle all risks in OWASP cheat sheet
- to prevent
XSSor intercepted/stolen the jwt, attacker impossible to retrieve thejwtid - to prevent
CSRFattack, the attacker impossible retrievejwtin user agent
RISKS:
- some information in
jwtmay be extract by attacker, if they intercepted/stolen thejwteven they are not able to use it.
Schema jwt-in-cookie:
- create a
jwtwhen user login,setjwt.jwtidwith a big random number. - set a cookie session
jwt - response with
jwtid_digest = hash(jwt.jwtid)when user login, user agent should keep this in memory, likelocalStorageorsessionStorage). - request with
jwtid_digest's value in header"jwtid_digest". - verify
hash(jwt.jwtid)===jwtid_digest
FEATURE:
- this handle all risks in OWASP cheat sheet
- to prevent
XSSor intercepted/stolen the jwt, attacker impossible to retrieve thejwt - to prevent
CSRFattack, the attacker impossible retrievejwtid_digestin user agent
RISKS:
- NONE
Schema Comparasion
| Schema | jwt-in-header | jwt-in-cookie |
|---|---|---|
| Cookie Stored | jwtid | jwt with jwt.jwtid |
| Client Stored | jwt with jwt.jwtid_digest | jwtid_digest |
| Client Header | authorization(with Bearer) | jwtid_digest |
| Verify Method | hash(jwtid) === jwt.jwtid_digest | hash(jwt.jwtid)===jwtid_digest |
Code:
jwt-in-header
req.headers.authorization
req.cookies.jwtidjwt-in-cookie
req.headers.jwtid_digest
req.cookies.jwtUpcoming
- jwt blacklist
- jwt local encrypt