{"id":13734578,"url":"https://github.com/sagi/workers-jwt","last_synced_at":"2025-07-08T23:35:28.691Z","repository":{"id":40812672,"uuid":"185047980","full_name":"sagi/workers-jwt","owner":"sagi","description":"Generate JWTs on Cloudflare Workers using the WebCrypto API","archived":false,"fork":false,"pushed_at":"2024-09-10T04:28:30.000Z","size":2006,"stargazers_count":96,"open_issues_count":3,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T00:44:24.154Z","etag":null,"topics":["cloudflare","cloudflare-workers","jwt","webcrypto","workers"],"latest_commit_sha":null,"homepage":"https://sagi.io","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sagi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"patreon":"sagi","liberapay":"sagi"}},"created_at":"2019-05-05T15:18:53.000Z","updated_at":"2025-03-10T23:01:20.000Z","dependencies_parsed_at":"2024-01-12T02:43:23.510Z","dependency_job_id":"3a13b36a-0343-4f0b-958f-e90fb1dd5217","html_url":"https://github.com/sagi/workers-jwt","commit_stats":{"total_commits":75,"total_committers":5,"mean_commits":15.0,"dds":0.6133333333333333,"last_synced_commit":"5eff4315191efcb01d6888b5ce5bb73e0f964d5e"},"previous_names":["sagi/cfw-jwt","sagi/gcp-jwt"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagi%2Fworkers-jwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagi%2Fworkers-jwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagi%2Fworkers-jwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagi%2Fworkers-jwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sagi","download_url":"https://codeload.github.com/sagi/workers-jwt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247685634,"owners_count":20979085,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudflare","cloudflare-workers","jwt","webcrypto","workers"],"created_at":"2024-08-03T03:00:57.588Z","updated_at":"2025-04-07T16:19:03.692Z","avatar_url":"https://github.com/sagi.png","language":"JavaScript","readme":"# workers-jwt\n\n[`@sagi.io/workers-jwt`](https://www.npmjs.com/package/@sagi.io/workers-jwt) helps you\ngenerate a `JWT` on Cloudflare Workers with the WebCrypto API. Helper function for GCP Service Accounts included.\n\n⭐ We use it at **[OpenSay](https://opensay.co/?s=workers-jwt)** to efficiently access Google's REST APIs with 1 round trip.\n\n[![CircleCI](https://circleci.com/gh/sagi/workers-jwt.svg?style=svg)](https://circleci.com/gh/sagi/workers-jwt)\n[![Coverage Status](https://coveralls.io/repos/github/sagi/workers-jwt/badge.svg?branch=master)](https://coveralls.io/github/sagi/workers-jwt?branch=master)\n[![MIT License](https://img.shields.io/npm/l/@sagi.io/workers-jwt.svg?style=flat-square)](http://opensource.org/licenses/MIT)\n[![version](https://img.shields.io/npm/v/@sagi.io/workers-jwt.svg?style=flat-square)](http://npm.im/@sagi.io/workers-jwt)\n\n## Installation\n\n~~~\n$ npm i @sagi.io/workers-jwt\n~~~\n\n## API\n\nWe currently expose two methods: `getToken` for general purpose `JWT` generation\nand `getTokenFromGCPServiceAccount` for `JWT` generation using a `GCP` service account.\n\n### **`getToken({ ... })`**\n\nFunction definition:\n\n```js\nconst getToken = async ({\n  privateKeyPEM,\n  payload,\n  alg = 'RS256',\n  cryptoImpl = null,\n  headerAdditions = {},\n}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`privateKeyPEM`** is the private key `string` in `PEM` format.\n  - **`payload`** is the `JSON` payload to be signed, i.e. the `{ aud, iat, exp, iss, sub, scope, ... }`.\n  - **`alg`** is the signing algorithm as defined in [`RFC7518`](https://tools.ietf.org/html/rfc7518#section-3.1), currently only `RS256` and `ES256` are supported.\n  - **`cryptoImpl`** is a `WebCrypto` `API` implementation. Cloudflare Workers support `WebCrypto` out of the box. For `Node.js` you can use [`require('crypto').webcrypto` - see examples below and in the tests.\n  - **`headerAdditions`** is an object with keys and string values to be added to the header of the `JWT`.\n\n### **`getTokenFromGCPServiceAccount({ ... })`**\n\nFunction definition:\n\n```js\nconst getTokenFromGCPServiceAccount = async ({\n  serviceAccountJSON,\n  aud,\n  alg = 'RS256',\n  cryptoImpl = null,\n  expiredAfter = 3600,\n  headerAdditions = {},\n  payloadAdditions = {}\n}) =\u003e { ... }\n```\n\nWhere:\n\n  - **`serviceAccountJSON`** is the service account `JSON` object .\n  - **`aud`** is the audience field in the `JWT`'s payload. e.g. `https://www.googleapis.com/oauth2/v4/token`'.\n  - **`expiredAfter`** - the duration of the token's validity. Defaults to 1 hour - 3600 seconds.\n  - **`payloadAdditions`** is an object with keys and string values to be added to the payload of the `JWT`. Example - `{ scope: 'https://www.googleapis.com/auth/chat.bot' }`.\n  - **`alg`**, **`cryptoImpl`**, **`headerAdditions`** are defined as above.\n\n\n## Example\n\nSuppose you'd like to use `Firestore`'s REST API. The first step is to generate\na service account with the \"Cloud Datastore User\" role. Please download the\nservice account and store its contents in the `SERVICE_ACCOUNT_JSON_STR` environment\nvariable.\n\nThe `aud` is defined by GCP's [service definitions](https://github.com/googleapis/googleapis/tree/master/google)\nand is simply the following concatenated string: `'https://' + SERVICE_NAME + '/' + API__NAME`.\nMore info [here](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#jwt-auth).\n\nFor `Firestore` the `aud` is `https://firestore.googleapis.com/google.firestore.v1.Firestore`.\n\n## Cloudflare Workers Usage\n\nCloudflare Workers expose the `crypto` global for the `Web Crypto API`.\n\n~~~js\nconst { getTokenFromGCPServiceAccount } = require('@sagi.io/workers-jwt')\n\nconst serviceAccountJSON = await ENVIRONMENT.get('SERVICE_ACCOUNT_JSON','json')\nconst aud = `https://firestore.googleapis.com/google.firestore.v1.Firestore`\n\nconst token = await getTokenFromGCPServiceAccount({ serviceAccountJSON, aud} )\n\nconst headers = { Authorization: `Bearer ${token}` }\n\nconst projectId = 'example-project'\nconst collection = 'exampleCol'\nconst document = 'exampleDoc'\n\nconst docUrl =\n  `https://firestore.googleapis.com/v1/projects/${projectId}/databases/(default)/documents`\n  + `/${collection}/${document}`\n\nconst response = await fetch(docUrl, { headers })\n\nconst documentObj =  await response.json()\n~~~\n\n## Node Usage (version \u003c=14)\n\nWe use the `node-webcrypto-ossl` package to imitate the `Web Crypto API` in Node.\n\n~~~js\nconst { Crytpo }= require('node-webcrypto-ossl');\nconst cryptoImpl  = new Crypto();\nconst { getTokenFromGCPServiceAccount } = require('@sagi.io/workers-jwt')\n\nconst serviceAccountJSON = { ... }\nconst aud = `https://firestore.googleapis.com/google.firestore.v1.Firestore`\n\nconst token = await getTokenFromGCPServiceAccount({ serviceAccountJSON, aud, cryptoImpl } )\n\n\u003c... SAME AS CLOUDFLARE WORKERS ...\u003e\n~~~\n\n\n## Node Usage (version 15+)\n\nNode 15 introduces the [Web Crypto API](https://nodejs.org/api/webcrypto.html).  When using NextJS, you may need to pass in the native Node `webcrypto` lib to get both SSR and webpack to work during dev mode.\n\n~~~js\nconst { getTokenFromGCPServiceAccount } = require('@sagi.io/workers-jwt')\n\nconst serviceAccountJSON = { ... }\nconst aud = 'https://firestore.googleapis.com/google.firestore.v1.Firestore';\n\nconst token = await getTokenFromGCPServiceAccount({\n  serviceAccountJSON,\n  aud,\n  cryptoImpl: globalThis.crypto || require('crypto').webcrypto,\n});\n\n\u003c... SAME AS CLOUDFLARE WORKERS ...\u003e\n~~~\n","funding_links":["https://patreon.com/sagi","https://liberapay.com/sagi"],"categories":["Security"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagi%2Fworkers-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsagi%2Fworkers-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagi%2Fworkers-jwt/lists"}