Check a permission from your API
Your API can ask the identity server whether the caller is allowed to do something, at the moment of the request. The answer reflects every grant as it stands at that moment, including grants on one resource and grants with conditions, which the token’s claims don’t carry. This page shows the request, the response, and the errors.
To decide from the token alone instead, see Read roles and permissions from the token.
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.
- Identity server 1.98.0 or later.
- The Admin API host of the install that serves your issuer, reachable
from your API’s backend. The examples call it
API_HOST.- On the Authwise service, the host is
api.authwise.com. - On your own install, it’s a host from the install’s
networking.api.hosts. If you put only part of the Admin API on a public hostname, as in Expose part of the Admin API, the paths under/v1/access:stay private. Call the API from inside the cluster, or add that prefix to the public paths.
- On the Authwise service, the host is
How the check identifies the caller
Section titled “How the check identifies the caller”The request carries no subject. The identity server takes the subject from the access token that you send as the bearer:
- The token’s
subis the subject: the person, or the application for aclient_credentialstoken. - The token’s
audselects the catalog: the audience that the token was issued for. - The token’s
issmust be an issuer of the install you call. The identity server verifies the signature against that issuer’s keys, and the token must not be expired.
Your API doesn’t need a credential of its own. It forwards the access token that it received from the caller, and the answer is about that caller. An API can’t ask about someone else with this endpoint.
Check one permission
Section titled “Check one permission”Send the permission’s name. With no resource, the question is about the whole API:
curl -s https://API_HOST/v1/access:check \ -H "Authorization: Bearer ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d '{"permission":"invoices.write"}' | jq{ "decision": { "allowed": true, "reason": "binding at / grants invoices.write", "bindingId": "axb-g54uczbi7tzx" }}Replace ACCESS_TOKEN with the access token that the caller sent to your
API.
When the caller isn’t allowed, the response has no allowed field:
curl -s https://API_HOST/v1/access:check \ -H "Authorization: Bearer ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d '{"permission":"invoices.approve"}'{"decision":{"reason":"no binding grants invoices.approve on /"}}| Field | Value |
|---|---|
decision.allowed | true when the caller is allowed. Absent when the caller isn’t. |
decision.reason | A sentence for logs and for people. Don’t parse it. |
decision.bindingId | The binding that granted the permission. Absent on a denial. |
Allow the request only when the response status is 200 and
decision.allowed is true. Treat every other outcome, including an
error or a timeout, as a denial.
Check several permissions in one request
Section titled “Check several permissions in one request”To decide what to show on a page, or to check a batch, send up to 100 checks in one request. The decisions come back in the same order:
curl -s https://API_HOST/v1/access:bulkCheck \ -H "Authorization: Bearer ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d '{"checks":[{"permission":"invoices.read"},{"permission":"invoices.write"},{"permission":"invoices.approve"}]}'{"decisions":[{"allowed":true,"reason":"binding at / grants invoices.read","bindingId":"axb-g54uczbi7tzx"},{"allowed":true,"reason":"binding at / grants invoices.write","bindingId":"axb-g54uczbi7tzx"},{"reason":"no binding grants invoices.approve on /"}]}Each entry of checks takes the same fields as a single check, so one
request can mix permissions and resources.
Check a permission on one resource
Section titled “Check a permission on one resource”To ask about one object in your API, add resource with the object’s type
and ID. A grant on the resource counts, and so does a grant on anything
above it, up to the whole API. To set up such grants, see
Grant access to one resource.
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"}}The reason names the resource that the grant is on. Here the caller
holds the role on the account that contains the invoice.
Set both type and id, or neither. The check doesn’t tell you whether
the resource exists: the identity server compares the strings that you
send with the grants that it holds. Confirm that the object exists in your
own data.
Send attributes for conditions
Section titled “Send attributes for conditions”A binding can carry a condition, an expression over attributes of the
request. Your API supplies those attributes in attributes, a map of
string keys to string values. Each key starts with request.,
subject., or resource.:
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 a value that the condition rejects, the reason says so:
{"decision":{"reason":"a binding grants invoices.export on / only under condition axc-g54uczbi7u07, which did not hold"}}The identity server fills in no attribute. Whoever calls the check chooses them, so call it from your backend with values that your backend established, and never pass values through from the caller unchecked. To write a condition, see Add a condition to a grant.
List the resources a caller can reach
Section titled “List the resources a caller can reach”To build a list, such as the accounts that the caller can approve invoices in, ask for every resource of one type where the caller holds a permission:
curl -s https://API_HOST/v1/access:lookupResources \ -H "Authorization: Bearer ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d '{"permission":"invoices.approve","resource_type":"account"}'{"resources":[{"type":"account","id":"acc-3"}]}The type must be one that the audience declares. The answer covers the resources that the identity server knows of: those that a binding names and those that a parent link names. See Find the resources a caller can reach.
Errors
Section titled “Errors”| Status | Body | Cause |
|---|---|---|
401 | Unauthenticated | No bearer, an expired or malformed token, a token whose issuer this install doesn’t serve, or a token whose aud isn’t an audience of its issuer. |
400 | check: permission is required | The request has no permission. |
400 | check: resource type and id must both be set, or both be empty for the platform root | resource has a type without an ID, or an ID without a type. |
400 | bulk_check: 101 checks exceeds the maximum of 100 | Too many checks in one request. |
500 | evaluate condition: … no such key | A condition reads an attribute that the request didn’t send. Send the attribute, or write the condition to test for it first. |
501 | resource type … cannot be enumerated | lookupResources was asked for a type that the audience doesn’t declare. |
A denial is not an error. It’s a 200 whose decision has no allowed
field.
What’s next
Section titled “What’s next”- Grant access to one resource.
- Read roles and permissions from the token for the decisions that don’t need a request to the identity server.