Skip to content

Grant access to one resource

A binding with no resource covers your whole API. To let someone approve invoices in one account only, put the binding on that account. This page shows a grant on one resource, a hierarchy in which a grant on a parent reaches its children, conditions, and the list of what a caller can reach.

Grants on a resource are decided by the runtime check. The roles and permissions claims in a token describe the whole API and leave these grants out.

  • Roles in the audience’s catalog. See Define roles and grant them. The examples on this page use two more roles, created the same way: invoices.approver, which contains the permission invoices.approve, and invoices.exporter, which contains invoices.export.

  • 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.

  • An Admin API access token, to declare resource types and parent links. awctl has no command for those two objects, so this page sends the requests with curl. To get a token for a service account:

    Terminal window
    ADMIN_TOKEN=$(curl -s TOKEN_URL -u "SA_CLIENT_ID:SA_CLIENT_SECRET" \
    -d grant_type=client_credentials -d audience=a-01 | jq -r .access_token)

    Replace TOKEN_URL, SA_CLIENT_ID, and SA_CLIENT_SECRET with the values of the service account’s credential. See Point it at an install for the token URL and API_HOST.

A resource is a type and an ID, written TYPE/ID, such as account/acc-3. Both are your own strings: the type is a noun from your API, and the ID is whatever your API uses to identify the object. The identity server stores no list of your objects and doesn’t check that an ID exists. It compares the strings on bindings with the strings that your API sends in a check.

The types tenant, issuer, realm, and audience belong to the identity server. Choose other names for yours.

Add the resource’s type and ID to the binding:

Terminal window
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.approver \
--resource-type account --resource-id acc-3
Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g51g0h16x7x5

The subject can be a user, a group, or a client, as for any binding.

Your API then names the resource in its check, with the caller’s access token as the bearer:

Terminal window
curl -s https://API_HOST/v1/access:check \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"permission":"invoices.approve","resource":{"type":"account","id":"acc-3"}}'
{"decision":{"allowed":true,"reason":"binding at account/acc-3 grants invoices.approve","bindingId":"axb-g51g0h16x7x5"}}

The same question about the whole API is denied, because the grant covers one account:

{"decision":{"reason":"no binding grants invoices.approve on /"}}

A grant on one resource needs nothing more. Declare resource types, as in the next section, when a grant on one resource must reach others.

Let a grant reach the resources underneath

Section titled “Let a grant reach the resources underneath”

An invoice belongs to an account. To make a grant on the account count for its invoices, tell the identity server two things: that the type invoice has the parent type account, and which account each invoice belongs to.

  1. Declare the parent type. It has no parent of its own:

    Terminal window
    curl -s https://API_HOST/v1/tenants/TENANT_ID/issuers/ISSUER_ID/audiences/AUDIENCE_ID/access-resource-types \
    -H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
    -d '{"name":"account","description":"A customer account"}'
    {"name":"tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-resource-types/account","description":"A customer account"}
  2. Declare the child type, with its parent type and the mode PUSH. In this mode, you send the identity server each parent link:

    Terminal window
    curl -s https://API_HOST/v1/tenants/TENANT_ID/issuers/ISSUER_ID/audiences/AUDIENCE_ID/access-resource-types \
    -H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
    -d '{"name":"invoice","description":"An invoice","parent_type":"account","expansion_mode":"PUSH"}'
    {"name":"tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-resource-types/invoice","description":"An invoice","parentType":"account","expansionMode":"PUSH"}
  3. When your API creates an invoice, link it to its account:

    Terminal window
    curl -s https://API_HOST/v1/tenants/TENANT_ID/issuers/ISSUER_ID/audiences/AUDIENCE_ID/access-resource-links \
    -H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
    -d '{"resource_type":"invoice","resource_id":"inv-9","parent_type":"account","parent_id":"acc-3"}'
    {"name":"tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-resource-links/axl-g51g0h16x7wi","resourceType":"invoice","resourceId":"inv-9","parentType":"account","parentId":"acc-3"}
  4. Check the permission on the invoice. The grant on the account reaches it:

    Terminal window
    curl -s https://API_HOST/v1/access:check \
    -H "Authorization: Bearer ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"permission":"invoices.approve","resource":{"type":"invoice","id":"inv-9"}}'
    {"decision":{"allowed":true,"reason":"binding at account/acc-3 grants invoices.approve","bindingId":"axb-g51g0h16x7x5"}}

    An invoice with no link is on its own, and the same check on it is denied:

    {"decision":{"reason":"no binding grants invoices.approve on invoice/inv-10"}}

What to know about links:

  • A resource has one parent. A second link for the same resource is refused with 409 and entity already exists. To move a resource, delete its link and create another.
  • Links are yours to keep current. Create the link when your API creates the object, and delete it when your API deletes the object. To delete a link, send DELETE to the name that the create returned, under https://API_HOST/v1/.
  • A missing link denies. Until an object is linked, only grants on the object itself and on the whole API reach it.
  • A hierarchy can be deeper. Give account a parent type of its own, and a grant at the top reaches every level below.

If your resource IDs already contain the path to the parent, declare the type with the mode PATH, and you send no links. The identity server reads the parent from the ID:

Terminal window
curl -s https://API_HOST/v1/tenants/TENANT_ID/issuers/ISSUER_ID/audiences/AUDIENCE_ID/access-resource-types \
-H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
-d '{"name":"document","description":"A document","parent_type":"folder","expansion_mode":"PATH"}'
{"name":"tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-resource-types/document","description":"A document","parentType":"folder","expansionMode":"PATH"}

Declare the parent type folder as well, with no parent of its own. With a binding on the folder whose ID is folders/f-1, a check on the document folders/f-1/documents/d-7 is allowed through the folder:

{"decision":{"allowed":true,"reason":"binding at folder/folders/f-1 grants invoices.approve","bindingId":"axb-g54uczbi7u0m"}}

The ID of a PATH resource alternates collection names and IDs, and the parent’s ID is the child’s ID without its last two segments.

A third mode, PULL, has the identity server ask an endpoint of yours for a resource’s parent at check time. This page doesn’t cover it.

A condition is an expression that must be true for a binding to count. It reads attributes that your API sends with the check. Conditions are written in the Common Expression Language.

  1. Create the condition:

    Terminal window
    awctl access access-conditions create \
    --tenant-id TENANT_ID --issuer-id ISSUER_ID --audience-id AUDIENCE_ID \
    --display-name office_network \
    --expression '"network" in request && request.network == "office"' \
    --description "The request came from the office network"
    Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-conditions/axc-g54uczbi7u07

    The last part of the name, axc-…, is the condition’s ID.

  2. Create a binding that names the condition:

    Terminal window
    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.exporter \
    --condition-id CONDITION_ID --expires-at 2027-01-31T00:00:00Z
    Created tenants/t-g4jkjj3z3cb0/issuers/i-g4jmsdt74oi1/audiences/a-g51g0h16x7vy/access-bindings/axb-g54uczbi7u2x

    This binding also has an expiry time. After it, the binding stops counting.

  3. Send the attribute with the check:

    Terminal window
    curl -s https://API_HOST/v1/access:check \
    -H "Authorization: Bearer ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"permission":"invoices.export","attributes":{"request.network":"office"}}'
    {"decision":{"allowed":true,"reason":"binding at / grants invoices.export","bindingId":"axb-g54uczbi7u2x"}}

    With another value, or without the attribute, the check is denied:

    {"decision":{"reason":"a binding grants invoices.export on / only under condition axc-g54uczbi7u07, which did not hold"}}

Rules for expressions:

  • Three variables exist: request, subject, and resource. Each is a map of the attributes whose key starts with that name. The attribute request.network is request.network in the expression. An attribute with any other prefix is dropped.
  • Your API supplies every attribute. The identity server fills in none, not the caller’s address and not the subject’s ID.
  • Values are strings. Compare them as strings, such as request.network == "office".
  • Test for an attribute before reading it. An expression that reads an attribute that the request didn’t send fails the check with a 500 error instead of a denial. Start the expression with "network" in request &&, as in the example.
  • Test the condition with a check after you create it. The identity server stores the expression as written, and a mistake in it appears only when a check evaluates it.

To list the resources of one type where the caller holds a permission, call lookupResources with the caller’s access token:

Terminal window
curl -s https://API_HOST/v1/access:lookupResources \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"permission":"invoices.approve","resource_type":"invoice"}'
{"resources":[{"type":"invoice","id":"inv-9"}]}

The identity server considers the resources of that type that it knows of: the IDs that a binding is on, and the IDs that a parent link names. It runs the ordinary check on each and returns the ones that are allowed. An object that has neither a binding nor a link isn’t in the answer, even when a grant on the whole API would reach it.

The type must be declared. An undeclared type answers 501. A type with more than 1,000 known resources answers an error instead of a partial list.

Define resource types and conditions with Terraform

Section titled “Define resource types and conditions with Terraform”

The provider has resources for the types, the condition, and the binding. It has none for parent links: send those from your API, as shown earlier. This example continues the configuration in Define the catalog with Terraform.

resource "authwise_access_resource_type" "account" {
audience_id = authwise_audience.invoices.audience_id
access_resource_type_id = "account"
description = "A customer account"
}
resource "authwise_access_resource_type" "invoice" {
audience_id = authwise_audience.invoices.audience_id
access_resource_type_id = "invoice"
parent_type = authwise_access_resource_type.account.access_resource_type_id
expansion_mode = "PUSH"
description = "An invoice"
}
resource "authwise_access_condition" "office" {
audience_id = authwise_audience.invoices.audience_id
display_name = "office_network"
expression = "\"network\" in request && request.network == \"office\""
description = "The request came from the office network"
}
resource "authwise_access_binding" "editor_on_account" {
audience_id = authwise_audience.invoices.audience_id
subject_type = "user"
subject_id = "USER_ID"
role_name = authwise_access_role.editor.access_role_id
resource_type = "account"
resource_id = "acc-3"
condition_id = authwise_access_condition.office.access_condition_id
expires_at = "2027-01-31T00:00:00Z"
depends_on = [authwise_access_role_access_permissions.editor]
}