Skip to content

Define roles and grant them

Create the permissions your API checks, group them into roles, and grant those roles. This page uses an invoicing API as its example: three permissions, two roles, and a grant to a person, a group, and a service. For the model behind these objects, see Authorize with Authwise Access.

  • An issuer with a realm. See Domains and issuers and Realms.
  • 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 IDs of the tenant and the issuer. The commands on this page pass them as --tenant-id TENANT_ID --issuer-id ISSUER_ID. To store them once instead, set an identity context.

The catalog belongs to an audience, the object that stands for your API. Applications request tokens for it, and the audience’s display name is the value of the aud claim in those tokens. If your API already has an audience, use it and skip this step.

Terminal window
awctl identity audiences create --tenant-id TENANT_ID --issuer-id ISSUER_ID \
--display-name "https://invoices.example.com"
Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy

The last part of the name, a-…, is the audience’s ID. The commands that follow pass it as --audience-id AUDIENCE_ID. To find it again, list the issuer’s audiences:

Terminal window
awctl identity audiences list --tenant-id TENANT_ID --issuer-id ISSUER_ID
ID DISPLAY_NAME STATUS
a-g4jmqya2jm31 a-g4jmqya2jm31 active
a-g51g0h16x7vy https://invoices.example.com active

A permission is one action that your API allows. Its name is what your API matches on, so choose names you can keep. A common form is the service, a dot, and the action, such as invoices.read. The names in Names you can’t use are refused.

To create a permission, pass its name, and the service it belongs to:

Terminal window
awctl access access-permissions create invoices.read \
--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
--service invoices --description "Read invoices"
awctl access access-permissions create invoices.write \
--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
--service invoices --description "Create and change invoices"
awctl access access-permissions create invoices.approve \
--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
--service invoices --description "Approve invoices"
Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-permissions/invoices.read
Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-permissions/invoices.write
Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-permissions/invoices.approve

A role is a named set of permissions. The role’s name is the value that the roles claim carries and that a binding references. The display name is for people and can change.

  1. Create the roles:

    Terminal window
    awctl access access-roles create invoices.viewer \
    --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
    --display-name "Invoices viewer"
    awctl access access-roles create invoices.editor \
    --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
    --display-name "Invoices editor"
    Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-roles/invoices.viewer
    Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-roles/invoices.editor
  2. Add permissions to each role. The role comes first, then one or more permissions:

    Terminal window
    awctl access access-roles add-access-permissions invoices.viewer invoices.read \
    --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID
    awctl access access-roles add-access-permissions invoices.editor invoices.read invoices.write \
    --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID
    Updated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-roles/invoices.viewer
    Updated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-roles/invoices.editor
  3. Confirm what a role contains:

    Terminal window
    awctl access access-roles list-access-permissions invoices.editor \
    --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID
    ID
    invoices.read
    invoices.write

To take a permission out of a role, use remove-access-permissions with the same arguments.

A grant is a binding: a subject holds a role. A binding with no resource covers the whole API. To grant a role on one account or one document instead, see Grant access to one resource.

The subject is one of three types:

--subject-type--subject-idWho
userA person’s ID, u-…One person in a realm of the tenant.
groupA group’s ID, g-…Every member of a group in a realm of the tenant.
clientAn application’s ID, c-…An application of the audience’s issuer, such as a service that uses the client_credentials grant.

To find a person’s ID or a group’s ID, list the realm:

Terminal window
awctl identity users list --tenant-id TENANT_ID --realm-id REALM_ID
awctl identity groups list --tenant-id TENANT_ID --realm-id REALM_ID
ID DISPLAY_NAME
u-g54uczbi7tzv Docs Example User
ID DISPLAY_NAME
g-g54uczbi7u1h Finance

To grant a role, create a binding:

Terminal window
awctl access access-bindings create \
--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
--subject-type user --subject-id USER_ID --role-name invoices.viewer
awctl access access-bindings create \
--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
--subject-type group --subject-id GROUP_ID --role-name invoices.viewer
awctl access access-bindings create \
--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
--subject-type client --subject-id CLIENT_ID --role-name invoices.editor
Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g51g0h16x7wb
Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g51g0h16x7ws
Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g54uczbi7tzx

Replace USER_ID, GROUP_ID, and CLIENT_ID with the IDs of the person, the group, and the application.

To give the grant an end, add --expires-at with an RFC 3339 time, such as --expires-at 2027-01-31T00:00:00Z. The binding stops counting at that time, and you don’t need to delete it for the grant to end.

To see every grant in the audience:

Terminal window
awctl access access-bindings list \
--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID
ID SUBJECT_TYPE SUBJECT_ID ROLE_NAME
axb-g51g0h16x7wb user u-g54uczbi7tzv invoices.viewer
axb-g51g0h16x7ws group g-g54uczbi7u1h invoices.viewer
axb-g54uczbi7tzx client c-g51g0h16x7w7 invoices.editor

To also see the resource, condition, and expiry of each grant, add --fields subject_type,subject_id,role_name,resource_type,resource_id,condition_id,expires_at.

To revoke a grant, delete its binding:

Terminal window
awctl access access-bindings delete BINDING_ID \
--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID --yes
Deleted tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g54uczbi7u0e

A runtime check reflects the change on the next request. A token that was already issued keeps its roles and permissions claims until it expires.

The same audience and catalog as configuration. USER_ID is the person’s ID:

resource "authwise_audience" "invoices" {
display_name = "https://invoices.example.com"
}
resource "authwise_access_permission" "read" {
audience_id = authwise_audience.invoices.audience_id
access_permission_id = "invoices.read"
service = "invoices"
description = "Read invoices"
}
resource "authwise_access_permission" "write" {
audience_id = authwise_audience.invoices.audience_id
access_permission_id = "invoices.write"
service = "invoices"
description = "Create and change invoices"
}
resource "authwise_access_role" "editor" {
audience_id = authwise_audience.invoices.audience_id
access_role_id = "invoices.editor"
display_name = "Invoices editor"
}
resource "authwise_access_role_access_permissions" "editor" {
access_role = authwise_access_role.editor.name
access_permissions = [
authwise_access_permission.read.name,
authwise_access_permission.write.name,
]
}
resource "authwise_access_binding" "editor" {
audience_id = authwise_audience.invoices.audience_id
subject_type = "user"
subject_id = "USER_ID"
role_name = authwise_access_role.editor.access_role_id
depends_on = [authwise_access_role_access_permissions.editor]
}

authwise_access_role_access_permissions owns the whole set of permissions on its role. A permission that someone adds to the role elsewhere is removed on the next apply. The binding depends on that set so that the role is complete before anyone holds it. For the provider’s configuration, see Terraform provider.

In the console, open the audience and go to its Access tab. The tab lists the people, groups, and applications that hold a role, the roles, and the permissions. To create a role, click New role. To create a binding, click Grant a role.

The identity server refuses a write that would look like a grant and grant nothing:

You try toThe server answers
Add a permission that doesn’t exist to a roleInvalidArgument: no such permission in this audience. Create the permission first.
Bind a person or group that isn’t in a realm of the audience’s tenantInvalidArgument: does not exist in any realm of tenant.
Create a permission or role in a reserved namespacePermissionDenied: is in a reserved namespace. See Names you can’t use.
Bind a role on an issuer, a realm, or an audienceInvalidArgument: the anchor grants nothing and is refused. Leave the resource empty for the whole API.
Create the same binding twiceALREADY_EXISTS.