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

@turnly/auth

v0.2.38

Published

JWT token verifier that is signed with the RS256 algorithm.

Downloads

40

Readme

Auth

If you're wondering how we handle authentication and authorization, you've come to the right place.

We use the JWKS (JSON Web Key Set) standard to verify the JWT (JSON Web Token) signature. This means you can bring your own OIDC (OpenID Connect) provider and authenticate the users without the need to write a wrapper around it.

How it works?

  1. The user obtain a JWT through the OIDC provider login flow. When the user is authenticated, the OIDC provider returns a signed JWT.
  2. Now, the user can use the JWT to access the Tenancy API.
  3. The Tenancy API will verify the JWT signature using the OIDC provider public keys.
  4. If the signature is valid, the Tenancy API will return the user claims.
  5. The user claims are used to authorize the user to access the Tenancy API.
  6. The user can now access the Tenancy API.
  7. Done! 🎉

RS256 Signing Algorithm

We use RS256 because it is the recommended algorithm when signing your JWTs. It's more secure, and you can rotate keys quickly if they're compromised without re-deploying your application with a new secret, as you would have to do with HS256.

JSON Web Key Set

When you create JSON Web Tokens, they are signed using public/private key pair. Signing the token allows its recipient to validate that the content of the token wasn't changed and verify the original issuer of the token created signature.

The OIDC server provides the keys publicly in a URL in the form of a JSON Web Key Set (JWKS). During verification, the public keys are obtained. Here is an example of JWKS:

{
  "keys": [
    {
      "kid": "dkvkkV9wgDWsA7g8bPwwckirxcoDDigCOHOXqoFck2Q",
      "kty": "RSA",
      "alg": "RS256",
      "use": "sig",
      "n": "joVj5rZQ89N8rJUCsqVca9DDcOgmXMVuuMJlnZh_hZtHGKvAE1Q...x200",
      "e": "AQAB"
    }
  ]
}

Usage

import { OIDC } from '@turnly/auth'

const oidc = new OIDC({
  /**
   * The issuer is used to validate the issuer of the JWTs.
   * You can also provide the `AUTH_ISSUER` environment variable.
   */
  issuer: 'https://turnly.us.auth0.com',

  /**
   * JWKs (JSON Web Key Set)
   */
  jwks: {
    /**
     * The uri is used to retrieve the signing keys from the JWKS endpoint.
     * You can also provide the `AUTH_JWKS_URI` environment variable.
     */
    jwksUri: 'https://turnly.us.auth0.com/.well-known/jwks.json',

    /**
     * The cache option is used to cache the signing keys.
     */
    cache: true,

    /**
     * The cacheMaxAge option is used to set the maximum age of the cache. (in milliseconds)
     * Default: 7_200_000 (2 hours)
     */
    cacheMaxAge: 300_000,
  },

  /**
   * Token type
   *
   * @description This is optional verification for validating the expected token type.
   */
  tokenType: {
    /**
     * The expected value of the token type.
     * @example Standard values are `Bearer` | `ID` | `Refresh`
     */
    type: 'Bearer',

    /**
     * The property to lookup the token type.
     */
    claim: 'token_type',
  },
})

const claims = await oidc.verify('__TOKEN___')

Tenancy - Authorization policies

If you're wondering how we handle access control, you've come to the right place.

We use a set of programming rules that determine who is authorized to access a particular resource or perform a particular action.

This package is used by the main API to authorize access to resources and actions. It is also used by the main API to determine which resources and actions are available to a user.

This may sound complicated, but it's not. The policies are just functions that receive a context and return a boolean value. The context contains the user claims and the resource.

How it works?

  1. Select the policy that you want to use.
  2. The policy will receive a context.
  3. The policy will return a boolean value.
  4. Done! 🎉

Example

import { Context, Policies } from '@turnly/auth'

const context: Context = {
  user: {
    memberships: [
      {
        organizationId: 'organization-id',
        roles: ['roles:admin'],
      },
    ],
  },
  organizationId: 'organization-id',
  roles: ['roles:admin'],
}

if (Policies.Roles.isAllowed(context)) {
  // Do something
}

If you are using another programming language, you should not worry about using these policies, event if you are using it.

These policies although available as a package, are intended for internal use of the main API.

The tenancy API takes care of using these policies and gets the user's claims from the access token.