v0.2.0
A collection of utility functions for working with Web Crypto API.
How to Install
# 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.