register_new_matrix_user 4.4 KB

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