Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,59 @@ jobs:
- name: Test
run: |
npm run test

# Each package under packages/ is released on its own, so each is built and
# tested on its own.
packages:
name: ${{ matrix.package }} (Node ${{ matrix.node }})
runs-on: ubuntu-latest
strategy:
matrix:
node: [22, 24]
package:
- workload-identity-github-actions
- workload-identity-gcp
- workload-identity-aws
steps:
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
persist-credentials: false
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0
with:
node-version: ${{ matrix.node }}

- name: Configure Socket Firewall
uses: workos/setup-socket-firewall@ca93dd8aa351f54f4729fe3377a9be23c631c25d
with:
token: ${{ secrets.PUBLIC_SOCKET_FIREWALL_TOKEN }}
allow-external-fork-fallback: true

# At the repository root: it installs every workspace package.
- name: Install Dependencies
run: |
npm install

- name: Prettier
working-directory: packages/${{ matrix.package }}
run: |
npm run prettier

- name: Lint
working-directory: packages/${{ matrix.package }}
run: |
npm run lint

- name: Typecheck
working-directory: packages/${{ matrix.package }}
run: |
npm run typecheck

- name: Build
working-directory: packages/${{ matrix.package }}
run: |
npm run build

- name: Test
working-directory: packages/${{ matrix.package }}
run: |
npm run test
36 changes: 35 additions & 1 deletion .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ jobs:
release-please:
runs-on: ubuntu-latest
outputs:
# With more than one package, these two refer to the root package (the SDK) only.
release_created: ${{ steps.release.outputs.release_created }}
tag_name: ${{ steps.release.outputs.tag_name }}
# One per provider package, keyed by its path in release-please-config.json.
github_actions_release_created: ${{ steps.release.outputs['packages/workload-identity-github-actions--release_created'] }}
gcp_release_created: ${{ steps.release.outputs['packages/workload-identity-gcp--release_created'] }}
aws_release_created: ${{ steps.release.outputs['packages/workload-identity-aws--release_created'] }}
steps:
- name: Generate token
id: generate-token
Expand All @@ -25,7 +30,7 @@ jobs:
private-key: ${{ secrets.SDK_BOT_PRIVATE_KEY }}
permission-contents: write
permission-pull-requests: write

- uses: googleapis/release-please-action@45996ed1f6d02564a971a2fa1b5860e934307cf7 # v5
id: release
with:
Expand All @@ -37,3 +42,32 @@ jobs:
uses: ./.github/workflows/release.yml
secrets:
PUBLIC_SOCKET_FIREWALL_TOKEN: ${{ secrets.PUBLIC_SOCKET_FIREWALL_TOKEN }}

# The provider packages do not depend on the SDK, so their publish jobs need
# no ordering against `publish` or against each other.
publish-workload-identity-github-actions:
needs: release-please
if: ${{ needs.release-please.outputs.github_actions_release_created == 'true' }}
uses: ./.github/workflows/release.yml
with:
package-path: packages/workload-identity-github-actions
secrets:
PUBLIC_SOCKET_FIREWALL_TOKEN: ${{ secrets.PUBLIC_SOCKET_FIREWALL_TOKEN }}

publish-workload-identity-gcp:
needs: release-please
if: ${{ needs.release-please.outputs.gcp_release_created == 'true' }}
uses: ./.github/workflows/release.yml
with:
package-path: packages/workload-identity-gcp
secrets:
PUBLIC_SOCKET_FIREWALL_TOKEN: ${{ secrets.PUBLIC_SOCKET_FIREWALL_TOKEN }}

publish-workload-identity-aws:
needs: release-please
if: ${{ needs.release-please.outputs.aws_release_created == 'true' }}
uses: ./.github/workflows/release.yml
with:
package-path: packages/workload-identity-aws
secrets:
PUBLIC_SOCKET_FIREWALL_TOKEN: ${{ secrets.PUBLIC_SOCKET_FIREWALL_TOKEN }}
16 changes: 16 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ name: Release

on:
workflow_dispatch:
inputs:
package-path:
description: 'Package to publish: "." for the SDK, or a path under packages/'
required: false
default: '.'
type: string
workflow_call:
inputs:
package-path:
description: 'Package to publish: "." for the SDK, or a path under packages/'
required: false
default: '.'
type: string
secrets:
PUBLIC_SOCKET_FIREWALL_TOKEN:
required: true
Expand All @@ -25,17 +37,21 @@ jobs:
with:
token: ${{ secrets.PUBLIC_SOCKET_FIREWALL_TOKEN }}

# Always at the repository root: it installs every workspace package.
- name: Install Dependencies
run: npm install

- name: Build Project
working-directory: ${{ inputs.package-path }}
run: npm run build

- name: Run Tests
working-directory: ${{ inputs.package-path }}
run: npm run test

- name: Teardown Socket Firewall
uses: workos/setup-socket-firewall/teardown@ca93dd8aa351f54f4729fe3377a9be23c631c25d

- name: Publish
working-directory: ${{ inputs.package-path }}
run: npm publish --tag latest --access public --provenance
10 changes: 10 additions & 0 deletions .github/workflows/workos-node-dep-bump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ jobs:
run: |
if [ "$EVENT_NAME" = "release" ]; then
TAG="$RELEASE_TAG"

# The packages under packages/ release from this repository too,
# with tags like @workos-inc/workload-identity-gcp-v1.0.0. Only an
# SDK release (vX.Y.Z) bumps the dependency in other repos.
if ! [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+ ]]; then
echo "Not an SDK release ($TAG) — skipping."
echo "skip=true" >> "$GITHUB_OUTPUT"
exit 0
fi

VERSION="${TAG#v}"
MAJOR=$(echo "$VERSION" | cut -d. -f1)
MINOR=$(echo "$VERSION" | cut -d. -f2)
Expand Down
5 changes: 4 additions & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
".": "12.4.0"
".": "12.4.0",
"packages/workload-identity-github-actions": "0.0.0",
"packages/workload-identity-gcp": "0.0.0",
"packages/workload-identity-aws": "0.0.0"
}
23 changes: 23 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,26 @@ Write American English in hand-maintained code, comments, docs and test names
Never respell a name or value that already ships: exported types, methods,
options, API fields and enum values, fixture payloads, generated files, and
third-party identifiers keep their spelling.

## Releases

release-please releases four packages from this repository: the SDK at the
root, and a provider package in each directory under `packages/`. A change to
a provider package must not release the SDK.

release-please counts a commit for the SDK unless every file it touches is
under `packages/`. Pull requests are squash-merged, so one pull request is one
commit. Only `feat`, `fix`, `perf` and `revert` commits, and breaking changes,
cause a release.

- A `feat` or `fix` pull request for a provider package touches only
`packages/<name>/`.
- A change to a provider package's dependencies goes in its own `chore(deps)`
pull request, with `packages/<name>/package.json` and the root
`package-lock.json` together. Merge it before the `feat` or `fix` pull
request that needs it.
- A `chore(deps)` pull request releases nothing. When a dependency change must
ship with no other change, follow it with a `fix` pull request that touches
only `packages/<name>/`.
- A pull request that changes the SDK and a provider package on purpose
releases both.
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,104 @@ if (!claims) {
console.log(claims.sub, claims.org_id, claims.permissions);
```

## Workload identity

A workload running in GitHub Actions, Google Cloud or AWS can call the WorkOS API without an API key. It exchanges the identity token its platform issues for a short-lived WorkOS access token. The SDK makes the exchange, caches the token and replaces it shortly before it expires.

Install the SDK and the provider package for your platform:

```
npm install @workos-inc/node @workos-inc/workload-identity-github-actions
```

Then pass `credentials` in place of an API key:

```ts
import { WorkOS, WorkloadIdentityCredentials } from '@workos-inc/node';
import { githubActions } from '@workos-inc/workload-identity-github-actions';

const workos = new WorkOS({
credentials: new WorkloadIdentityCredentials({
clientId: 'client_...', // the M2M application that has the trust rule
tokenUrl: 'https://<your token endpoint>/oauth2/token',
environmentId: 'environment_...', // optional
identityTokenProvider: githubActions('https://api.workos.com'),
}),
});

const organizations = await workos.organizations.listOrganizations();
```

`clientId` defaults to the `WORKOS_CLIENT_ID` environment variable, which is the only variable the credentials read. `tokenUrl` is required and `scope` is optional, and both are set only in code. The variable only fills in a `clientId` you leave out: workload identity is used only when you pass `credentials`. A client built with `credentials` ignores `WORKOS_API_KEY`, and passing both `credentials` and an API key is an error.

Calls that send the API key as the client secret throw an `ApiKeyRequiredException` on a client built with `credentials`, before any request is sent, so make them with a client built with an API key: `sso.getProfileAndToken`, a sealed session's `refresh`, and in `userManagement` the methods `authenticateWithCode`, `authenticateWithPassword`, `authenticateWithMagicAuth`, `authenticateWithRefreshToken`, `authenticateWithTotp`, `authenticateWithEmailVerification`, `authenticateWithOrganizationSelection`, `authenticateWithRadarSmsChallenge` and `authenticateWithRadarEmailChallenge`.

`tokenUrl` must be an absolute `https` URL. The request to it carries the workload's identity token and the response carries the access token, so any other URL is rejected when the credentials are constructed, with a `WorkloadIdentityError` whose code is `invalid_configuration`. That includes `http://localhost` and other loopback addresses.

`environmentId` is optional and is set only in code: no environment variable fills it. WorkOS binds a workload identity client to exactly one environment, so the value selects nothing. Setting it is a safety check: the SDK sends it as the `x-url-environment-id` header on every API request, and a value that names a different environment gets `403 environment_mismatch`. When it is not set, the header is not sent.

### Platform providers

Each platform has its own package. The argument is the audience the identity token is requested for, which must match the audience of the trust rule.

GitHub Actions. The job needs `permissions: id-token: write`:

```ts
import { githubActions } from '@workos-inc/workload-identity-github-actions';

const identityTokenProvider = githubActions('https://api.workos.com');
```

Google Cloud (Compute Engine, GKE, Cloud Run, Cloud Functions, App Engine):

```ts
import { gcpMetadata } from '@workos-inc/workload-identity-gcp';

const identityTokenProvider = gcpMetadata('https://api.workos.com');
```

AWS. The region is optional when the AWS SDK can resolve one:

```ts
import { awsSts } from '@workos-inc/workload-identity-aws';

const identityTokenProvider = awsSts('https://api.workos.com', 'us-east-1');
```

### Custom providers

A provider is any function that returns the identity token. Use a callback for a platform without a package:

```ts
const credentials = new WorkloadIdentityCredentials({
clientId: 'client_...',
tokenUrl: 'https://<your token endpoint>/oauth2/token',
identityTokenProvider: async () => {
const response = await fetch('https://identity.internal.example/token');
return response.text();
},
});
```

`tokenFile` reads the token from a file and `tokenEnv` from an environment variable, on every exchange, so a rotated token is picked up:

```ts
import { tokenEnv, tokenFile } from '@workos-inc/node';

// A file that another process writes, such as a sidecar or an earlier CI
// step that fetches a new token.
tokenFile('/run/workos/identity-token');
tokenEnv('MY_PLATFORM_JWT');
```

WorkOS accepts an identity token for one exchange. The SDK makes a new exchange about every four minutes. If the file or variable still holds the same token then, that exchange fails with `invalid_grant`. So use `tokenFile` and `tokenEnv` for a source that something rewrites before each exchange, or for a job shorter than four minutes. The platform providers (GitHub Actions, Google Cloud, AWS) fetch a new token every time.

`tokenFile` needs a file system, so it is not exported from `@workos-inc/node/worker`.

### Errors

A failed exchange throws a `WorkloadIdentityError`. Its `code` is the OAuth error code from the token endpoint (for example `invalid_grant`) or an SDK code (`exchange_failed`, `invalid_response`, `provider_failed`, `invalid_configuration`), `description` is the server's explanation, and `hint` says what to check. The error never contains the identity token: if the token endpoint repeats it, the SDK puts `[redacted]` in its place. The exchange is retried on network failures, 429 and 5xx responses. An API request that gets a 401 is retried once with a new token.

## Public Client Mode (Browser/Mobile/CLI)

For apps that can't securely store secrets, initialize with just a client ID:
Expand Down
3 changes: 2 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const ignores = [
'node_modules',
'dist',
'build',
'lib',
// Build output of the SDK and of every package under packages/.
'**/lib',
];

const tests = ['**/*.spec.{js,ts}'];
Expand Down
Loading