You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jwt.md 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # JWT Login Type
  2. Synapse comes with a non-standard login type to support
  3. [JSON Web Tokens](https://en.wikipedia.org/wiki/JSON_Web_Token). In general the
  4. documentation for
  5. [the login endpoint](https://matrix.org/docs/spec/client_server/r0.6.1#login)
  6. is still valid (and the mechanism works similarly to the
  7. [token based login](https://matrix.org/docs/spec/client_server/r0.6.1#token-based)).
  8. To log in using a JSON Web Token, clients should submit a `/login` request as
  9. follows:
  10. ```json
  11. {
  12. "type": "org.matrix.login.jwt",
  13. "token": "<jwt>"
  14. }
  15. ```
  16. The `token` field should include the JSON web token with the following claims:
  17. * A claim that encodes the local part of the user ID is required. By default,
  18. the `sub` (subject) claim is used, or a custom claim can be set in the
  19. configuration file.
  20. * The expiration time (`exp`), not before time (`nbf`), and issued at (`iat`)
  21. claims are optional, but validated if present.
  22. * The issuer (`iss`) claim is optional, but required and validated if configured.
  23. * The audience (`aud`) claim is optional, but required and validated if configured.
  24. Providing the audience claim when not configured will cause validation to fail.
  25. In the case that the token is not valid, the homeserver must respond with
  26. `403 Forbidden` and an error code of `M_FORBIDDEN`.
  27. As with other login types, there are additional fields (e.g. `device_id` and
  28. `initial_device_display_name`) which can be included in the above request.
  29. ## Preparing Synapse
  30. The JSON Web Token integration in Synapse uses the
  31. [`Authlib`](https://docs.authlib.org/en/latest/index.html) library, which must be installed
  32. as follows:
  33. * The relevant libraries are included in the Docker images and Debian packages
  34. provided by `matrix.org` so no further action is needed.
  35. * If you installed Synapse into a virtualenv, run `/path/to/env/bin/pip
  36. install synapse[jwt]` to install the necessary dependencies.
  37. * For other installation mechanisms, see the documentation provided by the
  38. maintainer.
  39. To enable the JSON web token integration, you should then add a `jwt_config` option
  40. to your configuration file. See the [configuration manual](usage/configuration/config_documentation.md#jwt_config) for some
  41. sample settings.
  42. ## How to test JWT as a developer
  43. Although JSON Web Tokens are typically generated from an external server, the
  44. example below uses a locally generated JWT.
  45. 1. Configure Synapse with JWT logins, note that this example uses a pre-shared
  46. secret and an algorithm of HS256:
  47. ```yaml
  48. jwt_config:
  49. enabled: true
  50. secret: "my-secret-token"
  51. algorithm: "HS256"
  52. ```
  53. 2. Generate a JSON web token:
  54. You can use the following short Python snippet to generate a JWT
  55. protected by an HMAC.
  56. Take care that the `secret` and the algorithm given in the `header` match
  57. the entries from `jwt_config` above.
  58. ```python
  59. from authlib.jose import jwt
  60. header = {"alg": "HS256"}
  61. payload = {"sub": "user1", "aud": ["audience"]}
  62. secret = "my-secret-token"
  63. result = jwt.encode(header, payload, secret)
  64. print(result.decode("ascii"))
  65. ```
  66. 3. Query for the login types and ensure `org.matrix.login.jwt` is there:
  67. ```bash
  68. curl http://localhost:8080/_matrix/client/r0/login
  69. ```
  70. 4. Login used the generated JSON web token from above:
  71. ```bash
  72. $ curl http://localhost:8082/_matrix/client/r0/login -X POST \
  73. --data '{"type":"org.matrix.login.jwt","token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Ag71GT8v01UO3w80aqRPTeuVPBIBZkYhNTJJ-_-zQIc"}'
  74. {
  75. "access_token": "<access token>",
  76. "device_id": "ACBDEFGHI",
  77. "home_server": "localhost:8080",
  78. "user_id": "@test-user:localhost:8480"
  79. }
  80. ```
  81. You should now be able to use the returned access token to query the client API.