# Access authorization
> The Authorization is intrinsically linked with the [Authentication](./identity.md).
## Directives
The Authorization is implemented as a set of [RTD Directives](tree.md#directives).
Directives are executed in a predetermined order until one of them grants access to a resource.
If none of the directives grants access, then the Authorization interrupts request processing and
responds with an authorization error.
> The Authorization directive provider is named `authorization`,
> so the full names of the directives are `authorization:{directive}`.
### `anonymous`
Grants access if its value is `true` and no credentials were provided[^1].
A [procedure](rpc.md) is the exception: it grants access whatever the request presented. The rule
is about a cacheable reply, and what a procedure answers is not one — it is a value in an envelope
the gateway answers `no-store`, and the procedure's own headers are discarded. So a request that
carries a credential reaches an `anonymous` route through `/.rpc` and `/.mcp`, where the same
request would be refused at the route itself. Describing one is the same exception: `OPTIONS` and
[discovery](discovery.md) answer what the route is whatever the request presented.
[^1]:
Credentials in the request make the
response [non-cacheable](https://datatracker.ietf.org/doc/html/rfc7234#section-3).
### `anyone`
Grants access if its value is `true` and valid credentials were provided.
### `id`
Grants access if resolved Identity matches the value of the URL path segment placeholder named after
the directive's value.
#### Example
Given the Route declaration and corresponding HTTP request:
```yaml
/users/:user-id:
id: 'user-id'
```
```http
GET /users/87480f2bd88048518c529d7957475ecd/
Authorization: ...
```
For this request access will be granted if the resolved Identity value
is `87480f2bd88048518c529d7957475ecd`.
### `role`
Grants access if resolved Identity has a role covering the scope the directive's value names, or
one of the scopes where the value is a list.
```yaml
/code:
role: [developer, reviewer]
```
Access will be granted if the resolved Identity has a role that covers `developer` or `reviewer`.
Read [Roles](#roles) section for more details.
#### Dynamic roles
The `role` directive can be used with a placeholder in the route.
```yaml
/:org-id:
role: app:{org-id}:moderator
```
### `claims`
Grants access if `Bearer` authentication scheme is used
and the Token's claims matches the specified values.
```yaml
/:
auth:claims:
iss: https://id.example.com
sub: someone
aud: stars
```
> If OIDC token claim contains `aud`
> as [an array](https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation), the
> directive will match if at least one value.
At least one property is required.
Values may refer to the Route parameters or the request authority:
```yaml
/secrets/:org-id:
auth:claims:
iss: https://id.org.com
sub: /:org-id
aud: :authority
```
An expression `:domain` will match if the domain in the value of `iss` matches the request
authority, excluding the most specific subdomain.
Issuer `https://accounts.example.com` matches request authorities `images.example.com`
and `sub.images.example.com`, but not `images.another.com`.
```yaml
/images/:user-id:
auth:claims:
iss: :domain
sub: /:org-id
```
### `rule`
The Rule is a collection of authorization directives. It allows access only if all the specified
directives grant access. The value of the `rule` directive can be a single Rule or a list of Rules.
#### Example
```yaml
/commits/:user-id:
rule:
id: user-id
role: developer
```
Access will be granted if an Identity matches a `user-id` placeholder and has a role covering
`developer`.
### `input`
Restricts access based on the request body (which must be an object).
```yaml
/commits/:id:
PUT:
auth:role: [developer, reviewer]
auth:input:
- prop: approved
role: reviewer
- prop: message
role: developer
```
The example above restricts access to the `approved` property of the request body to the identity
with the `reviewer` role, and the `message` property to the identity with the `developer` role.
> `auth:input` directive does not grant access by itself.
### `delegate`
Embeds the value of the current Identity into the request body as a property named after the value
of the directive value, and grants access.
The request body must be an object.
> :warning:
> The intended use case for this directive is audit.
> **Using it to pass Identity to the application logic is strongly discouraged.**
### `transfer`
Gives the value of the `authorization` header to the operation as the property of the input
named by the directive's value, and grants access. The input must be an object.
```yaml
/notifications:
POST:
auth:transfer: authorization
endpoint: ingest
```
For a caller that authenticates as itself, with a credential that is not the gateway's: a push of
a message broker, a webhook signed by its sender. The operation verifies it:
- the gateway does not read the header, so the call has no identity, and a credential of any
scheme reaches the operation as it was sent;
- without the header, the property is absent;
- the reply is never stored: `cache-control: no-store`;
- a [procedure](rpc.md) does not reach the method;
- no other `auth:` directive of its method is asked, whether declared on it or inherited.
## Roles
A role is a value assigned to an Identity, managed by the
[`identity.roles` component](components.md#roles). A scope is an area of access, named by a value
of the same form, which the [`role` directive](#role) requires and a token can be restricted to.
### Hierarchies
Role and scope values are alphanumeric tokens separated by a colon (`:`), and form a hierarchy:
`developer:senior` lies within `developer`, as does `developer` itself.
A role covers every scope within it, and grants access to each.
#### Example
```yaml
/commits/:user-id:
role: developer:senior
```
The directive above requires the `developer:senior` scope.
It is covered by the roles `developer:senior` and `developer`,
but it **is not** covered by the role `developer:senior:javascript`.
In other words, the Identity must have the specified or a more general role.
> The root-level scope `system` holds the scopes Toa's own resources require, such as
> `system:identity:roles`. The `system` role covers every one of them.
See also [role management resources](components.md#roles).
## Policies
Component Resource branches cannot have authorization directives.
Instead, they must declare Authorization Policies using `policy` directive to
be attached in the Context to a Resource Tree as a set of Authorization Directives
using `attachment` directive.
This restriction provides a separation of concerns, allowing components to be reused in different
Contexts with varying
access rules.
```yaml
# manifest.toa.yaml
name: posts
exposition:
/:user-id:
GET:
endpoint: observe
policy: read:list
POST:
endpoint: transit
policy: post:submit
/:post-id:
GET:
endpoint: observe
policy: read:post
PUT:
endpoint: assign
policy: post:edit
```
```yaml
# context.toa.yaml
exposition:
/posts:
attachment:
read:
anonymous: true
post:
id: user-id
post:edit:
role: app:posts:editor
```
Policy values form a hierarchy the way [role and scope](#hierarchies) values do: an Attachment
applies to every Policy within it.
In the example above:
- an Attachment `read` attaches Directive `anonymous: true` to both `read:list` and `read:post`
Policies.
This means that a list of posts and each post can be accessed without authorization.
- an Attachment `post` attaches Directive `id: user-id` to both `post:submit` and `post:edit`
Policies.
This means that an Identity can submit and edit their own posts.
- an Attachment `post:edit` attaches Directive `role: app:posts:editor` to `post:edit` Policy.
This means that an identity with a role covering `app:posts:editor` can edit posts by any author,
in addition to the fact that the author themselves can do this thanks to the previous Attachment.
### Nesting
Policies are namespace-scoped, meaning they can be attached to any Route under the
corresponding `/{namespace}` prefix.
Attachment is applied to the node where it is declared, as well as its nested nodes.
Directives of the Attachment are applied to the node where the attached Policies are declared, as
well as their nested nodes.
Here's an example of how this works:
```yaml
# manifest.toa.yaml
name: posts
exposition:
/:user-id:
GET:
endpoint: observe
policy: read
/:user-id/:post-id:
GET:
endpoint: observe
policy: read
```
```yaml
# context.toa.yaml
exposition:
/posts:
/:user-id:
attachment:
read:
anonymous: true
/:user-id/:post-id:
attachment:
read:
role: reader
```
In the example above, the same Policy `read` is attached to two Routes with different Directives.
The following example demonstrates the attachment of the `read` Policy to both Routes with the same
Directive:
```yaml
# context.toa.yaml
exposition:
/posts:
attachment:
read:
anonymous: true
```
## Introspection
[`OPTIONS`](introspection.md) answers only the methods this identity may reach, and `403` where it
may reach none. So does [discovery](discovery.md), which omits a resource they may reach no method
of, and so does MCP's `tools/list`: all three read the same description.
What is decided from the identity decides here — `anonymous`, `anyone`, `role`, `delegate`. `id`
and `federation` decide from one too: _which_ identity needs the request, and a description has no
route variable to read, but a caller with no identity at all is refused whatever the value would
have been — and is not shown it. `assert` and `input` admit nobody: they require a credential and
constrain a body, and whoever is admitted is admitted by something else.
`anonymous` is the exception it is for a procedure: a description is not the reply a cache would
hold, so a route it admits is described whatever the request presented.
What guards a method is said in the description as well: `authenticated` where reaching it takes
being someone and no more — `anyone`, `delegate`, `claims` — `private` where `id` decides it,
`protected` where `role` does, and `system` besides where the scope `role` requires lies
within `system`.
`rule` says whatever each directive it composes says. A resource carries whichever of the four any
of its methods does.
`anonymous` says so too, which is what tells a client where not to present a credential: it is
the one that refuses a caller carrying one. A method that says nothing at all — `echo` is the
one — takes a credential where there is one and answers without it where there is not.