Skip to content

Read roles and permissions from the token

Your API can decide what a caller can do from the access token alone. The audience puts two claims in its tokens: roles, the roles that the caller holds, and permissions, the permissions that the caller holds among the ones the application asked for. This page turns the claims on and shows both ways to ask for permissions.

To decide per resource, with conditions, or without waiting for a token to expire, check the permission from your API instead. For a comparison, see Choose how your API enforces it.

  • Roles and bindings in the audience’s catalog. See Define roles and grant them. The examples on this page continue from that page: an application holds invoices.editor, which contains invoices.read and invoices.write.
  • A tenant administrator seat, or a service account that holds tenant.admin.
  • Identity server 1.98.0 or later, with awctl 0.48.0 or later or Terraform provider 0.38.0 or later.

The claims are off until you turn them on for the audience.

With awctl:

Terminal window
awctl identity audiences update AUDIENCE_ID \
--tenant-id TENANT_ID --issuer-id ISSUER_ID \
--config '{"scopes":{"authorization":{"include_roles":true,"include_permissions":true}}}'
Updated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy

--config replaces the audience’s whole configuration. If the audience already has settings, such as token lifetimes, read them with awctl identity audiences describe AUDIENCE_ID --json and include them.

With Terraform, build the configuration with the authwise_audience_config data source and set it on the audience:

data "authwise_audience_config" "invoices" {
scopes = {
authorization = {
include_roles = true
include_permissions = true
}
}
}
resource "authwise_audience" "invoices" {
display_name = "https://invoices.example.com"
config = data.authwise_audience_config.invoices.json
}

In the console, open the audience and, under What its access tokens carry, turn on The person’s roles and The person’s permissions.

With include_roles on, every access token for the audience carries the caller’s roles. The application doesn’t request anything.

To see the claims, get a token and decode its payload. This example uses a service application and the client_credentials grant:

Terminal window
curl -s ISSUER_URL/oauth/token -u "CLIENT_ID:CLIENT_SECRET" \
-d grant_type=client_credentials -d audience=AUDIENCE_ID \
| jq -r .access_token \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson'
{
"aud": "https://invoices.example.com",
"client_id": "c-g51g0h16x7w7",
"exp": 1795418317,
"iat": 1795411117,
"iss": "ISSUER_URL",
"jti": "8ae2bf91-dc4b-405c-b7e1-57f047c420c9",
"permissions": [],
"roles": [
"invoices.editor"
],
"sub": "c-g51g0h16x7w7"
}

Each value in roles is a role’s name, not its display name. The list is sorted and has no duplicates. It includes the roles that the caller holds through a group.

permissions is empty in this token, although the application holds two permissions. The next section explains why.

The permissions claim is scoped. It carries the permissions that the caller holds and that the request asked for. It never carries every permission that the caller holds by default, so a token contains only what the application needs for its task.

There are two ways to ask. Choose one for an audience, because an audience that has a permission scope of the second kind ignores the first.

The application adds the names of the permissions it wants to the scope parameter. A person’s sign-in sends scope in the authorization request; a service sends it in the token request:

Terminal window
curl -s ISSUER_URL/oauth/token -u "CLIENT_ID:CLIENT_SECRET" \
-d grant_type=client_credentials -d audience=AUDIENCE_ID \
--data-urlencode "scope=invoices.read invoices.approve" \
| jq -r .access_token \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson'
{
"aud": "https://invoices.example.com",
"client_id": "c-g51g0h16x7w7",
"exp": 1795418318,
"iat": 1795411118,
"iss": "ISSUER_URL",
"jti": "9eb53a11-1fcf-4bd3-b1a7-f2c7a02b71fe",
"permissions": [
"invoices.read"
],
"roles": [
"invoices.editor"
],
"scope": "invoices.read",
"sub": "c-g51g0h16x7w7"
}

The request asked for invoices.read and invoices.approve. The caller holds invoices.read and doesn’t hold invoices.approve, so the claim carries one of them. The token’s scope claim lists what was granted.

This way needs no configuration. It suits an application that needs a few permissions, because each one is a word in the request.

When an application needs many permissions, define one scope that stands for them. The application requests the scope, and the claim carries the permissions on that scope that the caller holds.

  1. Create a scope of the kind authorization.permissionGroups:

    Terminal window
    awctl identity scopes create invoices \
    --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
    --kind authorization.permissionGroups
    Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/scopes/invoices
  2. Add the permissions to the scope. The scope comes first, then one or more permissions:

    Terminal window
    awctl identity scopes add-access-permissions invoices \
    invoices.read invoices.write invoices.approve \
    --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID
    Updated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/scopes/invoices
  3. Request the scope:

    Terminal window
    curl -s ISSUER_URL/oauth/token -u "CLIENT_ID:CLIENT_SECRET" \
    -d grant_type=client_credentials -d audience=AUDIENCE_ID \
    --data-urlencode "scope=invoices" \
    | jq -r .access_token \
    | jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson'
    {
    "aud": "https://invoices.example.com",
    "client_id": "c-g51g0h16x7w7",
    "exp": 1795418318,
    "iat": 1795411118,
    "iss": "ISSUER_URL",
    "jti": "a67b6459-8d97-4227-81de-b2015f2b88c5",
    "permissions": [
    "invoices.read",
    "invoices.write"
    ],
    "roles": [
    "invoices.editor"
    ],
    "scope": "invoices",
    "sub": "c-g51g0h16x7w7"
    }

    The scope lists three permissions. The caller holds two of them, and the claim carries those two.

With Terraform, the scope and its permissions are two resources:

resource "authwise_scope" "invoices" {
audience_id = authwise_audience.invoices.audience_id
scope_id = "invoices"
kind = "authorization.permissionGroups"
}
resource "authwise_scope_access_permissions" "invoices" {
scope = authwise_scope.invoices.name
access_permissions = [
authwise_access_permission.read.name,
authwise_access_permission.write.name,
]
}

What to know about a scope that groups permissions:

  • An audience can have several, such as one for each area of the API. An application can request more than one, and the claim carries the permissions of all the requested scopes that the caller holds.
  • A token that requests none of them has no permissions claim. The claim is absent, not empty.
  • Permission names in scope stop working on that audience. Only the scopes fill the claim.
  • To delete the scope, remove its permissions first with remove-access-permissions. The server refuses to delete a scope that still has permissions.

To put a scope’s permissions in every token for the audience, whether or not the application requests the scope, set the scope to automatic:

Terminal window
awctl identity scopes update invoices \
--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
--kind authorization.permissionGroups --auto
Updated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/scopes/invoices

A token that requests no scope then carries the caller’s permissions among the ones on the scope:

"permissions": [
"invoices.read",
"invoices.write"
],

With Terraform, set auto = true on the authwise_scope.

The roles and permissions claims describe what the caller can do across the whole API. A binding counts toward them only when all of these are true:

  • It covers the whole API. A binding on one resource, such as account/acc-3, is left out.
  • It has no condition. A token can’t know the request that a condition reads.
  • It doesn’t expire before the token does.

A grant that the claims leave out still counts in a runtime check.

A token carries at most 100 roles and 500 permissions. When a caller holds more, the identity server refuses to issue the token instead of issuing one with a shortened list. Request fewer permissions, or use the runtime check.

The ID token and the userinfo endpoint carry the same two claims as the access token.

To put the grants that roles and permissions leave out in the token as well, the audience can add an authz claim. It lists each permission with the resource it’s granted on, and the expression of a condition when the grant has one.

  1. Create a scope of the kind authorization.bindings, named authz:

    Terminal window
    awctl identity scopes create authz \
    --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
    --kind authorization.bindings
    Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/scopes/authz
  2. Request the scope authz. The token gains the claim:

    "authz": [
    {
    "perm": "invoices.approve",
    "resource": "account/acc-3"
    },
    {
    "cond": "\"network\" in request && request.network == \"office\"",
    "perm": "invoices.export",
    "resource": ""
    },
    {
    "perm": "invoices.read",
    "resource": ""
    },
    {
    "perm": "invoices.write",
    "resource": ""
    }
    ],

An empty resource is the whole API. The identity server doesn’t evaluate the condition for this claim. Your API must evaluate cond itself, and a grant with a cond that your API ignores is a grant your API gives without its condition. The claim is also as old as the token: a revoked grant stays in it until the token expires.

  1. Validate the access token: its signature against the issuer’s keys, its iss, its aud, and its expiry. See The access token.
  2. Decide from permissions where you can. A check for invoices.write keeps working when you rearrange which roles contain it.
  3. Decide from roles when the API has only a few coarse levels.

A token keeps the claims it was issued with. After you revoke a grant, a token that was issued before keeps the old claims until it expires, so keep access token lifetimes short for an API that relies on claims.

SymptomCauseFix
permissions is []The request asked for no permission, or for none that the caller holds.Add permission names or a scope that groups permissions to scope.
The token has no permissions claimThe audience has a scope that groups permissions, and the request named none. Or include_permissions is off.Request the scope, set it to automatic, or turn the claim on.
Permission names in scope are ignoredThe audience has a scope of the kind authorization.permissionGroups.Request that scope instead.
A role or permission is missingIts binding is on one resource, has a condition, or expires before the token.Use the runtime check for that grant.
A role’s display name was expectedThe claim carries the role’s name.Match on the name.