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.
Before you begin
Section titled “Before you begin”- 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 containsinvoices.readandinvoices.write. - A tenant administrator seat, or a
service account that holds
tenant.admin. - Identity server 1.98.0 or later, with
awctl0.48.0 or later or Terraform provider 0.38.0 or later.
Turn the claims on
Section titled “Turn the claims on”The claims are off until you turn them on for the audience.
With awctl:
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.
Read the roles claim
Section titled “Read the roles claim”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:
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.
Ask for permissions
Section titled “Ask for permissions”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.
Ask for permissions by name
Section titled “Ask for permissions by name”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:
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.
Ask for a scope that groups permissions
Section titled “Ask for a scope that groups permissions”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.
-
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.permissionGroupsCreated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/scopes/invoices -
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_IDUpdated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/scopes/invoices -
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
permissionsclaim. The claim is absent, not empty. - Permission names in
scopestop 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.
Include a scope in every token
Section titled “Include a scope in every token”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:
awctl identity scopes update invoices \ --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \ --kind authorization.permissionGroups --autoUpdated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/scopes/invoicesA 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.
Which grants a token carries
Section titled “Which grants a token carries”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.
Carry every grant in the token
Section titled “Carry every grant in the 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.
-
Create a scope of the kind
authorization.bindings, namedauthz:Terminal window awctl identity scopes create authz \--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \--kind authorization.bindingsCreated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/scopes/authz -
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.
Enforce the claims in your API
Section titled “Enforce the claims in your API”- Validate the access token: its signature against the issuer’s keys,
its
iss, itsaud, and its expiry. See The access token. - Decide from
permissionswhere you can. A check forinvoices.writekeeps working when you rearrange which roles contain it. - Decide from
roleswhen 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.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
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 claim | The 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 ignored | The audience has a scope of the kind authorization.permissionGroups. | Request that scope instead. |
| A role or permission is missing | Its 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 expected | The claim carries the role’s name. | Match on the name. |