1
0

register_new_matrix_user 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2015, 2016 OpenMarket Ltd
  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. import argparse
  17. import getpass
  18. import hashlib
  19. import hmac
  20. import json
  21. import sys
  22. import urllib2
  23. import yaml
  24. def request_registration(user, password, server_location, shared_secret, admin=False):
  25. mac = hmac.new(
  26. key=shared_secret,
  27. digestmod=hashlib.sha1,
  28. )
  29. mac.update(user)
  30. mac.update("\x00")
  31. mac.update(password)
  32. mac.update("\x00")
  33. mac.update("admin" if admin else "notadmin")
  34. mac = mac.hexdigest()
  35. data = {
  36. "user": user,
  37. "password": password,
  38. "mac": mac,
  39. "type": "org.matrix.login.shared_secret",
  40. "admin": admin,
  41. }
  42. server_location = server_location.rstrip("/")
  43. print "Sending registration request..."
  44. req = urllib2.Request(
  45. "%s/_matrix/client/api/v1/register" % (server_location,),
  46. data=json.dumps(data),
  47. headers={'Content-Type': 'application/json'}
  48. )
  49. try:
  50. if sys.version_info[:3] >= (2, 7, 9):
  51. # As of version 2.7.9, urllib2 now checks SSL certs
  52. import ssl
  53. f = urllib2.urlopen(req, context=ssl.SSLContext(ssl.PROTOCOL_SSLv23))
  54. else:
  55. f = urllib2.urlopen(req)
  56. f.read()
  57. f.close()
  58. print "Success."
  59. except urllib2.HTTPError as e:
  60. print "ERROR! Received %d %s" % (e.code, e.reason,)
  61. if 400 <= e.code < 500:
  62. if e.info().type == "application/json":
  63. resp = json.load(e)
  64. if "error" in resp:
  65. print resp["error"]
  66. sys.exit(1)
  67. def register_new_user(user, password, server_location, shared_secret, admin):
  68. if not user:
  69. try:
  70. default_user = getpass.getuser()
  71. except:
  72. default_user = None
  73. if default_user:
  74. user = raw_input("New user localpart [%s]: " % (default_user,))
  75. if not user:
  76. user = default_user
  77. else:
  78. user = raw_input("New user localpart: ")
  79. if not user:
  80. print "Invalid user name"
  81. sys.exit(1)
  82. if not password:
  83. password = getpass.getpass("Password: ")
  84. if not password:
  85. print "Password cannot be blank."
  86. sys.exit(1)
  87. confirm_password = getpass.getpass("Confirm password: ")
  88. if password != confirm_password:
  89. print "Passwords do not match"
  90. sys.exit(1)
  91. if not admin:
  92. admin = raw_input("Make admin [no]: ")
  93. if admin in ("y", "yes", "true"):
  94. admin = True
  95. else:
  96. admin = False
  97. request_registration(user, password, server_location, shared_secret, bool(admin))
  98. if __name__ == "__main__":
  99. parser = argparse.ArgumentParser(
  100. description="Used to register new users with a given home server when"
  101. " registration has been disabled. The home server must be"
  102. " configured with the 'registration_shared_secret' option"
  103. " set.",
  104. )
  105. parser.add_argument(
  106. "-u", "--user",
  107. default=None,
  108. help="Local part of the new user. Will prompt if omitted.",
  109. )
  110. parser.add_argument(
  111. "-p", "--password",
  112. default=None,
  113. help="New password for user. Will prompt if omitted.",
  114. )
  115. parser.add_argument(
  116. "-a", "--admin",
  117. action="store_true",
  118. help="Register new user as an admin. Will prompt if omitted.",
  119. )
  120. group = parser.add_mutually_exclusive_group(required=True)
  121. group.add_argument(
  122. "-c", "--config",
  123. type=argparse.FileType('r'),
  124. help="Path to server config file. Used to read in shared secret.",
  125. )
  126. group.add_argument(
  127. "-k", "--shared-secret",
  128. help="Shared secret as defined in server config file.",
  129. )
  130. parser.add_argument(
  131. "server_url",
  132. default="https://localhost:8448",
  133. nargs='?',
  134. help="URL to use to talk to the home server. Defaults to "
  135. " 'https://localhost:8448'.",
  136. )
  137. args = parser.parse_args()
  138. if "config" in args and args.config:
  139. config = yaml.safe_load(args.config)
  140. secret = config.get("registration_shared_secret", None)
  141. if not secret:
  142. print "No 'registration_shared_secret' defined in config."
  143. sys.exit(1)
  144. else:
  145. secret = args.shared_secret
  146. register_new_user(args.user, args.password, args.server_url, secret, args.admin)