-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add JWT documentation and improve sample config #7776
Changes from 2 commits
e341ec9
08578a8
820dfc3
0302489
0c0030b
cd7e06d
9f101aa
a619e90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# JWT Login Type | ||
|
||
Synapse comes with a non-standard login type to support | ||
[JSON Web Tokens](https://en.wikipedia.org/wiki/JSON_Web_Token). In general the | ||
documentation for | ||
[the login endpoint](https://matrix.org/docs/spec/client_server/r0.6.1#login) | ||
is still valid (and the mechanism works similarly to the | ||
[token based login](https://matrix.org/docs/spec/client_server/r0.6.1#token-based)). | ||
|
||
To log in using a JSON Web Token, clients should submit a `/login` request as | ||
follows: | ||
|
||
```json | ||
{ | ||
"type": "org.matrix.login.jwt", | ||
"token": "<jwt>" | ||
} | ||
``` | ||
|
||
Note that the login type of `m.login.jwt` is supported, but is deprecated. This | ||
will be removed in a future version of Synapse. | ||
|
||
The `jwt` should encode the local part of the user ID as the standard `sub` | ||
claim. In the case that the token is not valid, the homeserver must respond with | ||
`401 Unauthorized` and an error code of `M_UNAUTHORIZED`. | ||
|
||
(Note that this differs from the token based logins which return a | ||
`403 Forbidden` and an error code of `M_FORBIDDEN` if an error occurs.) | ||
|
||
As with other login types, there are additional fields (e.g. `device_id` and | ||
`initial_device_display_name`) which can be included in the above request. | ||
|
||
## How to test JWT as a developer | ||
|
||
Although JSON Web Tokens are typically generated from an external server, the | ||
examples below use [PyJWT](https://pyjwt.readthedocs.io/en/latest/) directly. | ||
|
||
1. Configure Synapse with JWT logins: | ||
|
||
```yaml | ||
jwt_config: | ||
enabled: true | ||
secret: "my-secret-token" | ||
algorithm: "HS256" | ||
``` | ||
2. Generate a JSON web token: | ||
|
||
```bash | ||
$ pyjwt --key=my-secret-token --alg=HS256 encode sub=test-user | ||
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Ag71GT8v01UO3w80aqRPTeuVPBIBZkYhNTJJ-_-zQIc | ||
``` | ||
3. Query for the login types and ensure `org.matrix.login.jwt` is there: | ||
|
||
```bash | ||
curl http://localhost:8080/_matrix/client/r0/login | ||
``` | ||
4. Login used the generated JSON web token from above: | ||
|
||
```bash | ||
$ curl http://localhost:8082/_matrix/client/r0/login -X POST \ | ||
--data '{"type":"org.matrix.login.jwt","token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Ag71GT8v01UO3w80aqRPTeuVPBIBZkYhNTJJ-_-zQIc"}' | ||
{ | ||
"access_token": "<access token>", | ||
"device_id": "ACBDEFGHI", | ||
"home_server": "localhost:8080", | ||
"user_id": "@test-user:localhost:8480" | ||
} | ||
``` | ||
|
||
You should now be able to use the returned access token to query the client API. |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -45,10 +45,17 @@ def read_config(self, config, **kwargs): | |||||||||||
|
||||||||||||
def generate_config_section(self, **kwargs): | ||||||||||||
return """\ | ||||||||||||
# The JWT needs to contain a globally unique "sub" (subject) claim. | ||||||||||||
# Configure the non-standard JSON Web Token login type. | ||||||||||||
# | ||||||||||||
# Each JSON Web Token needs to contain a "sub" (subject) claim, which is | ||||||||||||
# used as the localpart of the mxid. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no guarantee the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently it is assumed that it meets the restrictions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked at this again since I wasn't 100% sure this was true and the string just gets jammed into one of our synapse/synapse/rest/client/v1/login.py Lines 376 to 380 in 21a212f
So yeah, this is essentially completely unchecked. 😢 |
||||||||||||
# | ||||||||||||
#jwt_config: | ||||||||||||
# enabled: true | ||||||||||||
# secret: "a secret" | ||||||||||||
# algorithm: "HS256" | ||||||||||||
# Uncomment the following to enable JWT logins. | ||||||||||||
#enabled: true | ||||||||||||
# This is either the private secret or the public key used to decode | ||||||||||||
# the contents of the JWT. | ||||||||||||
#secret: "a secret" | ||||||||||||
# The algorithm used to sign the JWT. | ||||||||||||
#algorithm: "HS256" | ||||||||||||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should probably update the code to be the same as this, but I wanted to keep this PR to documentation only.