Sub topic 7
Requesting a token and choosing an audience
This chapter assumes you can read a GitHub Actions workflow, know what a job's permissions block does, and have met JWT claims in earlier chapters. Here you will request an OIDC token from inside a running job, choose its audience, and inspect the result.
What "requesting a token" means
A workflow does not receive an OIDC token automatically. Each job can ask GitHub's token service for one. The runner gives the job two environment variables for this purpose, and only when the job has the id-token: write permission:
| Variable | Purpose |
|---|---|
| `ACTIONS_ID_TOKEN_REQUEST_URL` | The endpoint to call. It already contains a query string. |
| `ACTIONS_ID_TOKEN_REQUEST_TOKEN` | A bearer token that authorizes the call to that endpoint. |
The bearer token is not the OIDC token. It only proves to GitHub that the request comes from a legitimate job. GitHub's response contains the real OIDC token, a signed JWT.
If the permission is missing, both variables are absent. Most "cannot request token" errors come from this.
Step 1: grant the permission
Set id-token: write where the job can see it. A permissions block replaces the default set. Any permission you do not list becomes none, so list what the job still needs, such as contents: read for checkout.
permissions:
id-token: write
contents: readYou can put this at the top of the workflow or inside a single job. Job-level placement follows least privilege better, because only the job that needs a token can request one.
The permission does not give the job access to anything. It only lets the job obtain a token, and the cloud trust policy decides what that token is worth.
Two cases need care:
- Pull requests from forks. The job's
GITHUB_TOKENis read-only for fork PRs, so you cannot rely onid-token: writethere. This is a useful safety property. Do not work around it withpull_request_targetwithout understanding the risk. - Reusable workflows. The calling workflow must grant
id-token: write, because a called workflow cannot gain permissions the caller did not give it.
Step 2: call the endpoint
Append the audience to the request URL with &audience=. Use & because the URL already has a ?.
name: inspect-oidc-token
on: workflow_dispatch
permissions:
id-token: write
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Request a token and show selected claims
run: |
TOKEN=$(curl -sS \
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" \
| jq -r '.value')
echo "::add-mask::$TOKEN"
payload=$(printf '%s' "$TOKEN" | cut -d. -f2 | tr '_-' '/+')
pad=$(( (4 - ${#payload} % 4) % 4 ))
payload="$payload$(printf '%*s' "$pad" '' | tr ' ' '=')"
printf '%s' "$payload" | base64 -d | jq '{iss, aud, sub, ref, job_workflow_ref}'How this works:
curlsends the request token as a bearer header. The JSON response looks like{"count": 1234, "value": "<jwt>"}.jq -r '.value'extracts the JWT string.::add-mask::tells the runner to hide the token in later log output.curldoes not do this for you, so mask the token before anything can print it.- A JWT has three dot-separated parts: header, payload, signature. The payload is base64url-encoded JSON. The commands swap the URL-safe characters (
-and_) for the standard ones (+and/), restore the=padding, and decode it.
The step prints only a few claims. Printing the whole token is unnecessary, and a token that was not masked would stay valid for its short lifetime.
Decoding a JWT does not verify it. This is for inspection only. The cloud provider does the real verification, using GitHub's public signing keys.
Using the toolkit instead of curl
Inside a JavaScript action or actions/github-script, core.getIDToken makes the same call for you. It also masks the token.
- uses: actions/github-script@v7
id: oidc
with:
script: |
const token = await core.getIDToken('api://my-internal-service');
const claims = JSON.parse(
Buffer.from(token.split('.')[1], 'base64url').toString()
);
core.info(`aud=${claims.aud} sub=${claims.sub}`);If you omit the argument, you get the default audience, described next. Most cloud-login actions, such as aws-actions/configure-aws-credentials, call this API internally. You rarely need the raw call unless you are talking to a service that has no ready-made action.
Choosing an audience
The aud claim says which service the token is meant for. The verifier must check that its own identifier appears in aud and reject tokens meant for someone else.
If you do not pass audience, GitHub sets aud to the URL of the repository owner, for example https://github.com/octo-org. That default identifies where the token came from, not who should accept it, so it makes a poor target for a trust policy. Choose an explicit audience that the receiving system expects.
| Receiver | Typical audience |
|---|---|
| AWS IAM OIDC provider | `sts.amazonaws.com` |
| Azure federated credential | `api://AzureADTokenExchange` |
| Google Cloud workload identity pool | The provider's full resource name, unless you set allowed audiences |
| HashiCorp Vault JWT/OIDC auth | The value in the role's `bound_audiences` |
| Your own API | A string you define, such as `api://my-internal-service` |
Setting the audience correctly gives you two things:
- Confinement. A token minted for
sts.amazonaws.comis useless to Vault or to your internal API, even if it leaks to them. - Separation. You can run several systems that trust GitHub without any of them accepting tokens meant for another.
The audience string in the request and the audience in the trust policy must match exactly. Case, trailing slashes, and the scheme all count.
If the value contains characters such as : or / and your client does not encode URLs for you, percent-encode it in the query string. curl --data-urlencode with -G does this cleanly.
Token lifetime and when to request
The token is short-lived, valid for minutes rather than hours. Request it right before you need it, and exchange it immediately. If a long-running job sits idle between the request and the exchange, the token can expire. Request a new token and do not cache one in a file or artifact.
Each call returns a fresh token. You can request tokens for different audiences within one job, for example one for AWS and one for an internal service. They carry the same identity claims and differ in aud.
Common failures at this stage
- Both variables are empty or the step fails with an authorization error. The job lacks
id-token: write. Check the job's effective permissions, including any workflow-level block that overrides defaults, and check whether the run came from a fork. parse errorornullfromjq. The response was not JSON, often because the URL was built incorrectly. Make sure you used&audience=and did not drop the existing query string. Run without-stemporarily and without printing secrets, or print only the HTTP status.- The cloud rejects the token for audience mismatch. The
audyou requested differs from what the provider is configured to accept. Decode the payload as shown above and compare it character by character with the trust configuration. - The action works on
mainbut not on a pull request. Thesubclaim differs by event, which is a trust-policy issue and not a request issue. The subject claim chapter covers it.
Exercise
A job sets permissions: { contents: read } at the workflow level and calls curl "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com". The command fails because the URL variable is empty. What is the fix, and why is it safest to apply it at job level?
Show answer
Add id-token: write to the permissions block. Setting permissions replaces the defaults, and id-token is not granted unless listed, so the runner never sets the request variables. Putting it on only the job that needs the token follows least privilege, because other jobs in the workflow cannot request an OIDC token.
Exercise
You request a token without an audience parameter and decode it. What does aud contain, and why is that a poor value to trust in a cloud policy?
Show answer
It contains the URL of the repository owner, such as https://github.com/octo-org. It names the issuer's organization and not the receiving service, so it does not stop a token meant for one system from being accepted by another. Request an explicit audience that the receiver expects, such as sts.amazonaws.com for AWS.
Exercise
Why must you call ::add-mask:: on the token when you fetch it with curl, and what does core.getIDToken do differently?
Show answer
The runner only hides values it has been told are secret. A token fetched with curl is ordinary output unless you mask it, so a later echo or debug step could print a valid token to the log. core.getIDToken masks the token for you, so you do not need to call ::add-mask:: yourself.