JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12309
  • Score
    100M100P100Q122808F
  • License MIT

Authentication library for the browser environment using Web Crypto API

Package Exports

  • web-auth-library
  • web-auth-library/auth
  • web-auth-library/credentials
  • web-auth-library/crypto

Readme

Authentication Library for the Web Environment

NPM Version NPM Downloads TypeScript Donate Discord

A collection of utility functions for working with Web Crypto API.

# Install using NPM
$ npm install web-auth-library --save-dev

# Install using Yarn
$ yarn add web-auth-library

Usage Example

Retrieving an access token from the Google's OAuth 2.0 authorization server using a service account key (JSON), in Cloudflare Workers environment:

import { getAuthToken } from "web-auth-library";

export default {
  async fetch(req, env) {
    // Get an access token for interacting with Google Cloud Platform APIs.
    const token = await getAuthToken({
      credentials: env.GOOGLE_CLOUD_CREDENTIALS,
      scope: "https://www.googleapis.com/auth/cloud-platform",
    });
    // => {
    //   accessToken: "ya29.c.b0AXv0zTOQVv0...",
    //   type: "Bearer",
    //   expires: 1653855236,
    // }
    return fetch("https://cloudresourcemanager.googleapis.com/v1/projects", {
      headers: {
        authorization: `Bearer ${token.accessToken}`,
      },
    });
  },
} as ExportedHandler;

Where env.GOOGLE_CLOUD_CREDENTIALS is an environment variable / secret containing a base64-encoded service account key obtained from the Google Cloud Platform.

How to Contribute

You're very welcome to create a PR or send me a message on Discord.

License

Copyright © 2022-present Kriasoft. This source code is licensed under the MIT license found in the LICENSE file.


Made with ♥ by Konstantin Tarkus (@koistya, blog) and contributors.