register_api.rst 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. Shared-Secret Registration
  2. ==========================
  3. This API allows for the creation of users in an administrative and
  4. non-interactive way. This is generally used for bootstrapping a Synapse
  5. instance with administrator accounts.
  6. To authenticate yourself to the server, you will need both the shared secret
  7. (``registration_shared_secret`` in the homeserver configuration), and a
  8. one-time nonce. If the registration shared secret is not configured, this API
  9. is not enabled.
  10. To fetch the nonce, you need to request one from the API::
  11. > GET /_synapse/admin/v1/register
  12. < {"nonce": "thisisanonce"}
  13. Once you have the nonce, you can make a ``POST`` to the same URL with a JSON
  14. body containing the nonce, username, password, whether they are an admin
  15. (optional, False by default), and a HMAC digest of the content. Also you can
  16. set the displayname (optional, ``username`` by default).
  17. As an example::
  18. > POST /_synapse/admin/v1/register
  19. > {
  20. "nonce": "thisisanonce",
  21. "username": "pepper_roni",
  22. "displayname": "Pepper Roni",
  23. "password": "pizza",
  24. "admin": true,
  25. "mac": "mac_digest_here"
  26. }
  27. < {
  28. "access_token": "token_here",
  29. "user_id": "@pepper_roni:localhost",
  30. "home_server": "test",
  31. "device_id": "device_id_here"
  32. }
  33. The MAC is the hex digest output of the HMAC-SHA1 algorithm, with the key being
  34. the shared secret and the content being the nonce, user, password, either the
  35. string "admin" or "notadmin", and optionally the user_type
  36. each separated by NULs. For an example of generation in Python::
  37. import hmac, hashlib
  38. def generate_mac(nonce, user, password, admin=False, user_type=None):
  39. mac = hmac.new(
  40. key=shared_secret,
  41. digestmod=hashlib.sha1,
  42. )
  43. mac.update(nonce.encode('utf8'))
  44. mac.update(b"\x00")
  45. mac.update(user.encode('utf8'))
  46. mac.update(b"\x00")
  47. mac.update(password.encode('utf8'))
  48. mac.update(b"\x00")
  49. mac.update(b"admin" if admin else b"notadmin")
  50. if user_type:
  51. mac.update(b"\x00")
  52. mac.update(user_type.encode('utf8'))
  53. return mac.hexdigest()