A simple web application that allows invitation based registration to a matrix instance
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # This script has been simplified and adapted from
  18. # https://raw.githubusercontent.com/matrix-org/synapse/master/synapse/_scripts/register_new_matrix_user.py
  19. #
  20. # The purpose is to facilitate registration using a shared_secret over the open
  21. # registration that is supported in the matrix spec
  22. #
  23. import hashlib
  24. import hmac
  25. import logging
  26. from typing import Dict, Optional
  27. import requests as _requests
  28. log = logging.getLogger(__name__)
  29. def request_registration(
  30. user,
  31. password,
  32. server_location,
  33. shared_secret,
  34. admin=False,
  35. user_type=None,
  36. requests=_requests) -> Optional[Dict]:
  37. url = "%s/_matrix/client/r0/admin/register" % (server_location,)
  38. # Get the nonce
  39. r = requests.get(url, verify=False)
  40. if r.status_code is not 200:
  41. log.error("ERROR! Received %d %s" % (r.status_code, r.reason))
  42. if 400 <= r.status_code < 500:
  43. try:
  44. log.error(r.json()["error"])
  45. except Exception as e:
  46. log.error(e)
  47. return None
  48. nonce = r.json()["nonce"]
  49. mac = hmac.new(key=shared_secret.encode('utf8'), digestmod=hashlib.sha1)
  50. mac.update(nonce.encode('utf8'))
  51. mac.update(b"\x00")
  52. mac.update(user.encode('utf8'))
  53. mac.update(b"\x00")
  54. mac.update(password.encode('utf8'))
  55. mac.update(b"\x00")
  56. mac.update(b"admin" if admin else b"notadmin")
  57. if user_type:
  58. mac.update(b"\x00")
  59. mac.update(user_type.encode('utf8'))
  60. mac = mac.hexdigest()
  61. data = {
  62. "nonce": nonce,
  63. "username": user,
  64. "password": password,
  65. "mac": mac,
  66. "admin": admin,
  67. "user_type": user_type,
  68. }
  69. log.debug("Sending registration request...")
  70. r = requests.post(url, json=data, verify=False)
  71. if r.status_code is not 200:
  72. log.error("ERROR! Received %d %s" % (r.status_code, r.reason))
  73. if 400 <= r.status_code < 500:
  74. try:
  75. log.error(r.json()["error"])
  76. except Exception as e:
  77. log.error(e)
  78. return None
  79. return r.json()
  80. def register_new_user(
  81. user: str,
  82. password: str,
  83. server_location: str,
  84. shared_secret: str) -> Optional[Dict]:
  85. return request_registration(
  86. user, password, server_location, shared_secret, False, None)