Skip to content

Commit b6bdebb

Browse files
committed
Port to new module API
This ports the auth provider to the new module API of Synapse 1.46+. Docs: https://matrix-org.github.io/synapse/latest/modules/password_auth_provider_callbacks.html Based on anishihara@6c29f4d by @anishihara Fixes ma1uta#9
1 parent 3524b47 commit b6bdebb

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ sudo pip install git+https://github.com/ma1uta/matrix-synapse-rest-password-prov
3030
If the command fail, double check that the python version still matches. If not, please let us know by opening an issue.
3131

3232
## Configure
33-
Add or amend the `password_providers` entry like so:
33+
Add or amend the `modules` entry like so:
3434
```yaml
35-
password_providers:
35+
modules:
3636
- module: "rest_auth_provider.RestAuthProvider"
3737
config:
3838
endpoint: "http://change.me.example.com:12345"
3939
```
4040
Set `endpoint` to the value documented with the endpoint provider.
4141

42+
**NOTE:** This requires Synapse 1.46 or later! If you migrate from the legacy `password_providers`, make sure
43+
to remove the old `RestAuthProvider` entry. If the `password_providers` list is empty, you can also remove it completely or
44+
comment it out.
45+
4246
## Use
4347
1. Install, configure, restart synapse
4448
2. Try to login with a valid username and password for the endpoint configured

rest_auth_provider.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@
2020
#
2121

2222
import logging
23+
from typing import Tuple, Optional, Callable, Awaitable
24+
2325
import requests
24-
import json
2526
import time
27+
import synapse
28+
from synapse import module_api
2629

2730
logger = logging.getLogger(__name__)
2831

2932

3033
class RestAuthProvider(object):
3134

32-
def __init__(self, config, account_handler):
33-
self.account_handler = account_handler
35+
def __init__(self, config: dict, api: module_api):
36+
self.account_handler = api
3437

3538
if not config.endpoint:
3639
raise RuntimeError('Missing endpoint config')
@@ -42,6 +45,36 @@ def __init__(self, config, account_handler):
4245
logger.info('Endpoint: %s', self.endpoint)
4346
logger.info('Enforce lowercase username during registration: %s', self.regLower)
4447

48+
# register an auth callback handler
49+
# see https://matrix-org.github.io/synapse/latest/modules/password_auth_provider_callbacks.html
50+
api.register_password_auth_provider_callbacks(
51+
auth_checkers={
52+
("m.login.password", ("password",)): self.check_m_login_password
53+
}
54+
)
55+
56+
async def check_m_login_password(self, username: str,
57+
login_type: str,
58+
login_dict: "synapse.module_api.JsonDict") -> Optional[
59+
Tuple[
60+
str,
61+
Optional[Callable[["synapse.module_api.LoginResponse"], Awaitable[None]]],
62+
]
63+
]:
64+
if login_type != "m.login.password":
65+
return None
66+
67+
# get the complete MXID
68+
mxid = self.account_handler.get_qualified_user_id(username)
69+
70+
# check if the password is valid with the old function
71+
password_valid = await self.check_password(mxid, login_dict.get("password"))
72+
73+
if password_valid:
74+
return mxid, None
75+
else:
76+
return None
77+
4578
async def check_password(self, user_id, password):
4679
logger.info("Got password check for " + user_id)
4780
data = {'user': {'id': user_id, 'password': password}}

0 commit comments

Comments
 (0)