
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
web-auth-library
Advanced tools
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
import { getAuthToken } from "web-auth-library/google";
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}`,
},
});
Where env.GOOGLE_CLOUD_CREDENTIALS
is an environment variable / secret
containing a service account key
(JSON) obtained from the Google Cloud Platform.
import { getAuthToken } from "web-auth-library/google";
const token = await getAuthToken({
credentials: env.GOOGLE_CLOUD_CREDENTIALS,
audience: "https://example.com",
});
// => {
// idToken: "eyJhbGciOiJSUzI1NiIsImtpZ...",
// audience: "https://example.com",
// expires: 1654199401,
// }
import { jwt } from "web-auth-library/google";
jwt.decode(idToken);
// {
// header: {
// alg: 'RS256',
// kid: '38f3883468fc659abb4475f36313d22585c2d7ca',
// typ: 'JWT'
// },
// payload: {
// iss: 'https://accounts.google.com',
// sub: '118363561738753879481'
// aud: 'https://example.com',
// azp: 'example@example.iam.gserviceaccount.com',
// email: 'example@example.iam.gserviceaccount.com',
// email_verified: true,
// exp: 1654199401,
// iat: 1654195801,
// },
// data: 'eyJhbGciOiJ...',
// signature: 'MDzBStL...'
// }
import { verifyIdToken } from "web-auth-library/google";
const token = await verifyIdToken(idToken, { audience: "https://example.com" });
// => {
// iss: 'https://accounts.google.com',
// aud: 'https://example.com',
// sub: '118363561738753879481'
// azp: 'example@example.iam.gserviceaccount.com',
// email: 'example@example.iam.gserviceaccount.com',
// email_verified: true,
// exp: 1654199401,
// iat: 1654195801,
// }
import { getCredentials, importKey, sign } from "web-auth-library/google";
const credentials = getCredentials(env.GOOGLE_CLOUD_CREDENTIALS);
const signingKey = await importKey(credentials.private_key, ["sign"]);
const signature = await sign(signingKey, "xxx");
JWT
tokenimport { jwt } from "web-auth-library";
jwt.decode("eyJ0eXAiOiJKV1QiLC...");
// => {
// header: { alg: "HS256", typ: "JWT" },
// payload: { iss: "...", aud: "...", iat: ..., exp: ... },
// signature: "xxx"
// }
jwt.decode("eyJ0eXAiOiJKV1QiLC...", { header: false, signature: false });
// => {
// payload: { iss: "...", aud: "...", iat: ..., exp: ... },
// }
You're very welcome to create a PR or send me a message on Discord.
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.
FAQs
Authentication library for the browser environment using Web Crypto API
The npm package web-auth-library receives a total of 7,011 weekly downloads. As such, web-auth-library popularity was classified as popular.
We found that web-auth-library demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.