auth.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from typing import Union
  17. import io
  18. from mausignald.errors import AuthorizationFailedError, TimeoutException, UnexpectedResponse
  19. from mautrix.appservice import IntentAPI
  20. from mautrix.bridge.commands import HelpSection, command_handler
  21. from mautrix.types import EventID, ImageInfo, MediaMessageEventContent, MessageType
  22. from .. import puppet as pu
  23. from ..util import normalize_number
  24. from .typehint import CommandEvent
  25. try:
  26. import PIL as _
  27. import qrcode
  28. except ImportError:
  29. qrcode = None
  30. SECTION_AUTH = HelpSection("Authentication", 10, "")
  31. async def make_qr(
  32. intent: IntentAPI, data: Union[str, bytes], body: str = None
  33. ) -> MediaMessageEventContent:
  34. # TODO always encrypt QR codes?
  35. buffer = io.BytesIO()
  36. image = qrcode.make(data)
  37. size = image.pixel_size
  38. image.save(buffer, "PNG")
  39. qr = buffer.getvalue()
  40. mxc = await intent.upload_media(qr, "image/png", "qr.png", len(qr))
  41. return MediaMessageEventContent(
  42. body=body or data,
  43. url=mxc,
  44. msgtype=MessageType.IMAGE,
  45. info=ImageInfo(mimetype="image/png", size=len(qr), width=size, height=size),
  46. )
  47. @command_handler(
  48. needs_auth=False,
  49. management_only=True,
  50. help_section=SECTION_AUTH,
  51. help_text="Link the bridge as a secondary device",
  52. help_args="[device name]",
  53. )
  54. async def link(evt: CommandEvent) -> None:
  55. if qrcode is None:
  56. await evt.reply("Can't generate QR code: qrcode and/or PIL not installed")
  57. return
  58. if await evt.sender.is_logged_in():
  59. await evt.reply(
  60. "You're already logged in. "
  61. "If you want to relink, log out with `$cmdprefix+sp logout` first."
  62. )
  63. return
  64. # TODO make default device name configurable
  65. device_name = " ".join(evt.args) or "Mautrix-Signal bridge"
  66. sess = await evt.bridge.signal.start_link()
  67. content = await make_qr(evt.az.intent, sess.uri)
  68. event_id = await evt.az.intent.send_message(evt.room_id, content)
  69. try:
  70. account = await evt.bridge.signal.finish_link(
  71. session_id=sess.session_id, overwrite=True, device_name=device_name
  72. )
  73. except TimeoutException:
  74. await evt.reply("Linking timed out, please try again.")
  75. except Exception:
  76. evt.log.exception("Fatal error while waiting for linking to finish")
  77. await evt.reply(
  78. "Fatal error while waiting for linking to finish (see logs for more details)"
  79. )
  80. else:
  81. await evt.sender.on_signin(account)
  82. await evt.reply(f"Successfully logged in as {pu.Puppet.fmt_phone(evt.sender.username)}")
  83. finally:
  84. await evt.main_intent.redact(evt.room_id, event_id)
  85. @command_handler(
  86. needs_auth=False,
  87. management_only=True,
  88. help_section=SECTION_AUTH,
  89. is_enabled_for=lambda evt: evt.config["signal.registration_enabled"],
  90. help_text="Sign into Signal as the primary device",
  91. help_args="<phone>",
  92. )
  93. async def register(evt: CommandEvent) -> None:
  94. if len(evt.args) == 0:
  95. await evt.reply("**Usage**: $cmdprefix+sp register [--voice] [--captcha <token>] <phone>")
  96. return
  97. if await evt.sender.is_logged_in():
  98. await evt.reply(
  99. "You're already logged in. "
  100. "If you want to re-register, log out with `$cmdprefix+sp logout` first."
  101. )
  102. return
  103. voice = False
  104. captcha = None
  105. while True:
  106. flag = evt.args[0].lower()
  107. if flag == "--voice" or flag == "-v":
  108. voice = True
  109. evt.args = evt.args[1:]
  110. elif flag == "--captcha" or flag == "-c":
  111. if "=" in evt.args[0]:
  112. captcha = evt.args[0].split("=", 1)[1]
  113. evt.args = evt.args[1:]
  114. else:
  115. captcha = evt.args[1]
  116. evt.args = evt.args[2:]
  117. else:
  118. break
  119. try:
  120. phone = normalize_number(evt.args[0])
  121. except Exception:
  122. await evt.reply("Please enter the phone number in international format (E.164)")
  123. return
  124. username = await evt.bridge.signal.register(phone, voice=voice, captcha=captcha)
  125. evt.sender.command_status = {
  126. "action": "Register",
  127. "room_id": evt.room_id,
  128. "next": enter_register_code,
  129. "username": username,
  130. }
  131. await evt.reply("Register SMS requested, please enter the code here.")
  132. async def enter_register_code(evt: CommandEvent) -> None:
  133. try:
  134. username = evt.sender.command_status["username"]
  135. account = await evt.bridge.signal.verify(username, code=evt.args[0])
  136. except UnexpectedResponse as e:
  137. if e.resp_type == "error":
  138. await evt.reply(e.data)
  139. else:
  140. raise
  141. else:
  142. await evt.sender.on_signin(account)
  143. await evt.reply(
  144. f"Successfully logged in as {pu.Puppet.fmt_phone(evt.sender.username)}."
  145. f"\n\n**N.B.** You must set a Signal profile name with `$cmdprefix+sp "
  146. f"set-profile-name <name>` before you can participate in new groups."
  147. )
  148. @command_handler(
  149. needs_auth=True,
  150. management_only=True,
  151. help_section=SECTION_AUTH,
  152. help_text="Remove all local data about your Signal link",
  153. )
  154. async def logout(evt: CommandEvent) -> None:
  155. if not evt.sender.username:
  156. await evt.reply("You're not logged in")
  157. return
  158. await evt.sender.logout()
  159. await evt.reply("Successfully logged out")
  160. @command_handler(
  161. needs_auth=True,
  162. management_only=True,
  163. help_section=SECTION_AUTH,
  164. help_text="List devices linked to your Signal account",
  165. )
  166. async def list_devices(evt: CommandEvent) -> None:
  167. devices = await evt.bridge.signal.get_linked_devices(evt.sender.username)
  168. await evt.reply(
  169. "\n".join(
  170. f"* #{dev.id}: {dev.name_with_default} (created {dev.created_fmt}, last seen "
  171. f"{dev.last_seen_fmt})"
  172. for dev in devices
  173. )
  174. )
  175. @command_handler(
  176. needs_auth=True,
  177. management_only=True,
  178. help_section=SECTION_AUTH,
  179. help_text="Remove a linked device",
  180. )
  181. async def remove_linked_device(evt: CommandEvent) -> EventID:
  182. if len(evt.args) == 0:
  183. return await evt.reply("**Usage:** `$cmdprefix+sp remove-linked-device <device ID>`")
  184. device_id = int(evt.args[0])
  185. try:
  186. await evt.bridge.signal.remove_linked_device(evt.sender.username, device_id)
  187. except AuthorizationFailedError as e:
  188. return await evt.reply(f"{e} Only the primary device can remove linked devices.")
  189. return await evt.reply("Device removed")