From c6e777b8241c698a94de36c7eb8785fe8f70a1fc Mon Sep 17 00:00:00 2001
From: Tulir Asokan <tulir@maunium.net>
Date: Sun, 15 Dec 2019 18:45:30 +0200
Subject: [PATCH] Add option to log in to custom puppet with shared secret

---
 example-config.yaml        |  6 ++++++
 mautrix_facebook/config.py |  1 +
 mautrix_facebook/puppet.py |  3 +++
 mautrix_facebook/user.py   | 10 ++++++++++
 setup.py                   |  2 +-
 5 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/example-config.yaml b/example-config.yaml
index 61b833e..72dd388 100644
--- a/example-config.yaml
+++ b/example-config.yaml
@@ -82,6 +82,12 @@ bridge:
     # Whether or not to use /sync to get presence, read receipts and typing notifications when using
     # your own Matrix account as the Matrix puppet for your Facebook account.
     sync_with_custom_puppets: true
+    # Shared secret for https://github.com/devture/matrix-synapse-shared-secret-auth
+    #
+    # If set, custom puppets will be enabled automatically for local users
+    # instead of users having to find an access token and run `login-matrix`
+    # manually.
+    login_shared_secret: null
     # Whether or not to bridge presence in both directions. Facebook allows users not to broadcast
     # presence, but then it won't send other users' presence to the client.
     presence: true
diff --git a/mautrix_facebook/config.py b/mautrix_facebook/config.py
index b2c2b7e..6d340ed 100644
--- a/mautrix_facebook/config.py
+++ b/mautrix_facebook/config.py
@@ -36,6 +36,7 @@ class Config(BaseBridgeConfig):
         copy("bridge.initial_chat_sync")
         copy("bridge.invite_own_puppet_to_pm")
         copy("bridge.sync_with_custom_puppets")
+        copy("bridge.login_shared_secret")
         copy("bridge.presence")
         copy("bridge.update_avatar_initial_sync")
 
diff --git a/mautrix_facebook/puppet.py b/mautrix_facebook/puppet.py
index f896680..2db002e 100644
--- a/mautrix_facebook/puppet.py
+++ b/mautrix_facebook/puppet.py
@@ -260,4 +260,7 @@ def init(context: 'Context') -> Iterable[Awaitable[None]]:
     Puppet.mxid_template = SimpleTemplate(config["bridge.username_template"], "userid",
                                           prefix="@", suffix=f":{Puppet.hs_domain}", type=str)
 
+    Puppet.login_shared_secret = config["bridge.login_shared_secret"].encode("utf-8")
+    Puppet.login_device_name = "Facebook Messenger Bridge"
+
     return (puppet.try_start() for puppet in Puppet.get_all_with_custom_mxid())
diff --git a/mautrix_facebook/user.py b/mautrix_facebook/user.py
index 6788cd2..498d548 100644
--- a/mautrix_facebook/user.py
+++ b/mautrix_facebook/user.py
@@ -168,6 +168,16 @@ class User(Client):
     async def post_login(self) -> None:
         self.log.info("Running post-login actions")
         self.by_fbid[self.fbid] = self
+
+        try:
+            puppet = pu.Puppet.get_by_fbid(self.fbid)
+
+            if puppet.custom_mxid != self.mxid and puppet.can_auto_login(self.mxid):
+                self.log.info(f"Automatically enabling custom puppet")
+                await puppet.switch_mxid(access_token="auto", mxid=self.mxid)
+        except Exception:
+            self.log.exception("Failed to automatically enable custom puppet")
+
         await self._create_community()
         await self.sync_contacts()
         await self.sync_threads()
diff --git a/setup.py b/setup.py
index ba8c9f8..2b1f257 100644
--- a/setup.py
+++ b/setup.py
@@ -33,7 +33,7 @@ setuptools.setup(
 
     install_requires=[
         "aiohttp>=3.0.1,<4",
-        "mautrix>=0.4.0rc2,<0.5.0",
+        "mautrix>=0.4.0rc4,<0.5.0",
         "ruamel.yaml>=0.15.94,<0.17",
         "commonmark>=0.8,<0.10",
         "python-magic>=0.4,<0.5",
-- 
GitLab