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

shelf-jwt-sessions

v0.1.1

Published

A simple tool for defining user session model classes in redis using shelf and jwt

Downloads

7

Readme

shelf-sessions

shelf Logo

Define user session model classes with ttl in redis using jwt and shelf.

Build Status npm version

Introduction

For when you need to store volatile user sessions, with all that useful info you need, but don't want to go through the pain of creating special cron jobs to clear them. Creating new sessions should be easy and authenticating requests should be fast, so, here you go, jwt + redis, perfect combo!

Shelf sessions uses joi for schema validation and node-jsonwebtoken lib for issuing tokens and validating them.

Example

const ShelfSessions = require('shelf-jwt-sessions')
const Crypto = require('crypto')
const Joi = require('joi')

const secret = Crypto.randomBytes(128)

let MyShelf = ShelfSessions('test', secret, {
  algorithm: 'HS256',
  subject: 'yolo',
  issuer: 'me'
})

let MyModel = MyShelf.extend({
  name: 'basic-user',
  props: {
    userAgent: Joi.string()
  }
})

MyModel.createSession({
  userId: 'me',
  scopes: ['default'],
  userAgent: 'stuff from the header'
}, (err, result) => {
  if (err) throw err
  MyModel.authenticate(result.jwt, console.log)
})

API

ShelfSessions(name, secretOrPrivateKey, [options])

Initiate a ShelfSessions instance.

  • name will be the name used to instantiate Shelf
  • secretOrPrivateKey secret/private key used to sign the JWT's as described by the node-jsonwebtoken library. It must be a string or a buffer.
  • [options] series of optional parameters used by node-jsonwebtoken and Shelf
    • [algorithm] algorithm used to sign the jwt
    • [audience] audience claim
    • [subject] subject claim
    • [issuer] issuer claim
    • [headers] additional headers as specified by node-jsonwebtoken
    • [ttl] sessions ttl in seconds. Defaults to 72 hours
    • [shelf] an already inited shelf instance (will override connection options to instantiate Shelf)
    • [host] redis host used by Shelf
    • [port] redis port used by Shelf
    • [password] redis password used by Shelf
    • [defaultProps] an optional json object whose keys should be Joi objects. This is used to extend the default session schema and be used on all the .extend() calls. The default schema will be:
    {
      jwt: Joi.string().required(),
      userId: Joi.string().required(),
      scopes: Joi.array().min(1).required()
    }
    You can extend the schema at will but this three keys must always exist.

.extend(model)

Generate a SessionModel schema similar to what you would do with a regular Shelf instance. The provided schema will then be used to operate on the specified models.

  • model a model similar to what you would use in Shelf.extend(model)

    • name a mandatory name for the schema.
    • [props] an optional json object whose keys should be Joi objects, used to extend the base user model according to the defaultProps.

    ** Note: ** no keys array is provided to act as Redis key, as it will always be the jwt.

SessionModel

The result of the .extend() call. This will allow to make operations based on the schemas and options previously provided.

.deleteSession(token, callback)

Delete the sessions associated with the token. The callback will be called with an error in case of failure.

  • token jwt token
  • callback a callback function

.createSession(session, callback)

Creates a session based on the given session object. The callback will be called as - callback(error, resultSession) - being that, if successful, the provided resultSession will be a Shelf Model with all the normal operations associated to it.

  • session a session json object
  • callback a callback function

.authenticate(token, callback)

Given a jwt, this method verifies it and returns the correspondent session Shelf Model stored in Redis. The callback will be called as - callback(error, resultSession)

  • token jwt token
  • callback a callback function

Contributing

We use standard js.

In order to run the tests you should have an Redis instance running locally.

License

MIT