JSPM

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

Authentication library for the browser environment using Web Crypto API

Package Exports

  • web-auth-library/gcp
  • web-auth-library/gcp/auth
  • web-auth-library/gcp/credentials
  • web-auth-library/gcp/crypto
  • web-auth-library/package.json

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

# 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/gcp";

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 service account key (JSON) obtained from the Google Cloud Platform.

Generating an ID token for the arbitrary resource

import { getAuthToken, importKey, sign } from "web-auth-library/gcp";

const token = await getAuthToken({
  credentials: env.GOOGLE_CLOUD_CREDENTIALS,
  audience: "https://example.com",
});
// => {
//   idToken: "eyJhbGciOiJSUzI1NiIsImtpZ...",
//   audience: "https://example.com",
//   expires: 1653855236,
// }

Generating a digital signature

import { getCredentials, importKey, sign } from "web-auth-library/gcp";

const credentials = getCredentials(env.GOOGLE_CLOUD_CREDENTIALS);
const signingKey = await importKey(credentials.private_key, ["sign"]);
const signature = await sign(signingKey, "xxx");

Backers 💰

              

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.