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

@fwl/oauth2

v0.2.5

Published

OAuth2 part of Fewlines Web Libraries

Downloads

27

Readme

FWL OAuth2

Disclaimer: this package is made for our internal usage and is only open source for convenience so we might not consider Pull Requests or Issues. Feel free to fork though.

This is part of the Fewlines Web Libraries packages. It mainly provides an abstraction for the OAuth2 flow, and a secure way to verify JWT for HS256 and RS256 algorithms

Installation

yarn add @fwl/oauth2

Getting Started

You first need to initialize the client instance, called OAuth2Client. This class takes the following constructor parameters:

  • openIDConfigurationURL: The URL to retrieve the OpenID configuration.
  • clientID: Client ID of the online service (e.g. internet website, application) that uses the Provider Authentication and Authorization service for its User.
  • clientSecret: Paired with the client ID, used to authenticate the Application from which the User intent to sign in.
  • redirectURI: URI used to redirect to the original Application website after a successful login in on Connect.
  • audience: The audience of the Application (its name for Connect)..
  • scopes: It represents the kind of information and actions that an Application is able to access on another Application. Every scope has to be authorized by the User during sign in operation.
import OAuth2Client, { OAuth2ClientConstructor } from "@fwl/oauth2";

const oauthClientConstructorProps: OAuth2ClientConstructor = {
  openIDConfigurationURL: "***",
  clientID: "***",
  clientSecret: "***",
  redirectURI: "***",
  audience: "***",
  scopes: ["***", "***"],
};

const oauthClient = new OAuth2Client(oauthClientConstructorProps);

Usage

The initialized instance of OAuth2Client provides the following methods to help with the OAuth2 flow. Each method will initialize the open OpenID configuration returned from the URL provided if not initialized yet.

getAuthorizationURL

async getAuthorizationURL(state?: string): Promise<URL>{}

The OAuth2 flow requires to move out of your application. If you need to keep some sort of state (like a user ID), you can pass said state to the function, which will be be added at the end of the query string. The function returns the authorization URL used to start the OAuth2 flow, and retrieve the authorization_code.

const authURL = await oauthClient.getAuthorizationURL("state");

getTokensFromAuthorizationCode

async getTokensFromAuthorizationCode(authorizationCode: string): Promise<string[]>{};

Returns a list containing the access_token, refresh_token, and id_token if present.

const tokens = await oauthClient.getTokensFromAuthorizationCode(
  "authorization_code",
);

verifyJWT

async verifyJWT<T = unknown>(accessToken: string, algo: string): Promise<T> {};

Used to verify the JWS (i.e. access_token). It provides a series of checks, like audiences, algorithm or public key.

const decoded = await oauthClient.verifyJWT(JWS, "RS256");

decryptJWE

async decryptJWE<T = unknown>(JWE: string, privateKey: string, isSigned: boolean): Promise<T> {}

Used to decrypt the JWE (i.e. access_token), and returns the JWS or the JWT.

The privateKey argument should be in PEM format.

// Signed JWE
const decrypted = oauthClient.decryptJWE<string>(JWE, privateKey, true);

// Un-signed JWE
const decrypted = oauthClient.decryptJWE<{ [key: string]: string }>(
  JWE,
  privateKey,
  false,
);

Utils

@fwl/oauth2 also provides utils functions and default objects that we are using in the package flow.

generateHS256JWS

import { generateHS256JWS } from "@fwl/oauth2"

generateHS256JWS(customPayload?: CustomPayload, secret?: string): string {};

If used without any argument, the function will return a default HS256 JWS composed of the default objects found below.

You can give a custom secret for signature, and/or a custom payload to customize your HS256 JWS.

generateRS256JWS

import { generateRS256JWS } from "@fwl/oauth2"

generateRS256JWS(customPayload?: CustomPayload, secret?: string): string {};

If used without any argument, the function will return a default RS256 JWS composed of the default objects found below.

You can give a custom secret for signature, and/or a custom payload to customize your RS256 JWS.

default JWS composition objects

The following exported objects are used when generating a JWS using generateHS256JWS and generateHS256JWS, while no arguments are passed:

const defaultPayload: JWTPayload = {
  aud: ["oauth2"],
  exp: Date.now() + 3600,
  iss: "https://fwl.test",
  scope: "email",
  sub: "2a14bdd2-3628-4912-a76e-fd514b5c27a8",
};
const defaultAsymmetricAlgoKeyPair = {
  privateKey: `-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAsBUV0XnPsTCYhI3TmcxjSmnOmlQlKWfFbr/sABcM65P6f28g
c39XyWj/CcE+iIscrfvodinyHr2dXm+wpTnVyqZUsOavEHhq0n8flp8G4RdsYDMn
MIH6gat4XVTLztLiM0Vsb+h5LAD1ETxiVNtoB3urtD9Wn6F1NobDWc1FKtrGGCyt
HC0S7GNVAUE+sZTxPTXOraCV8aZt5McPnZE7V2ohSoJJR4HxUV16xIlOkxDKrvt1
INTjE4G3va0kF+NO37as696KLnylS/Zk13zS0+GKU+swjJHMHGTwy8p4ZRdW+94k
Q8jQjkc3QNswv7GDqy+om5S36qz2UfEudDg8vwIDAQABAoIBAQCblcX1liCCfGIH
nhn+p66ELgG0M+2yLFMzjLnHSpbMAbExsMuHwlmbSf8wa2QJ2oG46gF5h468wuau
bxoypGvX6CilNvAxKXWaEWGkAVhexAeq6QgTwIXM1epA7ZA4a/YbP5iVOWD4FNB2
LljkigndstUBK4Yq/stHJsSuMMaRGc6p7SwGe/qbJLFE2gU13umQr0eGJGcunoaa
/NI8pI+XUqNBJ5o0gHwPVW39dCtN2PCfV+pnh4d/0FcGKJMNbZLutgmwyo+dB2+R
2iJPxjam/fZ0eaoGd0b9IaVCgrLb4er+9NW6P7C3SmUGk0FynsG0p7SZdyIFhT1M
2RLjX+1BAoGBAN2yQqkSr3Tv9dkf/R237RowAgTCAsuvww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0Vsb+h5LAD1ETxiVNto
B3urtD9Wn6F1NobDWc1FKtrGGCytHC0S7GNVAUE+sZTxPTXOraCV8aZt5McPnZE7
V2ohSoJJR4HxUV16xIlOkxDKrvt1INTjE4G3va0kF+NO37as696KLnylS/Zk13zS
0+GKU+swjJHMHGTwy8p4ZRdW+94kQ8jQjkc3QNswv7GDqy+om5S36qz2UfEudDg8
vwIDAQAB
-----END PUBLIC KEY-----`,
};
const defaultSecret = "c9ab0fdc-b2dc-47ad-933b-87cf1b180ab5";

They are all imported like so:

import {
  defaultAsymmetricAlgoKeyPair,
  defaultPayload,
  defaultSecret,
} from "@fwl/oauth2";

decodeJWTPart

Takes a part from a JWT (e.g. the JWA, or the payload), and decode it.

import { decodeJWTPart } from "@fwl/oauth2";

const [JWA, payload] = JWT.split(".");

const decodedJWA = decodeJWTPart(JWA);
const decodedPayload = decodeJWTPart(payload);

rsaPublicKeyToPEM

Takes a modulus and an exponent (found in the JWKS), and recreate a public key.

import { rsaPublicKeyToPEM } from "@fwl/oauth2";

const key = {
  e: "AQAB",
  kty: "RSA",
  kid: "d6512f53-9774-4a58-830c-981886c8bb43",
  n: "y3M7JqY49JeL/ornP7ZY2QlO76akS36Rj1iKVSIlFH754NnqmtGwMrCVZzCWrc882trbGuDhml2psOmCIBjKBpnghNLBZALGNRelCqfV7Cy+EMrQvQ+UWbogT7xfPoL+VYjCZKTeXosfzMNMZFum/Vnk/vYBKilXZfQH1t4sohU=",
  alg: "RS256",
};

const { e, n } = key;
const publicKey = rsaPublicKeyToPEM(n, e);