Restrict Who Can Call a Route
A customer sees their own orders and nobody else’s. The catalog is open to everyone. Only the
customer may change the items of an order, and only staff its status. Each of these is one line on
the route, a directive of the auth family; an operation never checks who calls it.
§A route admits nobody until it says who
The orders are those of store.orders, the component Create a Component writes. Expose It over HTTP opens its routes to everyone with anonymous: true. Replace that exposition with routes that have the customer in the path:
# components/store.orders/manifest.toa.yaml
exposition:
/:customer:
io:output: [id, items, total, status]
GET: enumerate
POST: create $ npm run restart GET lists the orders whose customer is the one in the path, and POST passes it to create as the customer of the new order; Map Methods to Operations has the rule.
GET /store/orders/2428c31ecb6e4a51a24ef52f0c4181b9/ HTTP/1.1
host: api.store.example 401 Unauthorized §A customer reaches only their own orders
Put the owner into the path and name that route variable in auth:id:
# components/store.orders/manifest.toa.yaml
exposition:
/:customer:
auth:id: customer
io:output: [id, items, total, status]
GET: enumerate
POST: create
/:id:
GET: observe auth:id admits the identity whose id equals the variable. It works because a customer’s account
and identity have the same id, and orders keep that id in customer.
The directive is inherited by everything nested under the node, so /store/orders/:customer/:id/ is the customer’s too.
Bob lists his orders:
GET /store/orders/2428c31ecb6e4a51a24ef52f0c4181b9/ HTTP/1.1
host: api.store.example
authorization: Token eyJhbGciOiJkaXIi...
accept: application/yaml 200 OK
- id: 7d1a0c1e5b6f4f0f9a3b2c4d5e6f7a8b
items:
- product: p1
quantity: 2
total: 1800
status: pending Alice asks for Bob’s orders with her own token:
403 Forbidden and a request without credentials, or with credentials that were rejected, gets 401 Unauthorized.
§Test it
The steps are the ones toa create wrote, which Write a Test explains.
The scenarios that article puts into features/orders.feature call the routes replaced above;
this file takes their place.
# features/orders.feature
Feature: Orders
Background:
When the request is sent:
"""
POST /accounts/ HTTP/1.1
content-type: application/yaml
accept: application/yaml
name: Bob
"""
Then the response is received:
"""
201 Created
authorization: Token ${{ bob.token }}
id: ${{ bob.id }}
"""
When the request is sent:
"""
POST /accounts/ HTTP/1.1
content-type: application/yaml
accept: application/yaml
name: Alice
"""
Then the response is received:
"""
201 Created
authorization: Token ${{ alice.token }}
id: ${{ alice.id }}
"""
Scenario: A customer sees only their own orders
When the request is sent:
"""
POST /store/orders/${{ bob.id }}/ HTTP/1.1
authorization: Token ${{ bob.token }}
content-type: application/yaml
accept: application/yaml
items:
- product: p1
quantity: 2
total: 1800
"""
Then the response is received:
"""
201 Created
id: ${{ order.id }}
"""
When the request is sent:
"""
GET /store/orders/${{ bob.id }}/ HTTP/1.1
authorization: Token ${{ bob.token }}
accept: application/yaml
"""
Then the response is received:
"""
200 OK
- id: ${{ order.id }}
"""
When the request is sent:
"""
GET /store/orders/${{ bob.id }}/ HTTP/1.1
authorization: Token ${{ alice.token }}
"""
Then the response is received:
"""
403 Forbidden
"""
When the request is sent:
"""
GET /store/orders/${{ bob.id }}/ HTTP/1.1
"""
Then the response is received:
"""
401 Unauthorized
""" $ npm run restart
$ npm run features The background signs two customers up at the sign-up route as guests, which takes one request each.
§Several ways in
Every auth directive that applies to a method is an alternative: the request is admitted as soon
as one of them admits it. Directives on a node apply to its methods and to everything nested
under it, so alternatives add up down the tree:
# components/store.orders/manifest.toa.yaml
exposition:
/:customer:
auth:id: customer # the customer…
auth:role: store:orders # …or staff
GET: enumerate
POST:
auth:role: store:partner # …or, for POST only, a partner
endpoint: create When nothing admits the request:
| The request | Reply |
|---|---|
| Has no credentials, or credentials that were rejected | 401 Unauthorized |
| Has an identity that no directive admits | 403 Forbidden |
A nested directive cannot narrow. It adds an alternative to those above it and does not replace
them. To require several conditions at once, use auth:rule.
To start afresh on a part of the tree, declare it as a sibling route or mark the node isolated: true; The Resource Tree shows both.
A directive may be written without the family prefix: anonymous: true is auth:anonymous: true.
§Open a route to everyone
auth:anonymous: true admits a request that carries no credentials. The catalog is a
component of its own, store.catalog:
# components/store.catalog/manifest.toa.yaml
exposition:
/:
auth:anonymous: true
GET: enumerate A request that does carry valid credentials is refused with 403: a public resource is meant to
be served from caches, and a response to a request with credentials cannot be. So a client does
not send its token to anonymous routes. A resource open to visitors and to signed-in users alike
declares both directives:
/:
auth:anonymous: true
auth:anyone: true
GET: enumerate Called through /.rpc or /.mcp, an anonymous
route is reachable whatever the request presents.
§Admit any signed-in user
auth:anyone: true admits every request with valid credentials, whoever it is. Product pictures
are uploaded to pictures:
# components/pictures/manifest.toa.yaml
exposition:
/:
auth:anyone: true
POST: upload A guest has valid credentials too.
§Admit staff
auth:role admits an identity that holds a role:
# components/store.orders/manifest.toa.yaml
exposition:
/:
auth:role: store:orders
GET: enumerate A list admits a holder of any of them:
auth:role: [store:orders, store:support] A role may be built from route variables, written in braces:
# components/store.catalog/manifest.toa.yaml
exposition:
/:vendor:
auth:role: store:vendors:{vendor}:manager
PUT: transit PUT /store/catalog/acme/ requires store:vendors:acme:manager. A variable the route does not
have is a mistake in the declaration and answers 500.
How roles are named, granted and tested is in Give Staff Wider Access.
§Require several conditions at once
auth:rule admits when all the directives inside it do:
# components/store.orders/manifest.toa.yaml
exposition:
/refunds/:customer/:id:
POST:
auth:rule:
id: customer
role: store:verified
endpoint: refund Only the customer, and only with the role store:verified, may ask for a refund; refund stands
for an operation of your own. Inside a rule the directives are written without the prefix.
A rule is one alternative among the others on the route. Here the route is declared beside /:customer, not under it: nested there, it would inherit auth:id: customer, which admits the
customer on its own, and the rule would narrow nothing.
§Limit who may send a property
A customer changes the items of their order; only staff change its status. auth:input names
properties of the request body and who may send them. The inherited assign sets properties once the manifest says which:
# components/store.orders/manifest.toa.yaml
operations:
assign:
input:
type: object
properties:
items: { type: array }
total: { type: integer, minimum: 0 }
status: { type: string }
additionalProperties: false
exposition:
/:customer:
/:id:
PATCH:
auth:id: customer
auth:role: store:orders
auth:input:
- prop: status
role: store:orders
- prop: [items, total]
id: customer
endpoint: assign Each entry names a property, or a list of them, and the directives required to send it.
PATCH /store/orders/2428c31ecb6e4a51a24ef52f0c4181b9/7d1a0c1e5b6f4f0f9a3b2c4d5e6f7a8b/ HTTP/1.1
host: api.store.example
authorization: Token <Bob's token>
content-type: application/yaml
status: approved 403 Forbidden
Input property is not authorized auth:input admits nobody itself; it is checked in addition to what admits the request. A body
without the property is unaffected, and a body that is not an object is not checked.
§Ask for the password again
auth:scheme refuses every way of authenticating but the named one. Use it where an action must
be confirmed with the password itself and not with a token that has been on a device for weeks:
# components/accounts/manifest.toa.yaml
exposition:
/:id:
/close:
POST:
auth:scheme: basic
auth:id: id
endpoint: close A request authenticated any other way is answered:
403 Forbidden
Basic authentication scheme is required to access this resource close stands for an operation of your own that closes the account. auth:scheme admits nobody itself; pair it with a directive
that does. The gateway’s own password change is declared this way.
§Record who called
The order must remember who approved it. auth:delegate puts the caller’s identity into the
request body under the named property. The order gets an approver, and approve an input:
# components/store.orders/manifest.toa.yaml
entity:
properties:
approver: { type: string }
operations:
approve:
type: transition
concurrency: retry
query: true
errors: [NOT_PENDING]
input:
type: object
properties:
approver:
type: object
properties:
id: { type: string }
roles: { type: array, items: { type: string } }
required: [id]
exposition:
/approvals/:id:
PUT:
auth:rule:
role: store:orders
delegate: approver
endpoint: approve // components/store.orders/operations/approve.ts
import type { ApproveInput, State } from '../types/index.d.ts'
export async function transition (input: ApproveInput, entry: State) {
if (entry.status !== 'pending')
return new Error('NOT_PENDING')
entry.status = 'approved'
entry.approver = input.approver.id
} The value is an object with the identity’s id and roles.
auth:delegatealone admits every signed-in caller. To record the caller and require a role, put both into a rule, as above. Side by side they would be alternatives, and the role would not be required.- The value comes from the gateway. Whatever the client sent under that property is overwritten,
and the property is not shown as input in resource descriptions. It does not have to be listed
in
io:input. - A request without a body gets one that holds the property. A body that is not an object is
answered
400.
Use it to record who did something (an approver, an author) and to answer a caller with their own account. Do not decide about access inside an operation with it. That belongs on the route.
§Admit by a provider’s token
A CI job imports the catalog, and proves itself with a token of its CI system instead of an
account in the shop. auth:claims admits a request authenticated with a Bearer token of a trusted provider whose claims have the given values; import stands for an operation of your own:
# components/store.catalog/manifest.toa.yaml
exposition:
/imports:
POST:
auth:claims:
iss: https://token.actions.githubusercontent.com
aud: https://github.com/acme
endpoint: import iss, sub and aud can be matched; at least one is required. A value is compared exactly;
where the token’s aud is a list, one matching entry is enough. Three forms of value are taken
from the request instead:
| Value | Matches |
|---|---|
/:name | The value of the route variable name. |
:authority | The authority of the request. |
:domain | For iss only: an issuer on the same parent domain as the request’s host. The issuer https://accounts.example.com matches requests to shop.example.com and a.shop.example.com, not to shop.other.com. |
/vendors/:vendor:
auth:claims:
iss: https://id.example.com
sub: /:vendor The directive applies only to the request that presents the provider’s token. A request made with
the gateway’s Token carries no claims and is not admitted by it.
§Admit a caller that signs as itself
A payment provider posts its events to the shop, signed with a token of its own that is no
account of the shop and no provider the shop trusts. auth:transfer gives the request’s authorization header to the operation as the named property of its input, and admits the
request; the operation verifies the token. receive stands for an operation of your own:
# components/store.payments/manifest.toa.yaml
exposition:
/events:
POST:
auth:transfer: authorization
endpoint: receive // components/store.payments/operations/receive.ts
import type { ReceiveInput, Context } from '../types/index.d.ts'
export async function effect (input: ReceiveInput, context: Context) {
// 'Bearer eyJ…' as the provider sent it, or undefined
const token = input.authorization
} - The gateway does not read the header: the call has no identity, a token of any scheme reaches the operation as it was sent, and one the gateway would refuse is not counted as a failed sign-in of the address.
- A request without the header is admitted too, without the property. Make it required in the operation’s input to refuse one.
- Every caller is admitted: an operation that does not verify the token serves anyone.
- No other
auth:directive of the method is asked, whether it declares one or inherits it. - The reply is never cached:
cache-control: no-store. - Called through
/.rpcor/.mcp, the method is not reached: the call is answered401.
§Declare access for the whole application
A component declares access to its own resources in its manifest. The Context can declare
directives on the tree as well, under exposition: /:, and every component’s routes inherit them:
# context.toa.yaml
exposition:
/:
auth:role: store:admin An administrator now reaches every resource of the shop, whatever the components declare for everyone else.
The gateway’s own resources under /identity/ inherit nothing: auth:anonymous: true at the root
does not open role management.
§Limits that come with the credential
After a directive admits a request, the credential itself may still refuse it:
- A custom token with
permissionsis refused with403outside the paths and methods it lists. - A custom token with
scopesholds those roles in place of its owner’s. - A token of another application bound to a resource is
refused with
401anywhere else.
§What a caller is shown
Resource descriptions follow access. OPTIONS on a resource, the discovery document and the tool list of MCP include only the methods the asking identity may call, and OPTIONS answers 403 where it may
call none. A description also says what guards a method:
| Mark | Meaning | Set by |
|---|---|---|
anonymous | Send no credentials here. | anonymous |
authenticated | Any identity. | anyone, delegate, claims |
private | The identity the route names. | id |
protected | A role. | role |
system | A role within the system scope. | role |
Whether a directive with a route variable (id, a role with braces, claims) admits depends
on the request, so such methods are shown to every caller with an identity and hidden from
callers without one.
§Reference
| Directive | Value | Admits when |
|---|---|---|
auth:anonymous | true | The request has no credentials. |
auth:anyone | true | The request has an identity. |
auth:id | route variable name | The identity equals the variable. |
auth:role | role or list of roles | A role of the identity covers one of them. |
auth:rule | map of directives | Every directive in it admits. |
auth:claims | iss, sub, aud | The provider’s token carries those claims. |
auth:delegate | input property name | The request has an identity; puts it into the input. |
auth:transfer | input property name | Always, but through /.rpc and /.mcp; puts the authorization header into the input, unread. |
auth:incept | output property name or null | The request has no identity yet; creates one. |
auth:echo | none | Always; answers with the caller’s identity. GET /identity/ is made of it, and an application has no reason to declare it again. |
auth:scheme | scheme name | Never; refuses other schemes with 403. |
auth:input | list of prop with directives | Never; refuses unauthorized properties with 403. |
auth:assert | true | Never; creates the identity for new credentials. |
Why access is declared on routes and never checked in operations is in the model.