npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rest-express-jwt

v0.0.2

Published

a restful compatible jwt authorization middleware for express.

Downloads

3

Readme

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-jwt
yarn add rest-express-jwt

How 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

  1. set a cookie session with jwtid(a big random number) when user login

  2. create a jwt when user login,set jwt.jwtid_digest = hash(session id)

  3. response the jwt, user agent should keep this in memory, like localStorage or sessionStorage)

  4. request with jwt in Bearer Authentication header for every subsequent requests

  5. verify hash(jwtid)===jwt.jwtid_digest

FEATURE:

  • this handle all risks in OWASP cheat sheet
  • to prevent XSS or intercepted/stolen the jwt, attacker impossible to retrieve the jwtid
  • to prevent CSRF attack, the attacker impossible retrieve jwt in user agent

RISKS:

  • some information in jwt may be extract by attacker, if they intercepted/stolen the jwt even they are not able to use it.

Schema jwt-in-cookie:

  1. create a jwt when user login,set jwt.jwtid with a big random number.
  2. set a cookie session jwt
  3. response with jwtid_digest = hash(jwt.jwtid) when user login, user agent should keep this in memory, like localStorage or sessionStorage).
  4. request with jwtid_digest's value in header "jwtid_digest".
  5. verify hash(jwt.jwtid)===jwtid_digest

FEATURE:

  • this handle all risks in OWASP cheat sheet
  • to prevent XSS or intercepted/stolen the jwt, attacker impossible to retrieve the jwt
  • to prevent CSRF attack, the attacker impossible retrieve jwtid_digest in 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.jwtid

jwt-in-cookie

req.headers.jwtid_digest
req.cookies.jwt

Upcoming

  • jwt blacklist
  • jwt local encrypt