auth.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import io
  17. from mausignald.errors import UnexpectedResponse
  18. from mausignald.types import Account
  19. from mautrix.types import MediaMessageEventContent, MessageType, ImageInfo
  20. from . import command_handler, CommandEvent, SECTION_AUTH
  21. from .. import puppet as pu
  22. try:
  23. import qrcode
  24. import PIL as _
  25. except ImportError:
  26. qrcode = None
  27. @command_handler(needs_auth=False, management_only=True, help_section=SECTION_AUTH,
  28. help_text="Link the bridge as a secondary device", help_args="[device name]")
  29. async def link(evt: CommandEvent) -> None:
  30. if qrcode is None:
  31. await evt.reply("Can't generate QR code: qrcode and/or PIL not installed")
  32. return
  33. # TODO make default device name configurable
  34. device_name = " ".join(evt.args) or "Mautrix-Signal bridge"
  35. async def callback(uri: str) -> None:
  36. buffer = io.BytesIO()
  37. image = qrcode.make(uri)
  38. size = image.pixel_size
  39. image.save(buffer, "PNG")
  40. qr = buffer.getvalue()
  41. mxc = await evt.az.intent.upload_media(qr, "image/png", "link-qr.png", len(qr))
  42. content = MediaMessageEventContent(body=uri, url=mxc, msgtype=MessageType.IMAGE,
  43. info=ImageInfo(mimetype="image/png", size=len(qr),
  44. width=size, height=size))
  45. await evt.az.intent.send_message(evt.room_id, content)
  46. account = await evt.bridge.signal.link(callback, device_name=device_name)
  47. await evt.sender.on_signin(account)
  48. await evt.reply(f"Successfully logged in as {pu.Puppet.fmt_phone(evt.sender.username)}")
  49. @command_handler(needs_auth=False, management_only=True, help_section=SECTION_AUTH,
  50. help_text="Sign into Signal as the primary device", help_args="<phone>")
  51. async def register(evt: CommandEvent) -> None:
  52. if len(evt.args) == 0:
  53. await evt.reply("**Usage**: $cmdprefix+sp register <phone>")
  54. return
  55. phone = evt.args[0]
  56. if not phone.startswith("+") or not phone[1:].isdecimal():
  57. await evt.reply(f"Please enter the phone number in international format (E.164)")
  58. return
  59. resp = await evt.bridge.signal.request("register", "verification_required")
  60. evt.sender.command_status = {
  61. "action": "Register",
  62. "room_id": evt.room_id,
  63. "next": enter_register_code,
  64. "username": resp["username"],
  65. }
  66. await evt.reply("Register SMS requested, please enter the code here.")
  67. async def enter_register_code(evt: CommandEvent) -> None:
  68. try:
  69. username = evt.sender.command_status["username"]
  70. resp = await evt.bridge.signal.request("verify", "verification_succeeded",
  71. code=evt.args[0], username=username)
  72. except UnexpectedResponse as e:
  73. if e.resp_type == "error":
  74. await evt.reply(e.data)
  75. else:
  76. raise
  77. else:
  78. account = Account.deserialize(resp)
  79. await evt.sender.on_signin(account)
  80. await evt.reply(f"Successfully logged in as {pu.Puppet.fmt_phone(evt.sender.username)}")