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

jwks-utils

v1.0.10

Published

Utility methods for working with a JSON Web Key (JWK) and/or JSON Web Key Set (JWKs)

Downloads

849

Readme

Build Status Coverage Status Dependency Status License

node-jwks-utils

A set of useful tools when working with JSON Web Key (JWK) and JSON Web Key Set (JWKs).

Install

$ npm install jwks-utils

Example

var jwksUtils = require('jwks-utils');

var jwk = { kid: '1234', kty: 'RSA', n: '12345...XYZ=', e: 'AQAB' };
var jwks = { keys: [ jwk ] }

// Detect a JWK object
if(jwksUtils.isJWK(jwk)) {
    // Do stuff with the JWk
}

// Detect a JWKs object
if(jwksUtils.isJWKset(jwks)) {
    // Do stuff with the JWKs
}

// Find a particilar JWK within a JWKs
var jwk1 = jwkUtils.findJWK('1234', jwks);

// Find the JWK corsponding to a particular JWS (or JWT)
var signature = getJWSFromSomwhere();
jwkUtils.jwkForSignature(signature, false, {timeout: 100}, function(err, jwk2) {
    if (!err) {
        // jwk2 is the corresponding JWK
    }
};

Caching of JSON Web Key Sets (jwks) from a JSON Web Key URI (jku)

This library makes requests to outside web URI's if it determines that a jku is needed to get the public key (jwk) to verify a signature. It expects that URL to have a JSON Web Key Set (jwks according to the standard). Because this process can sometimes be slow, and because in production sometimes networks go down, we have added a small in-memory cache to this library.

When the library decides it needs a jwks from a jku, it will immediately return the cached value if the given signature's key is in the cached keyset. It will also fire off a request in the background that will update the cache to the latest copy of the jwk set. It will consider the cache entry stale after 1 hour and then wait for the request to update the cache.

If the key in the signature was not in a cached jwks (or it was not yet cached at all), the function will wait for the request to finish. Once it finishes, if there was an error in the request, it will check the cache to see if we have a stale cached copy. If so, then it will use that stale cached copy for up to 24 hours before removing it from the cache.

If it does not have an error in the request, even if we've already returned the cached copy for the signature, it will go ahead and put the new response's jwks into the cache and then return it.

In this way, whenever you publish a new kid in your jwks, any clients will immediately be able to use it. However, if you revoke a kid, the client will still allow for 1 valid signature in the first hour, and then any request after the first one, or after an hour, will be invalid.

References

  1. JSON Web Key (JWK) Draft 40