This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement MSC3231: Token authenticated registration #10142
Merged
anoadragon453
merged 54 commits into
matrix-org:develop
from
govynnus:token-registration
Aug 21, 2021
Merged
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
5856f81
Hard-coded token authenticated registration
govynnus 5f21580
Create registration_tokens table
govynnus 2b8726c
Check in database to validate registration token
govynnus 5b1ec0b
Increment `completed` when registration token used
govynnus 15e5769
Rename total_uses to uses_allowed
govynnus 9c502b0
Improve unit tests
govynnus e7754a9
Increment pending while registration in progress
govynnus ef05a6d
Add unit test for registration token expiry
govynnus 53f0e05
Fix config file related bits
govynnus 7883191
Run connected database ops in same transaction
govynnus 1debc22
Fix some formatting problems
govynnus 6ac376d
Test `completed` is empty when auth should fail
govynnus dfa8fec
Override type of simple_select_one_txn
govynnus c89d786
Raise error if token changes during UIA
govynnus e7bd00a
Add validity checking endpoint
govynnus d6704fd
Use AuthHandler methods for accessing UIA session
govynnus 003e67d
Rate limit validity checking endpoint
govynnus 3c51680
Use LoginError rather than SynapseError in checker
govynnus af90be7
Add fallback
govynnus 1552b70
Docs for currently non-existent admin API
govynnus 4df4a6e
Implement admin API
govynnus 6901eee
Move admin api docs to correct location
govynnus 93f752d
Include general API shape in docstrings
govynnus b2bf3ac
More input validation when creating and updating
govynnus 5d5bdef
Add space to SQL query
govynnus b61c7f6
Fix SQL query for invalid tokens
govynnus e7495e6
Decrease pending when UIA session expires
govynnus 39d24d2
Add type to test argument
govynnus 70cc9d2
Add test for session expiry with deleted token
govynnus 09f6572
Use f-strings rather than str.format()
govynnus 36adec4
Update docs/usage/administration/admin_api/registration_tokens.md
govynnus 7e539f5
Use more descriptive name
govynnus 1cf29c9
Return 200 when nothing to update
govynnus 7f9efcd
Remove unneeded else and add missing f
govynnus e9435f8
Run linter
govynnus f6e4831
Add uses_allowed to updating example in docstring
govynnus 7208760
Add return values to docstring
govynnus 47b8837
Add docstring to validity checking endpoint
govynnus b76099e
Move functions into RegistrationWorkerStore
govynnus 86bbc24
Merge branch 'develop' into token-registration
govynnus c6cb80b
Add link to admin API docs in config file
govynnus ba22ffd
Move table creation SQL to latest delta
govynnus c775dce
Add changelog entry
govynnus f327b29
Regenerate sample config
govynnus c6bcae2
Move table creation sql to actual newest delta
govynnus 01a74da
Avoid integrity error when creating tokens
govynnus 5bfc707
Fix docs, comments and variable names
govynnus b5608c3
Try again if generated token already exists
govynnus bf28876
Let validity checking endpoint be used by workers
govynnus 2e59dda
Document usage of `null` when updating tokens
govynnus df0077d
Merge remote-tracking branch 'upstream/develop' into token-registration
govynnus 54867ef
Simplify retrying of token generation
govynnus 20b566c
Small additions to admin api documentation
govynnus 04b237a
Update synapse/storage/databases/main/registration.py
anoadragon453 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add support for [MSC3231 - Token authenticated registration](https://github.com/matrix-org/matrix-doc/pull/3231). Users can be required to submit a token during registration to authenticate themselves. Contributed by Callum Brown. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
295 changes: 295 additions & 0 deletions
295
docs/usage/administration/admin_api/registration_tokens.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,295 @@ | ||
| # Registration Tokens | ||
|
|
||
| This API allows you to manage tokens which can be used to authenticate | ||
| registration requests, as proposed in [MSC3231](https://github.com/govynnus/matrix-doc/blob/token-registration/proposals/3231-token-authenticated-registration.md). | ||
| To use it, you will need to enable the `registration_requires_token` config | ||
| option, and authenticate by providing an `access_token` for a server admin: | ||
| see [Admin API](../../usage/administration/admin_api). | ||
| Note that this API is still experimental; not all clients may support it yet. | ||
|
|
||
|
|
||
| ## Registration token objects | ||
|
|
||
| Most endpoints make use of JSON objects that contain details about tokens. | ||
| These objects have the following fields: | ||
| - `token`: The token which can be used to authenticate registration. | ||
| - `uses_allowed`: The number of times the token can be used to complete a | ||
| registration before it becomes invalid. | ||
| - `pending`: The number of pending uses the token has. When someone uses | ||
| the token to authenticate themselves, the pending counter is incremented | ||
| so that the token is not used more than the permitted number of times. | ||
| When the person completes registration the pending counter is decremented, | ||
| and the completed counter is incremented. | ||
| - `completed`: The number of times the token has been used to successfully | ||
| complete a registration. | ||
| - `expiry_time`: The latest time the token is valid. Given as the number of | ||
| milliseconds since 1970-01-01 00:00:00 UTC (the start of the Unix epoch). | ||
| To convert this into a human-readable form you can remove the milliseconds | ||
| and use the `date` command. For example, `date -d '@1625394937'`. | ||
|
|
||
|
|
||
| ## List all tokens | ||
|
|
||
| Lists all tokens and details about them. If the request is successful, the top | ||
| level JSON object will have a `registration_tokens` key which is an array of | ||
| registration token objects. | ||
|
|
||
| ``` | ||
| GET /_synapse/admin/v1/registration_tokens | ||
| ``` | ||
|
|
||
| Optional query parameters: | ||
| - `valid`: `true` or `false`. If `true`, only valid tokens are returned. | ||
| If `false`, only tokens that have expired or have had all uses exhausted are | ||
| returned. If omitted, all tokens are returned regardless of validity. | ||
|
|
||
| Example: | ||
|
|
||
| ``` | ||
| GET /_synapse/admin/v1/registration_tokens | ||
| ``` | ||
| ``` | ||
| 200 OK | ||
|
|
||
| { | ||
| "registration_tokens": [ | ||
| { | ||
| "token": "abcd", | ||
| "uses_allowed": 3, | ||
| "pending": 0, | ||
| "completed": 1, | ||
| "expiry_time": null | ||
| }, | ||
| { | ||
| "token": "pqrs", | ||
| "uses_allowed": 2, | ||
| "pending": 1, | ||
| "completed": 1, | ||
| "expiry_time": null | ||
| }, | ||
| { | ||
| "token": "wxyz", | ||
| "uses_allowed": null, | ||
| "pending": 0, | ||
| "completed": 9, | ||
| "expiry_time": 1625394937000 // 2021-07-04 10:35:37 UTC | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| Example using the `valid` query parameter: | ||
|
|
||
| ``` | ||
| GET /_synapse/admin/v1/registration_tokens?valid=false | ||
| ``` | ||
| ``` | ||
| 200 OK | ||
|
|
||
| { | ||
| "registration_tokens": [ | ||
| { | ||
| "token": "pqrs", | ||
| "uses_allowed": 2, | ||
| "pending": 1, | ||
| "completed": 1, | ||
| "expiry_time": null | ||
| }, | ||
| { | ||
| "token": "wxyz", | ||
| "uses_allowed": null, | ||
| "pending": 0, | ||
| "completed": 9, | ||
| "expiry_time": 1625394937000 // 2021-07-04 10:35:37 UTC | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## Get one token | ||
|
|
||
| Get details about a single token. If the request is successful, the response | ||
| body will be a registration token object. | ||
|
|
||
| ``` | ||
| GET /_synapse/admin/v1/registration_tokens/<token> | ||
| ``` | ||
|
|
||
| Path parameters: | ||
| - `token`: The registration token to return details of. | ||
|
|
||
| Example: | ||
|
|
||
| ``` | ||
| GET /_synapse/admin/v1/registration_tokens/abcd | ||
| ``` | ||
| ``` | ||
| 200 OK | ||
|
|
||
| { | ||
| "token": "abcd", | ||
| "uses_allowed": 3, | ||
| "pending": 0, | ||
| "completed": 1, | ||
| "expiry_time": null | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## Create token | ||
|
|
||
| Create a new registration token. If the request is successful, the newly created | ||
| token will be returned as a registration token object in the response body. | ||
|
|
||
| ``` | ||
| POST /_synapse/admin/v1/registration_tokens/new | ||
| ``` | ||
|
|
||
| The request body must be a JSON object and can contain the following fields: | ||
| - `token`: The registration token. A string of no more than 64 characters that | ||
| consists only of characters matched by the regex `[A-Za-z0-9-_]`. | ||
| Default: randomly generated. | ||
| - `uses_allowed`: The integer number of times the token can be used to complete | ||
| a registration before it becomes invalid. | ||
| Default: `null` (unlimited uses). | ||
| - `expiry_time`: The latest time the token is valid. Given as the number of | ||
| milliseconds since 1970-01-01 00:00:00 UTC (the start of the Unix epoch). | ||
| You could use, for example, `date '+%s000' -d 'tomorrow'`. | ||
| Default: `null` (token does not expire). | ||
| - `length`: The length of the token randomly generated if `token` is not | ||
| specified. Must be between 1 and 64 inclusive. Default: `16`. | ||
|
|
||
| If a field is omitted the default is used. | ||
|
|
||
| Example using defaults: | ||
|
|
||
| ``` | ||
| POST /_synapse/admin/v1/registration_tokens/new | ||
|
|
||
| {} | ||
| ``` | ||
| ``` | ||
| 200 OK | ||
|
|
||
| { | ||
| "token": "0M-9jbkf2t_Tgiw1", | ||
| "uses_allowed": null, | ||
| "pending": 0, | ||
| "completed": 0, | ||
| "expiry_time": null | ||
| } | ||
| ``` | ||
|
|
||
| Example specifying some fields: | ||
|
|
||
| ``` | ||
| POST /_synapse/admin/v1/registration_tokens/new | ||
|
|
||
| { | ||
| "token": "defg", | ||
| "uses_allowed": 1 | ||
| } | ||
| ``` | ||
| ``` | ||
| 200 OK | ||
|
|
||
| { | ||
| "token": "defg", | ||
| "uses_allowed": 1, | ||
| "pending": 0, | ||
| "completed": 0, | ||
| "expiry_time": null | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## Update token | ||
|
|
||
| Update the number of allowed uses or expiry time of a token. If the request is | ||
| successful, the updated token will be returned as a registration token object | ||
| in the response body. | ||
|
|
||
| ``` | ||
| PUT /_synapse/admin/v1/registration_tokens/<token> | ||
| ``` | ||
|
|
||
| Path parameters: | ||
| - `token`: The registration token to update. | ||
|
|
||
| The request body must be a JSON object and can contain the following fields: | ||
| - `uses_allowed`: The integer number of times the token can be used to complete | ||
| a registration before it becomes invalid. By setting `uses_allowed` to `0` | ||
| the token can be easily made invalid without deleting it. | ||
| If `null` the token will have an unlimited number of uses. | ||
| - `expiry_time`: The latest time the token is valid. Given as the number of | ||
| milliseconds since 1970-01-01 00:00:00 UTC (the start of the Unix epoch). | ||
| If `null` the token will not expire. | ||
|
|
||
| If a field is omitted its value is not modified. | ||
|
|
||
| Example: | ||
|
|
||
| ``` | ||
| PUT /_synapse/admin/v1/registration_tokens/defg | ||
|
|
||
| { | ||
| "expiry_time": 4781243146000 // 2121-07-06 11:05:46 UTC | ||
| } | ||
| ``` | ||
| ``` | ||
| 200 OK | ||
|
|
||
| { | ||
| "token": "defg", | ||
| "uses_allowed": 1, | ||
| "pending": 0, | ||
| "completed": 0, | ||
| "expiry_time": 4781243146000 | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## Delete token | ||
|
|
||
| Delete a registration token. If the request is successful, the response body | ||
| will be an empty JSON object. | ||
|
|
||
| ``` | ||
| DELETE /_synapse/admin/v1/registration_tokens/<token> | ||
| ``` | ||
|
|
||
| Path parameters: | ||
| - `token`: The registration token to delete. | ||
|
|
||
| Example: | ||
|
|
||
| ``` | ||
| DELETE /_synapse/admin/v1/registration_tokens/wxyz | ||
| ``` | ||
| ``` | ||
| 200 OK | ||
|
|
||
| {} | ||
| ``` | ||
|
|
||
|
|
||
| ## Errors | ||
|
|
||
| If a request fails a "standard error response" will be returned as defined in | ||
| the [Matrix Client-Server API specification](https://matrix.org/docs/spec/client_server/r0.6.1#api-standards). | ||
|
|
||
| For example, if the token specified in a path parameter does not exist a | ||
| `404 Not Found` error will be returned. | ||
|
|
||
| ``` | ||
| GET /_synapse/admin/v1/registration_tokens/1234 | ||
| ``` | ||
| ``` | ||
| 404 Not Found | ||
|
|
||
| { | ||
| "errcode": "M_NOT_FOUND", | ||
| "error": "No such registration token: 1234" | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.