~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
512 lines · 33.7 KB Raw History
# OAuth: access tokens on every method, and what scopes allow

An OAuth access token (`OAUTH.md`) is taken by every method that takes a
token, with the `DPoP` scheme and a DPoP proof of the call, and allows what
its session's scope allows. This page says which methods take one, how it is
checked there, and what each method needs of the scope. How the token is
issued, its proof and nonces are in [`OAUTH.md`](OAUTH.md); password
sessions and the `Authorization` header's form are in
[`SESSIONS.md`](SESSIONS.md).

What is not served yet: OAuth tokens through the MCP tools and the CLI
(`COMMANDS.md`), which take only `Bearer` tokens. `importRepo` needs
`account:repo` and `deactivateAccount` `account:status`, each to manage
(the methods' table, below). Calls for other services
(`PROXY.md`) need `rpc:` permissions ("Proxied calls", below); the space
methods (`SPACES.md`) need `space:` permissions ("Spaces", below).

## Sources

*This section is informative.*

| File | Is | Licence |
| --- | --- | --- |
| `atproto/permission.md` | atproto's permissions and their scope strings | CC-BY, `atproto/LICENSE.txt` |
| `atproto/oauth.md` | atproto's OAuth profile | CC-BY, `atproto/LICENSE.txt` |
| `rfc/rfc9449.txt` | DPoP, §7: the `DPoP` scheme at a protected resource | IETF Trust (BCP 78) |
| `rfc/rfc6750.txt` | bearer token usage, §3: `WWW-Authenticate` and its errors | IETF Trust |

The rules below that those leave open are the reference's, probed: which
methods take a token and when they check it, what each needs, and the
words of a refusal.

## Which methods take a token

The `Authorization` header is read as `SESSIONS.md` says: two words, the
scheme matched without regard to case, `Bearer`, `Basic` or `DPoP`, else
`400 InvalidToken`, wherever a method reads it. The methods are of five
kinds:

| Kind | Methods | With `Authorization: DPoP …` |
| --- | --- | --- |
| **take no token** | `describeServer`, `createSession`, `resolveHandle`, `repo.getRecord`, `listRecords`, `describeRepo`, `listRepos`, `getRepoStatus`, `createInviteCode`, `createInviteCodes`, `requestPasswordReset`, `resetPassword`, `deleteAccount`, `updateSubjectStatus`, `getSubjectStatus` | the header is not read as a token: any `Authorization` and `DPoP` headers, of any form, are as none, and the answer carries no `DPoP-Nonce` (the invite methods' and the admin's methods' `Basic` header is the admin's, `LIMITS.md` and `LIFECYCLE.md`, which answer a `DPoP` one as they say) |
| **createAccount** | `createAccount` | `401 AuthenticationRequired`, whatever the token and proof, before the input is checked, with no `DPoP-Nonce`; but a header that is not two words, `DPoP` alone or `DPoP a b`, is `400 InvalidToken` first, as `SESSIONS.md` says of any header's form |
| **take a token optionally** | `sync.getRepo`, `getLatestCommit`, `sync.getRecord`, `getBlocks`, `listBlobs`, `getBlob` | after the parameters are checked, the token and proof are checked as below; a good one answers as its account's own token does (`LIFECYCLE.md`): of the repository's account, as if it were active, and of another account, as no token does |
| **need a token** | `getSession`, `getRecommendedDidCredentials`, `updateHandle`, `uploadBlob`, `createRecord`, `putRecord`, `deleteRecord`, `applyWrites`, `createAppPassword`, `listAppPasswords`, `revokeAppPassword`, `getAccountInviteCodes`, `requestEmailConfirmation`, `confirmEmail`, `requestEmailUpdate`, `updateEmail`, `checkAccountStatus`, `deactivateAccount`, `activateAccount`, `requestAccountDelete`; of `MIGRATION.md`, `importRepo`, `listMissingBlobs`, `submitPlcOperation`, `requestPlcOperationSignature`, `signPlcOperation`; and of `PROXY.md`, `getServiceAuth`, `getPreferences`, `putPreferences`, `getProfile`, `getProfiles`, `getTimeline`, `getAuthorFeed`, `getActorLikes`, `getPostThread`, `getFeed`, `registerPush`, `unregisterPush`, and every call the server forwards | the token and proof are checked as below, then what the method needs of the scope; a forwarded call's once what `PROXY.md` checks before its token passes (its HTTP method, a method never forwarded, and its target), so that one refused for those is answered as with no token, with no `DPoP-Nonce` |
| **take a refresh token** | `refreshSession`, `deleteSession` | `401 AuthMissing`, as `SESSIONS.md` says, with no `DPoP-Nonce` |

The space methods (`SPACES.md`) that take an account's access token take
an OAuth access token too, as methods that need a token do ("Spaces",
below, says which, and what each needs). Those that take service auth, a
delegation token or a space credential alone (`notifyRepoUpdate`,
`listWriters`, `authorizeSpaceSync`, `notifyCredentialsRevoked`) are none
of these kinds: a `DPoP` call's token is not checked as below, and the
method refuses it as a token the server did not give out.

With `Bearer`, the methods that take a token optionally take a session's
`accessJwt` as `BLOBS.md` says `getBlob` does: one the server gave out, of
any account, is as none, and one it did not `400 InvalidToken`, after the
parameters are checked. An OAuth access token sent as `Bearer` is not one
the server gave out as a bearer token: `400 InvalidToken`, on every method
that checks a `Bearer` token (RFC 9449 §7.2).

## The token and its proof

A method that takes a token checks an `Authorization: DPoP <access token>`
call as `OAUTH.md` says `getSession` does ("OAuth tokens on XRPC"), with
`htm` the call's HTTP method and `htu` the issuer's origin and the path
`/xrpc/<method>`, `<method>` as the request's path spells it: a method
named in another case (`/xrpc/COM.ATPROTO.SERVER.GETSESSION`) is proved with
that path, and a proof whose `htu` spells it as the lexicon does is `401
invalid_dpop_proof`. A query in the request or in `htu` is not compared. The
answers are those of `OAUTH.md`: `401 invalid_dpop_proof`, `401
use_dpop_nonce` or `401 invalid_token`, as XRPC errors with
`WWW-Authenticate: DPoP algs="…", error="<error>"`. A proof that passes
the checks of `OAUTH.md` "DPoP" is spent, whatever the call answers after.

**When.** A query's parameters are checked first: a call refused for them is
refused as without a token, and its answer carries no `DPoP-Nonce`. A
procedure's token and proof are checked before its input. So a
`createRecord` with no `collection` and a proof without a nonce is `401
use_dpop_nonce`, and a `sync.getRepo` with a `did` that is not a DID and
the same proof is `400 InvalidRequest`.

**Headers.** Every answer to a call with the `DPoP` scheme, to a method
that needs a token or takes one optionally, carries `DPoP-Nonce` (the
nonce of `OAUTH.md`), unless the call was refused before its token was
looked at: for the form of its `Authorization` header, for the HTTP method
(`PDS.md`), or for a query's parameters. Each answer that carries
`DPoP-Nonce` also carries `Access-Control-Expose-Headers` naming
`DPoP-Nonce`, and `WWW-Authenticate` too when the answer has that header,
so a browser's script may read them (RFC 9449 §7.1). Only the `401`
answers of the token and proof carry `WWW-Authenticate`: a `403` does not.

Once the token and proof pass, the call is made for the session's account,
as with that account's password session, but for what the scope allows.
With rate limits on, it counts as that session's call would, by the
account's DID, in the same buckets; a call refused for its token, its proof,
or a scope checked with the token costs nothing (`LIMITS.md`, "OAuth access
tokens").

## What a scope allows

A session's scope is its scope tokens as `OAUTH.md` keeps them (at PAR,
narrowed at consent), its `include:` scopes replaced by their sets'
permissions (`OAUTH-PERMISSION-SETS.md`, "The token's scope"):
`atproto`, the `transition:` scopes, and the permissions that parse. An
`include:` scope itself allows nothing. Every token has `atproto`, which
alone allows what
every method needs of a token and nothing more. Each permission allows:

- `repo` (`collection` set *C*, `action` set *A*, by default all three):
  a write of action *a* to collection *c* when *a* is in *A*, and *c* is in
  *C* or *C* has `*`. Collections are compared as written, case and all:
  `repo:com.example.note` allows nothing of `com.Example.note`.
- `blob` (`accept` set): a media type *m* when *m* is a type and subtype,
  that is: one `/`, something before it and after it, no space and no `*`;
  a space is U+0020 alone, so `image/p<tab>ng` is a type and subtype that
  `blob:image/*` allows, and `image/png<tab>` (`image/png<tab>; q=1`) too;
  and some accept value is `*/*`, or *m* itself, or `<type>/*` with *m*
  starting `<type>/`. Compared as written, case and all: `blob:image/*`
  allows `image/png` and not `IMAGE/PNG`, and no `blob` permission allows
  `*/*`, `image/*` or `a/b/c` as a media type.
- `identity` (`attr`): the handle, when `attr` is `handle` or `*`.
- `account` (`attr`, `action` set, by default `read`): reading or managing
  that attribute; `manage` allows reading too. `account:email` and
  `account?attr=email` allow reading the email.
- `rpc` (`lxm` set, one `aud`): a call of method *l* to service *s* when
  some `lxm` is *l* or `*`, and `aud` is *s* or `*`. Both are compared as
  written: `aud` is a DID and a `#` service id, percent-decoded as the
  scope's parameters are.

And the transitional scopes (`atproto/permission.md`):

- `transition:generic` allows every `repo` and `blob` check, whatever the
  collection or media type (`*/*` and `a/b/c` too), every `rpc` check of a
  method whose name does not start with `chat.bsky.` compared without
  regard to case, `*` among them, whatever the service, and nothing of
  `identity` or `account`. So it allows nothing of
  `Chat.Bsky.convo.sendMessage` or `CHAT.BSKY.convo.getLog`.

  *Note:* a service may read a method's name without regard to case, as
  the server itself does (`PROXY.md`), and the case a client chose must
  not change what its scope allows.
- `transition:chat.bsky` allows every `rpc` check of a method whose name
  starts with `chat.bsky.` in any case, whatever the service (the AppView's
  `#bsky_appview` as well as a chat service), with or without
  `transition:generic`; and nothing else, not `*`.
- `transition:email` allows reading the email.

## What each method needs

| Method | Needs | Checked |
| --- | --- | --- |
| `getSession` | nothing; but answers `email`, `emailConfirmed` and `emailAuthFactor` only if the scope allows reading the email (`OAUTH.md`) | |
| `getRecommendedDidCredentials`, and the methods that take a token optionally | nothing | |
| `updateHandle` | the handle (`identity:handle`) | after the token, before the input |
| `uploadBlob` | `blob` of the call's media type, as `BLOBS.md` reads it from `Content-Type` | after the `Content-Type` check, before the size |
| `createRecord` | `repo` `create` of the collection | after the input and `repo` checks (`PDS.md`), before anything of the record, its key, `validate` or the swaps: a record whose `$type` is not its collection, a key that exists, a `swapCommit` that is not the head |
| `putRecord` | `repo` `create`, then `update`, of the collection, whether or not the record exists | as `createRecord` |
| `deleteRecord` | `repo` `delete` of the collection, whether or not the record exists | as `createRecord` |
| `applyWrites` | for each collection its creates name, in the order first named, `repo` `create`; then for its updates' collections `update`; then for its deletes' `delete` | as `createRecord`, before anything of each write: an update of a record that does not exist is the scope's refusal, not `500` |
| `createAppPassword`, `listAppPasswords`, `revokeAppPassword`, `getAccountInviteCodes`, `requestAccountDelete` | never allowed to an OAuth token: `403 Forbidden`, `OAuth credentials are not supported for this endpoint` | after the token, before the input |
| `requestEmailConfirmation`, `confirmEmail` | managing the email (`account:email?action=manage`); `transition:email` only reads it | after the token, before the input |
| `requestEmailUpdate`, `updateEmail` | never allowed to an OAuth token, whatever its scope: `403 Forbidden`, `Use the account manager interface to update email address associated with an account` | after the token, before the input |
| `checkAccountStatus` | nothing | |
| `deactivateAccount` | managing the account's status (`account:status?action=manage`); it then ends every OAuth session of the account and deletes its app passwords (`LIFECYCLE.md`) | after the token, before the input |
| `activateAccount` | never allowed to an OAuth token: `403 Forbidden`, `Account reactivation is not available with OAuth credentials. Sign in to your account management page to reactivate.` | after the token, before the input |
| `importRepo` | managing the account's repository (`account:repo?action=manage`) | after the token, before the `Content-Type` |
| `listMissingBlobs` | nothing | |
| `submitPlcOperation`, `requestPlcOperationSignature`, `signPlcOperation` | the whole identity (`identity:*`) | after the token, before the input |
| a call forwarded (`PROXY.md`) | `rpc` of the method, as its path names it, to the service its `atproto-proxy` header names, or `<AppView DID>#bsky_appview` without one | after the token, before the body's size and the privileged methods' check |
| `getServiceAuth` | `rpc` of its `lxm`, or `*` without one, to its `aud`, as given | after the parameters and the token, before `aud`'s form, `exp` and the privileged and never-forwarded methods' checks |
| `getPreferences`, `putPreferences`, `getProfile`, `getProfiles`, `getTimeline`, `getAuthorFeed`, `getActorLikes`, `getPostThread` | `rpc` of the method to the service its `atproto-proxy` header names, as given, or `<AppView DID>#bsky_appview` | after the parameters and the token, before the input and the header's form |
| `getFeed` | as those, of `getFeed`, then of `getFeedSkeleton`, to the same service | as those |
| `registerPush`, `unregisterPush` | `rpc` of the method to `<serviceDid>#bsky_notif` | after the token and the input |

So with only `atproto`, a `createRecord` to another account's repository is
`401 AuthenticationRequired`, one to a repository no account has `403
AccountNotFound`, and one whose input is wrong `400 InvalidRequest`; one
whose record's `$type` is not its collection is the scope's refusal.
`uploadBlob` without a `Content-Type` is `400 InvalidRequest` whatever the
scope, and one of more than 5 MiB with a scope that allows it `413
PayloadTooLarge`.

**The refusal.** A call the scope does not allow is `403`:

```json
{"error": "ScopeMissingError", "message": "Missing required scope \"<scope>\""}
```

where `<scope>` is the first permission missing, in the order above, as a
scope string:

- `repo:<collection>?action=<create|update|delete>`;
- `identity:handle`;
- `account:<attr>?action=manage`, as `account:email?action=manage` and
  `account:status?action=manage`;
- `rpc:<lxm>?aud=<aud>`, *aud* percent-encoded as *m* below, but for a
  space, which is `+`: `aud=did:plc:…#a b` is `aud=did:plc:…%23a+b`;
- `blob:<m>`, where *m* is the media type in lower case, percent-encoded
  as JavaScript's `encodeURIComponent` encodes it, and then with `%3A`,
  `%2F`, `%2B`, `%2C`, `%40` and `%25` turned back to `:`, `/`, `+`, `,`,
  `@` and `%`. So `IMAGE/PNG` is `blob:image/png`, though `blob:image/png`
  would not allow it; `text/plain ;q=1`, whose media type is `text/plain `
  (`BLOBS.md`), is `blob:text/plain%20`; `im<tab>age/png` is
  `blob:im%09age/png`; `image/svg+xml` is `blob:image/svg+xml`; and `*/*`
  is `blob:*/*`. Each byte of the `Content-Type` header is the character
  of that value (Latin-1), not part of a UTF-8 sequence, and is lowered as
  Unicode lowers it, which in that range is `A`–`Z`, `À`–`Ö` and `Ø`–`Þ`
  each to the character 0x20 after it; the result is encoded as UTF-8.
  So a byte 0xC9 (`É`) is `%C3%A9`, and `É` sent as UTF-8, the bytes 0xC3
  0x89, is `%C3%A3%C2%89`.

## Proxied calls

A call for another service (`PROXY.md`) with an OAuth token is made as the
account's: the service token the server sends is the account's, whatever
the session. The scope is asked for `rpc` where the table above says, and
in place of the privileged methods' check: an OAuth session may call a
`chat.bsky.` method, forwarded or in `getServiceAuth`'s `lxm`, if its scope
allows it (`transition:chat.bsky`, alone or with `transition:generic`, to
any service, the AppView too; or an `rpc` permission naming it), and
`com.atproto.server.createAccount` likewise; `transition:generic` alone
allows no `chat.bsky.` method. `putPreferences` and
`getPreferences` take an OAuth session as one of another scope than
`com.atproto.access`: it neither reads nor writes `personalDetailsPref`.
Each answer to a call whose token and proof passed carries `DPoP-Nonce`, a
service's answer passed on too.

## Spaces

`space:` permissions are the spaces proposal's (`spaces/proposal.md`,
"OAuth scopes"), as the pinned spaces alpha of the reference
(`SPACES.md`) serves them.

**Which parse.** A `space` permission parses, and is kept at PAR as
`OAUTH.md` keeps every permission that parses, when its parameters are
these, each given in one place only, the positional one or as itself, and
each with a value allowed:

| Parameter | Many | Default | Values |
| --- | --- | --- | --- |
| `spaceType` (positional, required) | no | | an NSID, or `*` |
| `authority` | no | `self` | a DID, `self`, or `*` |
| `skey` | no | `*` | a record key (`atproto/record-key.md`), or `*` |
| `collection` | yes | none | an NSID, or `*` |
| `action` | yes | `read`, `create`, `update`, `delete` | `read_self`, `read`, `create`, `update`, `delete` |
| `manage` | yes | none | `create`, `update`, `delete` |

So `space:com.example.forum`, `space:*?authority=*&action=read`,
`space?spaceType=com.example.forum&manage=create` and
`space:com.example.forum?authority=did:plc:abc&skey=main&collection=com.example.post&action=create`
are kept, and `space`, `space:forum`, `space:com.example.forum?authority=me`,
`space:com.example.forum?action=write`, `space:com.example.forum?spaceType=com.example.forum`,
`space:com.example.forum?authority=*&authority=self` and
`space:com.example.forum?other=1` are dropped.

**Space type declarations.** A space type's NSID names a **declaration**:
a lexicon document whose `defs.main` has `type: "space"`, a `key` (a
string), and `collections`, an array of NSIDs, none `*`, which may be
empty (`spaces/proposal.md`, "Space type declarations"). It is resolved
as a permission set is (`OAUTH-PERMISSION-SETS.md`, "Resolving a set",
steps 1 to 4, and its caching, shared with the sets'), and is a
declaration when its document is as step 5 says but for `defs.main`:
`type` `space`, with `key` and `collections` as said. A document whose
`defs.main.type` is something else is not a declaration; one of type
`space` with no `key`, or whose `collections` is missing or not as said,
does not resolve. The types are those the scope's `space:` tokens name,
and those of the `space` permissions its sets grant
(`OAUTH-PERMISSION-SETS.md`, "What a set grants"), each distinct one
once, but `*`.

- At PAR, no declaration is resolved.
- The authorization page resolves each type's declaration, once the sets
  resolve (`OAUTH-PERMISSION-SETS.md`). One that does not resolve, or is
  not a declaration, ends the request, and the page sends the browser
  back to the client as it does a request that can no longer be used
  (`OAUTH.md`, "The authorization page"), with
  `error=invalid_scope` and `error_description` `Unable to retrieve space
  declarations`; the request's page loaded again is then the error page
  of a request that is not there.
- At the code grant each is resolved again, after the sets; one that does
  not resolve or is not a declaration is `400 invalid_request`.

**On the authorization page.** When the scope, with what its sets grant,
has a `space` permission, the page's `__authorizeData` (`OAUTH.md`, "The
authorization page") has, as the alpha's has:

- `spaces`: an object with a key for each type above, its value that
  declaration's `defs.main` as published, every field kept;
- `spaceHandles`: an object with a key for each DID that a `space:` token
  of the scope itself (not one a set grants, as the alpha) names as its
  `authority`, each once, whose handle verifies, its value that handle as
  the document writes it, its case kept. A handle verifies when the DID
  resolves (`IDENTITY.md`), its document's first `alsoKnownAs` entry that
  begins `at://` is a handle after it, and that handle resolves to the
  same DID as `IDENTITY.md` ("Resolving a handle") resolves an external
  one, by DNS and HTTPS, even the handle of an account here. A DID whose
  handle does not verify is left out, and does not end the request. With
  none, `{}`.

Without a `space` permission it has neither. The consent view shows the
scope's `space:` tokens, and the `space` permissions of each set, from
these (`OAUTH-PAGES.md`, "What it asks for").

**The token's scope.** At the code grant, once the sets are expanded
(`OAUTH-PERMISSION-SETS.md`, "The token's scope"), each `space:` token of
the expansion, in place, is rewritten:

1. one with no `collection`, of a `spaceType` that is not `*`, whose
   declaration's `collections` is not empty, gets those collections;
2. then one whose `authority` is `self`, given or by default, gets the
   account's DID.

A token neither step changes is kept as it was given. One either changes
is written again: `space:<spaceType>`, then, after `?` and joined by `&`,
`authority=<a>` unless it is `self`, `skey=<k>` unless it is `*`, each
collection as `collection=<c>` (several: each once, sorted, or `*` alone
if one is `*`), each action as `action=<x>` in the order of the table's
values unless they are the default four, and each `manage=<m>` in the
order `create`, `update`, `delete`. The type is percent-encoded as
JavaScript's `encodeURIComponent` encodes it, and the parameters as a URL's
form-encoded query (`application/x-www-form-urlencoded`: all but letters,
digits and `*-._` percent-encoded, a space `+`); in both, `%3A`, `%2F`,
`%2B`, `%2C`, `%40` and `%25` are then turned back to `:`, `/`, `+`, `,`,
`@` and `%`. So `space:com.example.forum`, with a declaration of
`[com.example.reply, com.example.thread]`, is
`space:com.example.forum?authority=did:plc:…&collection=com.example.reply&collection=com.example.thread`.
That is the token answer's `scope`, and what the session's tokens allow.
At each refresh the scope is expanded and rewritten again, the
declarations resolved again; one that does not resolve leaves the
session's last expansion as it was, as a set does.

**What it allows.** A space operation is a space, its authority, type and
key, and one of: reading the whole space (`read`), reading the caller's
own repo in it (`read_self`), a write of a collection (`create`, `update`
or `delete`), or managing the space (`manage` `create`, `update` or
`delete`). A `space` permission allows it when its `spaceType`,
`authority` and `skey` are each the operation's or `*` (an `authority` of
`self` matches nothing), and:

- `read` when its actions have `read`; `read_self` when they have `read`
  or `read_self`; whatever its collections;
- a write when its actions have the write's action and its collections
  have the collection or `*`: one with no collections allows no write;
- managing when its `manage` has the operation; its actions and
  collections are not asked.

`transition:generic` and every other permission allow nothing of a space.
A password session, and an app password's, is allowed every operation.

**What each method needs.** With an OAuth token, each method asks the
scope for the space its input names, and for these, at these points:

| Method | Needs | Checked |
| --- | --- | --- |
| `createSpace` | `manage` `create`, of the space under the caller's DID with the input's `spaceType`, and `skey` (or the one made for it) | after the input, before the policy |
| `updateSpace`, `removeWriter`, `putMembers`, `removeMembers` | `manage` `update` | after the input, before the caller is checked as the space's authority |
| `deleteSpace` | `manage` `delete` | as `updateSpace` |
| `importSpaceMeta` | `manage` `create`, then `manage` `update`, of the archive's space | once the archive is read, before the caller is checked as its authority |
| `getSpace`, `exportSpaceMeta`, `listMembers` | `read_self` | after the parameters, before the caller is checked as the space's authority |
| `getDelegationToken` | `read` | after the parameters |
| `createRecord`, `putRecord`, `deleteRecord` | `create` or `delete` of the collection; for `putRecord`, `update` if the record is there, else `create` | after the input's fields, `repo` and the collection's check (`SPACES-REPO.md`), before the record is checked and the space looked for |
| `applyWrites` | for each write, in order, its action of its collection (`update` for an update) | once the input is checked, each write's record too, before the space is looked for |
| `clearRecords` | `delete` of each collection the caller's repo in the space holds a record of, but those kept (`SPACES-REPO.md`), in the order of their names | as `applyWrites` |
| `importRepo` | `create` of each collection the archive's records name, in the order first named | once the archive is read and found the caller's repo's, before the space is looked for |
| `getRecord`, `listRecords`, `getLatestCommit`, `listRepoOps`, `getRepo`, `getBlob`, `listBlobs` | `read_self` | after the caller's `repo` is checked as its own (`RepoNotFound` first) |
| `listSpaces` | `read_self` of the space whose type is the `spaceType` given, else `*`, whose authority is the `spaceDid` given, else `*`, and whose key is `*` | after the parameters |

So an unfiltered `listSpaces` needs a permission of `spaceType` `*` and
`authority` `*`. Every other check a method makes of its caller is as with
a password session: an OAuth session is its account's, and is neither an
app password's nor a taken-down account's.

**The refusal** is `403 ScopeMissingError`, as above, naming the first
operation not allowed as a `space:` scope, written as the token's scope is:
the space's type, `authority` its DID (or `*`), `skey` its key unless it
is `*`; for managing, `action=read_self` and `manage=<m>`; for a read,
`action=<read or read_self>`; for a write, `collection=<c>` and
`action=<a>`. So `putRecord` of `com.example.tile` in
`at://did:plc:abc/space/com.example.workspace/main` with no permission for
it is `Missing required scope
"space:com.example.workspace?authority=did:plc:abc&skey=main&collection=com.example.tile&action=create"`.

### For other slices

*This section is informative.*

What a handler is given, once a call's token has passed: the account's DID,
and how the call was made: a password session, an app password's session
(`SESSIONS.md`), or an OAuth session with its scope. A method that needs
some permission asks the scope, as "What a scope allows" and "Spaces" say;
an OAuth scope that does not allow it is the `403 ScopeMissingError` above,
naming the scope that would. Password sessions are allowed every
permission; whether an app password's session is allowed a method is the
method's page's to say.

## How it is checked

`harness/checks/oauth-scopes.mjs` signs accounts in through `OAUTH.md`'s
flow with loopback clients whose scopes mix `atproto`, the transitional
scopes and `repo`, `blob`, `identity` and `account` permissions, each
request a random part of its client's, and adds those sessions to the
account's. Then its steps, among the others of `compare.mjs`:

- **a call with a session's token:** a random method of each kind above,
  with the session's access token and a good proof: writes of the
  harness's collections (`createRecord`, `putRecord`, `deleteRecord`,
  `applyWrites`, with creates, updates and deletes of several collections),
  `uploadBlob` with media types drawn from a list (`image/png`,
  `TEXT/Plain`, `*/*`, `a/b/c`, none, ...), `updateHandle` with a handle
  that does not parse (so that nothing changes), `getSession`,
  `getRecommendedDidCredentials`, the app password methods and
  `getAccountInviteCodes`, the email and lifecycle methods in forms that
  leave the account's state as it is (a `deactivateAccount` whose
  `deleteAfter` is not a datetime, `checkAccountStatus` by status alone),
  the methods that take a token optionally, the methods that take none, and
  `createAccount`.
- **proxied calls:** `harness/checks/proxy.mjs`'s steps (`PROXY.md`) draw
  OAuth sessions too, signed in with loopback clients whose scopes mix
  `transition:generic`, `transition:chat.bsky` and `rpc` permissions of
  some of the methods and services the steps call, or none; each call is
  then made with `DPoP` and a proof, and compared as `PROXY.md` says, with
  the nonce, `WWW-Authenticate`'s `error` and a `ScopeMissingError`'s
  `message` too. But for a `chat.bsky.` method named in another case
  (`Chat.Bsky.convo.getLog`, `CHAT.BSKY.x.y`) with a scope of
  `transition:generic` and not `transition:chat.bsky`: the reference's
  check of the prefix minds case, and allows it (probed: its
  `getServiceAuth` of `Chat.Bsky.convo.getLog` and of
  `CHAT.BSKY.convo.getLog` answers a token, of `chat.bsky.convo.getLog`
  `403 ScopeMissingError`). Such a call, `getServiceAuth` of it and the
  method forwarded to the chat service, is sent to the server alone, which
  must answer `403 ScopeMissingError` and send nothing on; a step of its
  own signs a session of `transition:generic` alone in for it.
- **a call refused:** one of those methods with one thing wrong in the
  proof (`OAUTH.md`'s list, and a replay), or in the token (none, a
  replaced, revoked or garbage one, the refresh token, a password
  session's, a proof by another key), or in the scheme (`Bearer` with the
  OAuth token, a header of one or three words, `dpop` in lower case); and
  a query whose parameters are wrong, or a procedure whose input is wrong,
  with a proof without a nonce.
- **the edges, each every time:** `uploadBlob` with a session of `atproto
  blob:image/*` alone and media types with a tab (`image/png<tab>; q=1`,
  `image/p<tab>ng`, `im<tab>age/png`) and with a byte outside ASCII (0xC9,
  and 0xC3 0x89), the refusals' `message` compared; `createAccount` with
  `Authorization` `DPoP` alone, `DPoP a b`, `DPoP abc` and `dpop abc`, no
  proof, and a handle that does not parse; and `getSession` and
  `getLatestCommit` named in another case, with a proof whose `htu` is
  the path as sent and one whose `htu` is as the lexicon spells it.

Compared, on both sides: the status; the `error`; the `message` of a
`ScopeMissingError`; whether the answer carries `DPoP-Nonce`; the `error`
of `WWW-Authenticate`; which of `DPoP-Nonce` and `WWW-Authenticate`
`Access-Control-Expose-Headers` names; and a success's body as `PDS.md`
compares it (the `getRepo` and `sync.getRecord` CAR files by status only).
A write that lands is noted as the other write steps note theirs, so the
repository checks see it.

Not compared:

- Other `message`s, and `WWW-Authenticate`'s `algs` and
  `error_description`, as `OAUTH.md` says.
- `Cache-Control` and `Vary` of answers to calls with a token (the
  reference sends `private` and `Authorization`), for any scheme.
- A `Basic` header on the methods that take a token optionally (the
  reference answers `401 AuthenticationRequired`, `SESSIONS.md` the rest
  `400 InvalidToken`).
- A media type with no `/`: the reference looks it up as a file extension
  (`BLOBS.md`), and its scope check takes `png` as `image/png`.

The `space:` permissions are checked in the spaces run (`SPACES.md`),
against the pinned alpha, by `harness/spaces/scopes.mjs`. The run's
lexicon authority, a PDS of its own on the alpha's directory, publishes
declarations (one with two collections and a title, one with none, one
of another `key`), a declaration with no `key`, one with no
`collections`, and a document of another type; the server is started
with `PDS_LEXICON_AUTHORITY_DID` its DID, and the alpha given the same
(`OAUTH-PERMISSION-SETS.md`, "How it is checked"). Loopback clients
asking for `atproto` and `space:` scopes from a list (bare; `manage`;
`authority` `*`, another account's DID and `self` given; the positional
as a parameter; `read` and `read_self` alone and with `manage`;
collections outside the declaration, repeated, and `*`; `skey`; type
`*`; two types; `transition:generic` alone; tokens that do not parse; and
each type that is no declaration, and one with no document) sign in on
both sides through the page's API; what each step answers is compared:
PAR's status, the page's status and where it sends the browser with its
`error`, the page's `spaces` and `spaceHandles` (each `{}` when not
there), the page loaded again, and the token answer's `scope`, each
account's DID named alike. With each session's token, a good proof and a
nonce, the space methods are called on the account's own spaces (a
`personalspace` holding a record and a `memberspace`, made with its
password session), another account's, and spaces never made: each
`createSpace` case, `getSpace`, `listSpaces` with and without each
filter, `putRecord` of a record there and of one not, `createRecord` of
a declared collection, another, a published one, a record of another
type, and in another's repo and space, `deleteRecord` of a record there
and not, `applyWrites` with each action, with a bad write and with none,
the reads of its own repo and another's, `getDelegationToken` of its own
space and another's, `putMembers`, `listMembers`, `removeMembers`,
`removeWriter`, `updateSpace`, `exportSpaceMeta`, `importSpaceMeta` of
the archive its password session exported, `clearRecords` and
`deleteSpace`. Each answer is compared: the status, the `error`, and a
`ScopeMissingError`'s `message`, each account's DID named alike and a key
the server made as `<tid>`. The main run, which the latest reference
answers and which keeps no `space:` permission, asks for none.

Not compared: a success's body (the spaces run's other checks hold
them); `importRepo` with an OAuth token; a declaration changed
during a session; the code grant's refusal of a declaration that no
longer resolves, as no declaration changes during a run; and a
`spaceHandles` entry, as no handle of the run's network resolves by DNS
or HTTPS on either side, so both give `{}`.