Sub topic 7
Requesting a token and choosing an audience
This chapter assumes you know basic GitHub Actions syntax and can read JSON. You should also know that the previous chapter's id-token: write permission is what allows a job to ask for a token. Here you will see what happens when a step actually makes that request, and how the audience value shapes the token you get back.
What the runner gives your job
When a job has id-token: write, the runner adds two environment variables to its steps:
ACTIONS_ID_TOKEN_REQUEST_URLis the endpoint on GitHub's token service.ACTIONS_ID_TOKEN_REQUEST_TOKENis a short-lived bearer credential. It authorizes the call to that endpoint and is not the OIDC token itself.
If the permission is missing, these variables do not exist. A step that tries to use them gets empty values, and the request fails.
The request flow
Requesting a token by hand
Most workflows use an action that does this for them. Doing it manually once shows what the action does.
curl -sS \
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" \
| jq -r '.value'Three details matter here:
- The request URL already contains a query string, so the audience is appended with
&, not?. - The response is JSON. The JWT is in the
valuefield, which is whyjq -r '.value'extracts it. - The JWT is a credential. Do not print it to the job log. If you store it in a variable for later steps, keep it out of the output.
What the audience does
The aud claim says which service the token is meant for. The token service writes whatever you pass in the audience parameter into aud. If you pass nothing, GitHub uses a default based on the repository owner's URL, such as https://github.com/octo-org.
The audience protects you against token replay. A verifier accepts a token only if aud matches the value it expects. A token minted for sts.amazonaws.com is useless against a service that expects a different audience.
| Provider | Typical audience |
|---|---|
| AWS STS | `sts.amazonaws.com` |
| Your own service | A value you define, such as `https://deploy.example.com` |
The audience is not a secret. It is a label, and the receiving side must check it. Both sides have to agree on the same string, character for character.
Letting an action request the token
In the AWS example, aws-actions/configure-aws-credentials performs the request for you. It asks for the audience sts.amazonaws.com by default, which matches the aud condition in the role's trust policy. You can change it with the action's audience input, but then the trust policy must change as well.
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1
audience: sts.amazonaws.comCommon mistakes
- Omitting
id-token: write. The environment variables are missing and the request fails. - Setting
permissionsat the job level and forgetting that it replaces the defaults. Addcontents: readback if the job needsactions/checkout. - Using a different audience in the workflow than in the trust policy. The cloud provider rejects the token.
- Logging the token while debugging.
Exercise
Your workflow requests a token with &audience=https://deploy.example.com, but the AWS trust policy requires sts.amazonaws.com for the aud condition. What happens, and what are two ways to fix it?
Show answer
AWS rejects the token because aud does not match the trust policy condition. Either request the token with audience=sts.amazonaws.com, or change the trust policy condition to accept https://deploy.example.com. The first option is usual for AWS.