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.
Before you begin
Section titled “Before you begin”- 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
awctl0.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.
Create an audience for your API
Section titled “Create an audience for your API”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.
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-g51g0h16x7vyThe 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:
awctl identity audiences list --tenant-id TENANT_ID --issuer-id ISSUER_IDID DISPLAY_NAME STATUSa-g4jmqya2jm31 a-g4jmqya2jm31 activea-g51g0h16x7vy https://invoices.example.com activeCreate the permissions
Section titled “Create the permissions”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:
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.readCreated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-permissions/invoices.writeCreated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-permissions/invoices.approveCreate a role and add permissions to it
Section titled “Create a role and add permissions to it”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.
-
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.viewerCreated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-roles/invoices.editor -
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_IDawctl access access-roles add-access-permissions invoices.editor invoices.read invoices.write \--tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_IDUpdated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-roles/invoices.viewerUpdated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-roles/invoices.editor -
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_IDIDinvoices.readinvoices.write
To take a permission out of a role, use remove-access-permissions with
the same arguments.
Grant a role
Section titled “Grant a role”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-id | Who |
|---|---|---|
user | A person’s ID, u-… | One person in a realm of the tenant. |
group | A group’s ID, g-… | Every member of a group in a realm of the tenant. |
client | An 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:
awctl identity users list --tenant-id TENANT_ID --realm-id REALM_IDawctl identity groups list --tenant-id TENANT_ID --realm-id REALM_IDID DISPLAY_NAMEu-g54uczbi7tzv Docs Example UserID DISPLAY_NAMEg-g54uczbi7u1h FinanceTo grant a role, create a binding:
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.viewerawctl 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.viewerawctl 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.editorCreated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g51g0h16x7wbCreated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g51g0h16x7wsCreated tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g54uczbi7tzxReplace 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.
Review and revoke grants
Section titled “Review and revoke grants”To see every grant in the audience:
awctl access access-bindings list \ --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_IDID SUBJECT_TYPE SUBJECT_ID ROLE_NAMEaxb-g51g0h16x7wb user u-g54uczbi7tzv invoices.vieweraxb-g51g0h16x7ws group g-g54uczbi7u1h invoices.vieweraxb-g54uczbi7tzx client c-g51g0h16x7w7 invoices.editorTo 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:
awctl access access-bindings delete BINDING_ID \ --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID --yesDeleted tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g54uczbi7u0eA runtime check reflects the change on the
next request. A token that was already issued keeps its roles and
permissions claims until it expires.
Define the catalog with Terraform
Section titled “Define the catalog with Terraform”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.
Use the console
Section titled “Use the console”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.
What the server refuses
Section titled “What the server refuses”The identity server refuses a write that would look like a grant and grant nothing:
| You try to | The server answers |
|---|---|
| Add a permission that doesn’t exist to a role | InvalidArgument: 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 tenant | InvalidArgument: does not exist in any realm of tenant. |
| Create a permission or role in a reserved namespace | PermissionDenied: is in a reserved namespace. See Names you can’t use. |
Bind a role on an issuer, a realm, or an audience | InvalidArgument: the anchor grants nothing and is refused. Leave the resource empty for the whole API. |
| Create the same binding twice | ALREADY_EXISTS. |