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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@vanioinformatika/jwt-wrapper

v2.0.5

Published

A wrapper around the jsonwebtoken npm module that handles key ids.

Downloads

34

Readme

Coverage Status Build Status

node-jwt-wrapper

A promisified wrapper around the jsonwebtoken npm module that handles key ids. Uses native promises.

Usage

const jwt = require('@vanioinformatika/jwt-wrapper') (

/**
 * Resolves public keys or certificates can be sync or async function
 * @param {string} The key id
 */
function pubkeyResolver (keyId) {
  const cert = ... // load certificate or public key
  const alg = ... // resolve key algorithm (JWA values like RS256, ES256, etc.)
  return {cert, alg}
}

/**
 * Resolves private keys can be sync or async function
 * @param {string} The key id
 */
function privkeyResolver (keyId) {
  const key = ... // load private key in PEM format
  const passphrase = ... // load private key passphrase
  const alg = ... // resolve key algorithm (JWA values like RS256, ES256, etc.)
  return {key, passphrase, alg}
}

const jwtHandler = new jwt.JwtHandler('myproject', pubkeyResolver, privkeyResolver)

// or with options object

const jwtHandler = new jwt.JwtHandler({
    debugNamePrefix: 'myproject',
    pubkeyResolver: pubkeyResolver,
    privkeyResolver: privkeyResolver,
})

// Verifying JWT tokens
jwtHandler.verify(jwtRaw)
          .then(jwtBody => {
            ...
          })
          .catch(err => {
            // Handle errors
          })


// Creating JWT tokens
const keyId = 'keyid1'
const tokenBody = { iat: Math.floor(Date.now() / 1000), sub: 'token subject', iss: 'issuer1', aud: 'audience1' }

jwtHandler.create(tokenBody, keyId)
          .then(jwt => {
            ...
          })
          .catch(err => {
            // Handle errors
          })
...

Implementing key resolvers

This module can be used with either synchronous or asynchronous key resolvers. Either way, you may want to implement some kind of cache for the keys as the resolvers are called on every Handler.validate and Handler.create calls.

Synchronous example:

/** resolves public keys and certificates synchronously */
function pubkeyResolver (keyId) {
  const cert = fs.readFileSync(path.join(basedir, `cert.${keyId}.crt`))
  const alg = getKeyAlg(cert)
  if (cert) {
    return {cert, alg}
  }
}
/** resolves private keys synchronously */
function privkeyResolver (keyId) {
  const key = fs.readFileSync(path.join(basedir, `privkey.${keyId}.key`))
  if (key) {
    const alg = getKeyAlg(key)
    const passphrase = getPassphrase(keyId)
    return {key, passphrase, alg}
  }
}

Asynchronous example:

/** resolves public keys and certificates asynchronously */
function pubkeyResolver (keyId) {
  return new Promise(resolve, reject) {
    fs.readFile(path.join(basedir, `cert.${keyId}.crt`), (err, cert) => {
      if (!err) {
        const alg = getKeyAlg(cert)
        resolve({cert, alg})
      } else if (err.code === 'ENOENT') {
        resolve() // Note that if you resolve with undefined, it will result in UnknownKeyIdError
      } else {
        reject(err)
      }
    })
  }
}
/** resolves private keys asynchronously */
function privkeyResolver (keyId) {
  return new Promise(resolve, reject) {
    fs.readFile(path.join(basedir, `privkey.${keyId}.key`), (err, key) => {
      if (!err) {
        const alg = getKeyAlg(key)
        const passphrase = getPassphrase(keyId)
        resolve({key, passphrase, alg})
      } else if (err.code === 'ENOENT') {
        resolve() // Note that if you resolve with undefined, it will result in UnknownKeyIdError
      } else {
        reject(err)
      }
    })
  }
}