Sub topic 2
What is inside the OIDC token
This chapter assumes you know what a workflow, job, and branch are, can read GitHub Actions YAML, and know that a JSON Web Token (JWT) is a signed, encoded set of statements. It also assumes you have a basic idea of what an IAM role and trust policy are. The examples use GitHub Actions and AWS IAM.
Why the token's contents matter
A trust policy cannot inspect your repository, your workflow file, or your branch protection settings. It can only compare values that appear in the token. The token is the only evidence the cloud provider has about who is asking. If you don't know which claims exist and what values they take, you can't write a condition that means what you think it means.
The rest of this tutorial builds conditions out of these claims, so learn their shape now.
Anatomy of a JWT
A JWT is three base64url-encoded parts joined by dots:
- Header. Names the signing algorithm (
alg, for GitHubRS256) and the key ID (kid) used to sign. - Payload. A JSON object of claims. A claim is one named statement, such as "the repository is example-org/example-repo".
- Signature. Proves the issuer created the header and payload and that nobody changed them.
The payload is encoded, not encrypted. Anyone who holds the token can read it. That is fine, because the security comes from the signature, not from hiding the contents.
The signature check works like this. GitHub publishes its public signing keys at a JWKS (JSON Web Key Set) URL under the issuer's /.well-known/ path. AWS fetches those keys through the IAM OIDC identity provider you registered. AWS verifies the signature, then checks that the token has not expired, and only then evaluates your trust policy conditions against the claims.
The claims GitHub puts in the token
GitHub's issuer is https://token.actions.githubusercontent.com. A token requested from a job running on a push to main in a job with no environment looks like this once decoded. The values are examples.
{
"jti": "example-unique-token-id",
"sub": "repo:example-org/example-repo:ref:refs/heads/main",
"aud": "sts.amazonaws.com",
"ref": "refs/heads/main",
"sha": "0123456789abcdef0123456789abcdef01234567",
"repository": "example-org/example-repo",
"repository_owner": "example-org",
"repository_id": "123456789",
"repository_owner_id": "987654",
"repository_visibility": "private",
"actor": "octocat",
"workflow": "deploy",
"event_name": "push",
"ref_type": "branch",
"ref_protected": "false",
"job_workflow_ref": "example-org/example-repo/.github/workflows/deploy.yml@refs/heads/main",
"runner_environment": "github-hosted",
"iss": "https://token.actions.githubusercontent.com",
"iat": 1764864000,
"nbf": 1764863700,
"exp": 1764865200
}The claims fall into groups.
| Group | Claims | What they tell you |
|---|---|---|
| Token metadata | `iss`, `aud`, `iat`, `nbf`, `exp`, `jti` | Who issued the token, who it is for, when it is valid, and a unique ID |
| Identity of the request | `sub` | A single string that combines repository and context |
| Repository | `repository`, `repository_owner`, `repository_id`, `repository_owner_id`, `repository_visibility` | Which repository and owner the job belongs to |
| Git context | `ref`, `ref_type`, `sha`, `head_ref`, `base_ref`, `ref_protected` | The branch or tag and commit, and for pull requests the source and target branches |
| Run context | `event_name`, `workflow`, `run_id`, `run_number`, `run_attempt`, `actor`, `actor_id` | What triggered the run and who started it |
| Workflow identity | `workflow_ref`, `job_workflow_ref`, `job_workflow_sha` | Which workflow file ran, and for reusable workflows, which one was called |
| Deployment | `environment` | The environment name. It appears only when the job sets `environment:` |
Some of these appear only in certain situations. head_ref and base_ref are populated for pull request events and empty otherwise. environment is absent from jobs without an environment: key. Do not write a condition on a claim that a given kind of job may not have.
The three claims that do most of the work
iss says which identity provider signed the token. In AWS, the OIDC provider you register is tied to this issuer URL, and the provider hostname prefixes the condition keys (token.actions.githubusercontent.com:sub).
aud says who the token is intended for. The audience is whatever the requester asked for. The AWS credentials action asks for sts.amazonaws.com. A token minted for some other audience should not be accepted by your role, so your trust policy should pin aud. The next chapter covers that.
sub is the claim trust policies rely on most. It is a colon-separated string. The default formats are:
| Job context | Default `sub` value |
|---|---|
| Branch push, no environment | `repo:example-org/example-repo:ref:refs/heads/main` |
| Tag push | `repo:example-org/example-repo:ref:refs/tags/v1.0.0` |
| Pull request event | `repo:example-org/example-repo:pull_request` |
| Job with an environment | `repo:example-org/example-repo:environment:production` |
Notice the last row. When a job sets environment:, the default sub carries the environment name and not the branch. A trust policy that matches ref:refs/heads/main will reject a job that targets the production environment, even if that job runs on main. Later chapters use this on purpose.
Which claims AWS can see
AWS exposes the token's claims to IAM conditions through keys prefixed with the provider hostname. For GitHub, the keys you will use constantly are token.actions.githubusercontent.com:aud and token.actions.githubusercontent.com:sub. IAM supports only a limited set of claims in the Condition block, so a claim being present in the token does not mean you can match on it. Check the current AWS documentation for the list before you plan a policy around a claim such as job_workflow_ref. This is one reason sub carries so much weight, and why a later chapter covers customizing it.
Looking at a real token
Reading a decoded example is useful, but seeing your own workflow's token teaches more. This workflow requests a token and prints only the decoded payload. It uses the two environment variables GitHub sets when the job has id-token: write.
name: inspect-oidc-token
on: workflow_dispatch
permissions:
id-token: write
contents: read
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Print decoded 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"
export TOKEN
python3 - <<'PY'
import base64, json, os
payload = os.environ["TOKEN"].split(".")[1]
payload += "=" * (-len(payload) % 4)
claims = json.loads(base64.urlsafe_b64decode(payload))
print(json.dumps(claims, indent=2, sort_keys=True))
PYThe &audience= parameter shows where aud comes from: the requester chooses it. The Python code pads the base64url text because JWTs drop the trailing = characters that the decoder expects.
Run it, then add environment: production to the job and run it again. The sub value changes from the ref: form to the environment: form, and an environment claim appears.
Common errors when reading claims
- Assuming
subalways contains the branch. Jobs with an environment, and pull request events, produce subjects with no branch in them. - Matching on a claim that is empty for the event.
head_refis empty on a push, so a condition on it never matches there. - Confusing
repositorywithsub. They overlap, but the trust policy sees the one you name. Matchingrepositoryis not possible in AWS unless the key is supported, whereassubalways is. - Trusting the decoded value of a token from an unknown source. Decoding shows content. Verification is separate.
- Forgetting
id-token: write. Without it the job has no request URL and the token call fails, even if the role is configured correctly.
Exercise
A job runs on the main branch of example-org/example-repo and has environment: staging set. You have a trust policy that requires sub to equal repo:example-org/example-repo:ref:refs/heads/main. Will the job be able to assume the role? Explain which claims you would look at to confirm your answer.
Show answer
No. Because the job sets an environment, the default sub is repo:example-org/example-repo:environment:staging, which does not equal the branch-form string, so the condition fails. To confirm, decode the token payload in a test run and read sub. The ref claim would still say refs/heads/main and the environment claim would say staging, but the trust policy compares only sub, so those other values do not help.