Skip to content
Merged
Changes from 10 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a2e9656
API Scopes MSC
sandhose Jan 14, 2021
544d75b
Proposed insufficient privilege response format
hughns May 16, 2022
d49f2d8
Remove realm as not required
hughns May 18, 2022
23c7638
Clarifications + update on latest device management proposal
hughns Jul 31, 2022
b609c2b
Revised namespace structure + unstable prefixes
hughns Aug 3, 2022
f07b466
Revise prefix for device ID
hughns Aug 3, 2022
1ce049e
Reference to MSC3861 + cleanup
hughns Aug 5, 2022
5c57507
Add scope for guest access and tidy up
hughns Nov 17, 2022
5afb697
No need for UIA scope
hughns Feb 16, 2023
8ec2d7c
Update proposals/2967-api-scopes.md
hughns Jul 28, 2023
8539ab2
Clarification about encoding of device ID within URN scope
hughns May 10, 2024
0666b24
Merge branch 'matrix-org:main' into msc/sandhose/api-scopes
sandhose Sep 3, 2024
f65aef3
Rework MSC
sandhose Sep 4, 2024
660946a
Update proposals/2967-api-scopes.md
sandhose Mar 5, 2025
4dd433f
Reword as dbkr suggested
sandhose Mar 13, 2025
79845f5
Reword how unstable subdivisions are used
sandhose Mar 13, 2025
49550fe
Remove confusing sentence
sandhose Mar 13, 2025
14b962c
Gather all the links at the bottom of the document
sandhose Mar 13, 2025
58f2398
Tyding up, define exactly how device IDs are handled
sandhose Mar 14, 2025
e7531fa
Don't use a table for a single row
sandhose Mar 14, 2025
acc8e91
Typo
sandhose Mar 14, 2025
1304203
Fix math rendering
sandhose Mar 14, 2025
6623b96
Fix the math
sandhose Mar 18, 2025
1ddd733
Minor rewording on device uniqueness
sandhose Mar 18, 2025
a33d1e3
Simplify wording around the ASCII range
sandhose Mar 18, 2025
0800ea6
Typo
sandhose Mar 18, 2025
a7bb99c
Scope vs scope token is confusing
sandhose Mar 18, 2025
abbae1e
Reword how the device ID is requested
sandhose Mar 18, 2025
082625d
Explain why we keep the device ID generation on the client
sandhose Mar 25, 2025
089a789
MSCXXXX is a better placeholder
sandhose Mar 26, 2025
858b7be
The scope MUST have a device ID
sandhose Mar 26, 2025
c9f8690
Clarify that device IDs are still unique per user
sandhose Mar 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions proposals/2967-api-scopes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# MSC2967: API scopes

This proposal is part of the broader [MSC3861: Matrix architecture change to delegate authentication via OIDC](https://github.com/matrix-org/matrix-spec-proposals/pull/2967).

When a user signs in with a Matrix client, it currently gives the client full access to their Matrix account.

This proposal introduces access scopes to allow restricting client access to only part(s) of the Matrix client API.

## Proposal

[MSC2964](https://github.com/matrix-org/matrix-doc/pull/2964) introduces the usage of OAuth 2.0 for a client to authenticate against a Matrix homeserver.

OAuth 2.0 grants have scopes associated to them and provides a framework for obtaining user consent.

The framework encourages the practise of obtaining additional use consent when a client asks for a new scope that was not granted previously.

This MSC does not attempt to define all the scopes necessary to cover all Matrix APIs and use cases, but proposes the structure of a namespace and some specific scopes to cover existing use cases.

Additionally it is proposed that a standard approach to error response representation is adopted across the API. This could replace the UIA based responses that exist on some API endpoints today.

### Scope format

All scopes related to Matrix should start with `urn:matrix` and use the `:` delimiter for further sub-division.

| Prefix | Purpose |
| - | - |
| `urn:matrix:client` | For mapping of Client Server API access levels |

For future MSCs that build on this namespace unstable sub divisions should be used whilst in development:

e.g. `urn:matrix:com.example.mscXXXX.foo:something` or `urn:matrix:client:com.example.mscXXXX.something`

### Allocated scopes

#### Legacy use cases

To support existing, pre-MSC2964 use cases the following scopes are assigned:

| Scope | Purpose | Implementation notes |
| - | - | - |
| `urn:matrix:client:api:guest` | Grants access as a guest to endpoints in the Client-Server API | The OP can issue a refresh token for grants with this scope. |
| `urn:matrix:client:api:*` | Grants full access to the Client-Server API | The OP can issue a refresh token for grants with this scope. |

These are referred to as "legacy" because it is envisioned that a client would request more specific actions in future when required. e.g. something like `urn:matrix:client:api:read:*`

#### Device ID handling
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I'm surprised that the specifics of the Device ID handling scope is included in this MSC, which otherwise is the framework for defining scopes in general. Is this because this scope is compulsory for OIDC to work? If so, we should say so. If not, at the least, we should say that the scope is defined here as a concrete example of how scopes would work... or failing that, split it into a separate MSC.

If it /is/ primarily intended example of the framework, it just feels a bit weird to also be speccing specific behaviour at the same time - wouldn't it be better to give a fake example, and then clearly separate the Device ID management proposal somehow?

I have a feeling i've missed something here that explains why this isn't completely out of place :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a high level, this MSC defines two things:

  • a general shape for scopes (including future ones with unstable prefixes)
  • an initial set of scopes: one for wide access to the C-S API, another to specify the device

I have to include somewhere that it is now always the responsibility of the client to allocate a device, but I'm unsure where the right place for this is?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine here imho


Presently a device ID is typically generated by the homeserver and is associated with a specific access token. In OAuth 2.0 there is no such thing as a session and so a mapping cannot be handled using the same mechanism.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still true after refresh tokens (MSC2918)? I thought we did a bunch of work in Synapse related to this recently, but maybe I'm confusing different types of tokens.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically yes, in practice no as MSC2918-style refresh tokens are almost not adopted by anyone. Is it worth clarifying here?


MSC2964 proposes that the Matrix client is responsible for generating/allocating a device ID. A client can adopt and rehydrate an existing client by asking for its scope on login.

This also has a nice side-effect: if the device asked was never used by the client making the request, the authorization server will ask for explicit consent from the user.

The client can then bind the device ID to the grant by requesting a scope with the format:

| Scope | Purpose | Implementation notes |
| - | - | - |
| `urn:matrix:client:device:<device ID>` | bind the given device ID to the grant/access token | The OIDC Provider must only grant exactly one device scope for a token. |

For the purpose of this MSC we are assuming that device IDs are as per [MSC1597](https://github.com/matrix-org/matrix-spec-proposals/pull/1597) and, as such, are already URL safe and so can be represented as a scope without modification.

### Future scopes

Exact scopes for the whole API are intentionally not specified in this MSC.

It is envisioned that the namespace could be further partitioned to support use cases such as read only, write only, limited to one or more rooms etc.

Some thoughts/ideas for possible scopes are:

- `urn:matrix:client:api:<permission>` or `urn:matrix:client:api:<permission>:*` - grant limited access to the client API in all rooms. Permissions could be read, write, delete, append.
- `urn:matrix:client:api:read:<resource>` - read-only access to the client API for just the named resource. e.g. `urn:matrix:client:api:read:#matrix-auth`

New MSCs should be created for proposing and discussing such new scopes.

### Insufficient privilege response

It is proposed that a [RFC6750](https://datatracker.ietf.org/doc/html/rfc6750) formatted `WWW-Authenticate` response header is used to provide feedback to the client with `error="insufficent_scope"`.

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="insufficient_scope", scope="urn:matrix:api:something"
```

On receipt of such a response the client may then request a new authorization from the issuer as per [MSC2964](https://github.com/matrix-org/matrix-doc/pull/2964) requesting the additional scope be granted.

The rest of the response would be as per the [standard error response](https://spec.matrix.org/v1.2/client-server-api/#standard-error-response) spec.

For example:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="insufficient_scope", scope="urn:matrix:api:something"
Content-Type: application/json

{
"errcode": "M_FORBIDDEN",
"error": "Insufficient scope"
}
```

## Potential issues

The Device ID handling involves a change in where device IDs are generated. This is discussed in [MSC2964](https://github.com/matrix-org/matrix-doc/pull/2964). On the OIDC Provider side the device ID proposal requires the use of dynamic scopes. That is, the specific scope is a templated form rather than being static. This is not currently supported by some OpenID Providers (e.g. Okta and Auth0).

The addition of the `WWW-Authenticate` header could cause issue with some clients.

## Alternatives

### Scopes

Scope format could also have an URL format, e.g. `https://matrix.org/api/*/read`.

The URL prefix could either be static (`https://matrix.org`) or dependant on the homeserver (`https://matrix.example.com`).
In both cases, the URL could be confused with API endpoints and in the second case it would require discovery to know what scopes to ask.

The actual namespace prefix and sub divisions are open to debate.

### Insufficient privilege response

The [standard Client-Server API error response](https://spec.matrix.org/v1.2/client-server-api/#standard-error-response) could be used.

A custom HTTP header could be used instead.

## Security considerations

As we are just representing existing access models there shouldn't be anything special.

## Unstable prefix

While this feature is in development the following unstable scope prefixes should be used:

- `urn:matrix:client` --> `urn:matrix:org.matrix.msc2967.client`

## Dependencies

- [MSC2964: Delegation of auth from homeserver to OIDC Provider](https://github.com/matrix-org/matrix-spec-proposals/pull/2964)