|
| 1 | +# Password auth provider callbacks |
| 2 | + |
| 3 | +Password auth providers offer a way for server administrators to integrate |
| 4 | +their Synapse installation with an external authentication system. The callbacks can be |
| 5 | +registered by using the Module API's `register_password_auth_provider_callbacks` method. |
| 6 | + |
| 7 | +## Callbacks |
| 8 | + |
| 9 | +### `auth_checkers` |
| 10 | + |
| 11 | +``` |
| 12 | + auth_checkers: Dict[Tuple[str,Tuple], Callable] |
| 13 | +``` |
| 14 | + |
| 15 | +A dict mapping from tuples of a login type identifier (such as `m.login.password`) and a |
| 16 | +tuple of field names (such as `("password", "secret_thing")`) to authentication checking |
| 17 | +callbacks, which should be of the following form: |
| 18 | + |
| 19 | +```python |
| 20 | +async def check_auth( |
| 21 | + user: str, |
| 22 | + login_type: str, |
| 23 | + login_dict: "synapse.module_api.JsonDict", |
| 24 | +) -> Optional[ |
| 25 | + Tuple[ |
| 26 | + str, |
| 27 | + Optional[Callable[["synapse.module_api.LoginResponse"], Awaitable[None]]] |
| 28 | + ] |
| 29 | +] |
| 30 | +``` |
| 31 | + |
| 32 | +The login type and field names should be provided by the user in the |
| 33 | +request to the `/login` API. [The Matrix specification](https://matrix.org/docs/spec/client_server/latest#authentication-types) |
| 34 | +defines some types, however user defined ones are also allowed. |
| 35 | + |
| 36 | +The callback is passed the `user` field provided by the client (which might not be in |
| 37 | +`@username:server` form), the login type, and a dictionary of login secrets passed by |
| 38 | +the client. |
| 39 | + |
| 40 | +If the authentication is successful, the module must return the user's Matrix ID (e.g. |
| 41 | +`@alice:example.com`) and optionally a callback to be called with the response to the |
| 42 | +`/login` request. If the module doesn't wish to return a callback, it must return `None` |
| 43 | +instead. |
| 44 | + |
| 45 | +If the authentication is unsuccessful, the module must return `None`. |
| 46 | + |
| 47 | +### `check_3pid_auth` |
| 48 | + |
| 49 | +```python |
| 50 | +async def check_3pid_auth( |
| 51 | + medium: str, |
| 52 | + address: str, |
| 53 | + password: str, |
| 54 | +) -> Optional[ |
| 55 | + Tuple[ |
| 56 | + str, |
| 57 | + Optional[Callable[["synapse.module_api.LoginResponse"], Awaitable[None]]] |
| 58 | + ] |
| 59 | +] |
| 60 | +``` |
| 61 | + |
| 62 | +Called when a user attempts to register or log in with a third party identifier, |
| 63 | +such as email. It is passed the medium (eg. `email`), an address (eg. `jdoe@example.com`) |
| 64 | +and the user's password. |
| 65 | + |
| 66 | +If the authentication is successful, the module must return the user's Matrix ID (e.g. |
| 67 | +`@alice:example.com`) and optionally a callback to be called with the response to the `/login` request. |
| 68 | +If the module doesn't wish to return a callback, it must return None instead. |
| 69 | + |
| 70 | +If the authentication is unsuccessful, the module must return None. |
| 71 | + |
| 72 | +### `on_logged_out` |
| 73 | + |
| 74 | +```python |
| 75 | +async def on_logged_out( |
| 76 | + user_id: str, |
| 77 | + device_id: Optional[str], |
| 78 | + access_token: str |
| 79 | +) -> None |
| 80 | +``` |
| 81 | +Called during a logout request for a user. It is passed the qualified user ID, the ID of the |
| 82 | +deactivated device (if any: access tokens are occasionally created without an associated |
| 83 | +device ID), and the (now deactivated) access token. |
| 84 | + |
| 85 | +## Example |
| 86 | + |
| 87 | +The example module below implements authentication checkers for two different login types: |
| 88 | +- `my.login.type` |
| 89 | + - Expects a `my_field` field to be sent to `/login` |
| 90 | + - Is checked by the method: `self.check_my_login` |
| 91 | +- `m.login.password` (defined in [the spec](https://matrix.org/docs/spec/client_server/latest#password-based)) |
| 92 | + - Expects a `password` field to be sent to `/login` |
| 93 | + - Is checked by the method: `self.check_pass` |
| 94 | + |
| 95 | + |
| 96 | +```python |
| 97 | +from typing import Awaitable, Callable, Optional, Tuple |
| 98 | + |
| 99 | +import synapse |
| 100 | +from synapse import module_api |
| 101 | + |
| 102 | + |
| 103 | +class MyAuthProvider: |
| 104 | + def __init__(self, config: dict, api: module_api): |
| 105 | + |
| 106 | + self.api = api |
| 107 | + |
| 108 | + self.credentials = { |
| 109 | + "bob": "building", |
| 110 | + "@scoop:matrix.org": "digging", |
| 111 | + } |
| 112 | + |
| 113 | + api.register_password_auth_provider_callbacks( |
| 114 | + auth_checkers={ |
| 115 | + ("my.login_type", ("my_field",)): self.check_my_login, |
| 116 | + ("m.login.password", ("password",)): self.check_pass, |
| 117 | + }, |
| 118 | + ) |
| 119 | + |
| 120 | + async def check_my_login( |
| 121 | + self, |
| 122 | + username: str, |
| 123 | + login_type: str, |
| 124 | + login_dict: "synapse.module_api.JsonDict", |
| 125 | + ) -> Optional[ |
| 126 | + Tuple[ |
| 127 | + str, |
| 128 | + Optional[Callable[["synapse.module_api.LoginResponse"], Awaitable[None]]], |
| 129 | + ] |
| 130 | + ]: |
| 131 | + if login_type != "my.login_type": |
| 132 | + return None |
| 133 | + |
| 134 | + if self.credentials.get(username) == login_dict.get("my_field"): |
| 135 | + return self.api.get_qualified_user_id(username) |
| 136 | + |
| 137 | + async def check_pass( |
| 138 | + self, |
| 139 | + username: str, |
| 140 | + login_type: str, |
| 141 | + login_dict: "synapse.module_api.JsonDict", |
| 142 | + ) -> Optional[ |
| 143 | + Tuple[ |
| 144 | + str, |
| 145 | + Optional[Callable[["synapse.module_api.LoginResponse"], Awaitable[None]]], |
| 146 | + ] |
| 147 | + ]: |
| 148 | + if login_type != "m.login.password": |
| 149 | + return None |
| 150 | + |
| 151 | + if self.credentials.get(username) == login_dict.get("password"): |
| 152 | + return self.api.get_qualified_user_id(username) |
| 153 | +``` |
0 commit comments