Skip to content

Commit 2d59855

Browse files
committed
Add support for com.devture.shared_secret_auth for double puppeting
1 parent 782f568 commit 2d59855

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

mautrix/bridge/custom_puppet.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from aiohttp import ClientConnectionError
1818
from yarl import URL
1919

20-
from mautrix.api import Path
2120
from mautrix.appservice import AppService, IntentAPI
21+
from mautrix.client import ClientAPI
2222
from mautrix.errors import (
2323
IntentError,
2424
MatrixError,
@@ -33,6 +33,7 @@
3333
Filter,
3434
FilterID,
3535
LoginType,
36+
MatrixUserIdentifier,
3637
PresenceState,
3738
RoomEventFilter,
3839
RoomFilter,
@@ -182,31 +183,32 @@ async def _login_with_shared_secret(cls, mxid: UserID) -> str:
182183
base_url = cls.az.intent.api.base_url
183184
else:
184185
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 = {}
195188
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
198191
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
210212

211213
async def switch_mxid(
212214
self, access_token: str | None, mxid: UserID | None, start_sync_task: bool = True

mautrix/types/auth.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class LoginType(ExtensibleEnum):
2626

2727
UNSTABLE_JWT: "LoginType" = "org.matrix.login.jwt"
2828

29+
DEVTURE_SHARED_SECRET: "LoginType" = "com.devture.shared_secret_auth"
30+
2931

3032
@dataclass
3133
class LoginFlow(SerializableAttrs):

0 commit comments

Comments
 (0)