Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2623 from matrix-org/rav/callbacks_for_auth_provi…
Browse files Browse the repository at this point in the history
…ders

Allow password_auth_providers to return a callback
  • Loading branch information
dbkr authored Nov 2, 2017
2 parents 04897c9 + 979eed4 commit 1f080a6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/password_auth_providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ Password auth provider classes may optionally provide the following methods.
the canonical ``@localpart:domain`` user id if authentication is successful,
and ``None`` if not.

Alternatively, the ``Deferred`` can resolve to a ``(str, func)`` tuple, in
which case the second field is a callback which will be called with the
result from the ``/login`` call (including ``access_token``, ``device_id``,
etc.)

``someprovider.check_password``\(*user_id*, *password*)

This method provides a simpler interface than ``get_supported_login_types``
Expand Down
17 changes: 11 additions & 6 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,19 @@ def get_session_data(self, session_id, key, default=None):
sess = self._get_session_info(session_id)
return sess.setdefault('serverdict', {}).get(key, default)

@defer.inlineCallbacks
def _check_password_auth(self, authdict, _):
if "user" not in authdict or "password" not in authdict:
raise LoginError(400, "", Codes.MISSING_PARAM)

user_id = authdict["user"]
password = authdict["password"]

return self.validate_login(user_id, {
(canonical_id, callback) = yield self.validate_login(user_id, {
"type": LoginType.PASSWORD,
"password": password,
})
defer.returnValue(canonical_id)

@defer.inlineCallbacks
def _check_recaptcha(self, authdict, clientip):
Expand Down Expand Up @@ -517,7 +519,8 @@ def validate_login(self, username, login_submission):
login_submission (dict): the whole of the login submission
(including 'type' and other relevant fields)
Returns:
Deferred[str]: canonical user id
Deferred[str, func]: canonical user id, and optional callback
to be called once the access token and device id are issued
Raises:
StoreError if there was a problem accessing the database
SynapseError if there was a problem with the request
Expand Down Expand Up @@ -581,11 +584,13 @@ def validate_login(self, username, login_submission):
),
)

returned_user_id = yield provider.check_auth(
result = yield provider.check_auth(
username, login_type, login_dict,
)
if returned_user_id:
defer.returnValue(returned_user_id)
if result:
if isinstance(result, str):
result = (result, None)
defer.returnValue(result)

if login_type == LoginType.PASSWORD:
known_login_type = True
Expand All @@ -595,7 +600,7 @@ def validate_login(self, username, login_submission):
)

if canonical_user_id:
defer.returnValue(canonical_user_id)
defer.returnValue((canonical_user_id, None))

if not known_login_type:
raise SynapseError(400, "Unknown login type %s" % login_type)
Expand Down
5 changes: 4 additions & 1 deletion synapse/rest/client/v1/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def _do_other_login(self, login_submission):
raise SynapseError(400, "User identifier is missing 'user' key")

auth_handler = self.auth_handler
canonical_user_id = yield auth_handler.validate_login(
canonical_user_id, callback = yield auth_handler.validate_login(
identifier["user"],
login_submission,
)
Expand All @@ -238,6 +238,9 @@ def _do_other_login(self, login_submission):
"device_id": device_id,
}

if callback is not None:
yield callback(result)

defer.returnValue((200, result))

@defer.inlineCallbacks
Expand Down

0 comments on commit 1f080a6

Please sign in to comment.