|
17 | 17 | from aiohttp import ClientConnectionError |
18 | 18 | from yarl import URL |
19 | 19 |
|
20 | | -from mautrix.api import Path |
21 | 20 | from mautrix.appservice import AppService, IntentAPI |
| 21 | +from mautrix.client import ClientAPI |
22 | 22 | from mautrix.errors import ( |
23 | 23 | IntentError, |
24 | 24 | MatrixError, |
|
33 | 33 | Filter, |
34 | 34 | FilterID, |
35 | 35 | LoginType, |
| 36 | + MatrixUserIdentifier, |
36 | 37 | PresenceState, |
37 | 38 | RoomEventFilter, |
38 | 39 | RoomFilter, |
@@ -182,31 +183,32 @@ async def _login_with_shared_secret(cls, mxid: UserID) -> str: |
182 | 183 | base_url = cls.az.intent.api.base_url |
183 | 184 | else: |
184 | 185 | raise AutologinError(f"No homeserver URL configured for {server}") |
185 | | - url = base_url / str(Path.v3.login) |
186 | | - headers = {"Content-Type": "application/json"} |
187 | | - login_req = { |
188 | | - "initial_device_display_name": cls.login_device_name, |
189 | | - "device_id": cls.login_device_name, |
190 | | - "identifier": { |
191 | | - "type": "m.id.user", |
192 | | - "user": mxid, |
193 | | - }, |
194 | | - } |
| 186 | + client = ClientAPI(base_url=base_url) |
| 187 | + login_args = {} |
195 | 188 | if secret == b"appservice": |
196 | | - login_req["type"] = str(LoginType.APPSERVICE) |
197 | | - headers["Authorization"] = f"Bearer {cls.az.as_token}" |
| 189 | + login_type = LoginType.APPSERVICE |
| 190 | + client.api.token = cls.az.as_token |
198 | 191 | else: |
199 | | - login_req["type"] = str(LoginType.PASSWORD) |
200 | | - login_req["password"] = hmac.new( |
201 | | - secret, mxid.encode("utf-8"), hashlib.sha512 |
202 | | - ).hexdigest() |
203 | | - resp = await cls.az.http_session.post(url, data=json.dumps(login_req), headers=headers) |
204 | | - data = await resp.json() |
205 | | - try: |
206 | | - return data["access_token"] |
207 | | - except KeyError: |
208 | | - error_msg = data.get("error", data.get("errcode", f"HTTP {resp.status}")) |
209 | | - raise AutologinError(f"Didn't get an access token: {error_msg}") from None |
| 192 | + flows = await client.get_login_flows() |
| 193 | + flow = flows.get_first_of_type(LoginType.DEVTURE_SHARED_SECRET, LoginType.PASSWORD) |
| 194 | + if not flow: |
| 195 | + raise AutologinError("No supported shared secret auth login flows") |
| 196 | + login_type = flow.type |
| 197 | + token = hmac.new(secret, mxid.encode("utf-8"), hashlib.sha512).hexdigest() |
| 198 | + if login_type == LoginType.DEVTURE_SHARED_SECRET: |
| 199 | + login_args["token"] = token |
| 200 | + elif login_type == LoginType.PASSWORD: |
| 201 | + login_args["password"] = token |
| 202 | + resp = await client.login( |
| 203 | + identifier=MatrixUserIdentifier(user=mxid), |
| 204 | + device_id=cls.login_device_name, |
| 205 | + initial_device_display_name=cls.login_device_name, |
| 206 | + login_type=login_type, |
| 207 | + **login_args, |
| 208 | + store_access_token=False, |
| 209 | + update_hs_url=False, |
| 210 | + ) |
| 211 | + return resp.access_token |
210 | 212 |
|
211 | 213 | async def switch_mxid( |
212 | 214 | self, access_token: str | None, mxid: UserID | None, start_sync_task: bool = True |
|
0 commit comments