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

toa-token

v2.3.1

Published

Json web token (JWT) module for toa.

Downloads

46

Readme

toa-token

Json web token (JWT) module for toa.

It sign and verify token through a rotating credential system, in which new server keys can be added and old ones removed regularly, without invalidating client credentials.

NPM version Build Status Downloads

toa

JSON Web Tokens

This module lets you authenticate HTTP requests using JWT tokens in toa applications. JWTs are typically used protect API endpoints, and are often issued using OpenID Connect.

Demo

const Toa = require('toa')
const toaToken = require('toa-token')
const Router = require('toa-router')
const toaBody = require('toa-body')

const router = new Router()

router
  .get('/auth', function * () {
    let user = yield this.parseBody()
    // verify with user.name and user.passwd, get user._id
    let token = this.signToken({
      name: user.name,
      _id: user._id
    })
    this.body = token
  })
  .get('/', function (Thunk) {
    // should have this.token when client request with authorization header.
    // var token = this.token // {_id: 'user id', name: 'user name'}
    // ....
  })

const app = Toa(function * () {
  yield router.route(this)
})

toaBody(app)
toaToken(app, 'secretKeyxxx', {
  expiresIn: 60
})

app.listen(3000)

Installation

npm install toa-token

API

const toaToken = require('toa-token')

toaToken(app, secretOrPrivateKeys, [options]))

  • secretOrPrivateKeys: secretOrPrivateKeys is a array of string or buffer containing either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.

  • options.authScheme: String, Authorization scheme name, default to Bearer. In HTTP header fields: Authorization: Bearer QWxhZGRpbjpvcGVuIHNld2FtZQ==.

  • options.useProperty: String, token name add to context, default to token.

  • options.getToken: Function, A custom function for extracting the token, This is useful if you need to pass the token through a query parameter or a cookie.

  • options.algorithm (default: HS256)

  • options.expiresIn: expressed in seconds or a string describing a time span rauchg/ms. Eg: 60, "2 days", "10h", "7d"

  • options.notBefore: expressed in seconds or a string describing a time span rauchg/ms. Eg: 60, "2 days", "10h", "7d"

  • options.audience

  • options.issuer

  • options.jwtid

  • options.subject

  • options.noTimestamp

  • options.header

More options is same as jsonwebtoken

context.token

This is a getter that auto verify the token. if authorization is invalid, context will throw error. token maybe custom by options.useProperty.

console.log(this.token)

app.signToken(payload) / context.signToken(payload)

Generate a token string. payload could be an literal, buffer or string, If payload is not a buffer or a string, it will be coerced into a string using JSON.stringify.

app.decodeToken(token, options) / context.decodeToken(token, options)

Returns the decoded payload without verifying if the signature is valid.

  • token: String, the JsonWebToken string.
  • options.json: Boolean, force JSON.parse on the payload even if the header doesn't contain "typ":"JWT".

app.verifyToken(token, options) / context.verifyToken(token, options)

Returns the decoded payload with verifying if the signature is valid.

  • token: String, the JsonWebToken string.

toaToken.jwt

It is a reference of jsonwebtoken module.

toaToken.JWT(secretOrPrivateKeys)

It is a wrap of jsonwebtoken module.

const jwt = new toaToken.JWT(secretOrPrivateKeys)

jwt.signToken(payload, options)

jwt.decodeToken(token, options)

jwt.verifyToken(token, options)

Licences

(The MIT License)