password_auth_providers.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. Password auth provider modules
  2. ==============================
  3. Password auth providers offer a way for server administrators to integrate
  4. their Synapse installation with an existing authentication system.
  5. A password auth provider is a Python class which is dynamically loaded into
  6. Synapse, and provides a number of methods by which it can integrate with the
  7. authentication system.
  8. This document serves as a reference for those looking to implement their own
  9. password auth providers.
  10. Required methods
  11. ----------------
  12. Password auth provider classes must provide the following methods:
  13. *class* ``SomeProvider.parse_config``\(*config*)
  14. This method is passed the ``config`` object for this module from the
  15. homeserver configuration file.
  16. It should perform any appropriate sanity checks on the provided
  17. configuration, and return an object which is then passed into ``__init__``.
  18. *class* ``SomeProvider``\(*config*, *account_handler*)
  19. The constructor is passed the config object returned by ``parse_config``,
  20. and a ``synapse.module_api.ModuleApi`` object which allows the
  21. password provider to check if accounts exist and/or create new ones.
  22. Optional methods
  23. ----------------
  24. Password auth provider classes may optionally provide the following methods.
  25. *class* ``SomeProvider.get_db_schema_files``\()
  26. This method, if implemented, should return an Iterable of ``(name,
  27. stream)`` pairs of database schema files. Each file is applied in turn at
  28. initialisation, and a record is then made in the database so that it is
  29. not re-applied on the next start.
  30. ``someprovider.get_supported_login_types``\()
  31. This method, if implemented, should return a ``dict`` mapping from a login
  32. type identifier (such as ``m.login.password``) to an iterable giving the
  33. fields which must be provided by the user in the submission to the
  34. ``/login`` api. These fields are passed in the ``login_dict`` dictionary
  35. to ``check_auth``.
  36. For example, if a password auth provider wants to implement a custom login
  37. type of ``com.example.custom_login``, where the client is expected to pass
  38. the fields ``secret1`` and ``secret2``, the provider should implement this
  39. method and return the following dict::
  40. {"com.example.custom_login": ("secret1", "secret2")}
  41. ``someprovider.check_auth``\(*username*, *login_type*, *login_dict*)
  42. This method is the one that does the real work. If implemented, it will be
  43. called for each login attempt where the login type matches one of the keys
  44. returned by ``get_supported_login_types``.
  45. It is passed the (possibly UNqualified) ``user`` provided by the client,
  46. the login type, and a dictionary of login secrets passed by the client.
  47. The method should return a Twisted ``Deferred`` object, which resolves to
  48. the canonical ``@localpart:domain`` user id if authentication is successful,
  49. and ``None`` if not.
  50. Alternatively, the ``Deferred`` can resolve to a ``(str, func)`` tuple, in
  51. which case the second field is a callback which will be called with the
  52. result from the ``/login`` call (including ``access_token``, ``device_id``,
  53. etc.)
  54. ``someprovider.check_password``\(*user_id*, *password*)
  55. This method provides a simpler interface than ``get_supported_login_types``
  56. and ``check_auth`` for password auth providers that just want to provide a
  57. mechanism for validating ``m.login.password`` logins.
  58. Iif implemented, it will be called to check logins with an
  59. ``m.login.password`` login type. It is passed a qualified
  60. ``@localpart:domain`` user id, and the password provided by the user.
  61. The method should return a Twisted ``Deferred`` object, which resolves to
  62. ``True`` if authentication is successful, and ``False`` if not.
  63. ``someprovider.on_logged_out``\(*user_id*, *device_id*, *access_token*)
  64. This method, if implemented, is called when a user logs out. It is passed
  65. the qualified user ID, the ID of the deactivated device (if any: access
  66. tokens are occasionally created without an associated device ID), and the
  67. (now deactivated) access token.
  68. It may return a Twisted ``Deferred`` object; the logout request will wait
  69. for the deferred to complete but the result is ignored.