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

Commit 1f080a6

Browse files
authored
Merge pull request #2623 from matrix-org/rav/callbacks_for_auth_providers
Allow password_auth_providers to return a callback
2 parents 04897c9 + 979eed4 commit 1f080a6

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

docs/password_auth_providers.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ Password auth provider classes may optionally provide the following methods.
7070
the canonical ``@localpart:domain`` user id if authentication is successful,
7171
and ``None`` if not.
7272

73+
Alternatively, the ``Deferred`` can resolve to a ``(str, func)`` tuple, in
74+
which case the second field is a callback which will be called with the
75+
result from the ``/login`` call (including ``access_token``, ``device_id``,
76+
etc.)
77+
7378
``someprovider.check_password``\(*user_id*, *password*)
7479

7580
This method provides a simpler interface than ``get_supported_login_types``

synapse/handlers/auth.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,19 @@ def get_session_data(self, session_id, key, default=None):
270270
sess = self._get_session_info(session_id)
271271
return sess.setdefault('serverdict', {}).get(key, default)
272272

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

277278
user_id = authdict["user"]
278279
password = authdict["password"]
279280

280-
return self.validate_login(user_id, {
281+
(canonical_id, callback) = yield self.validate_login(user_id, {
281282
"type": LoginType.PASSWORD,
282283
"password": password,
283284
})
285+
defer.returnValue(canonical_id)
284286

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

584-
returned_user_id = yield provider.check_auth(
587+
result = yield provider.check_auth(
585588
username, login_type, login_dict,
586589
)
587-
if returned_user_id:
588-
defer.returnValue(returned_user_id)
590+
if result:
591+
if isinstance(result, str):
592+
result = (result, None)
593+
defer.returnValue(result)
589594

590595
if login_type == LoginType.PASSWORD:
591596
known_login_type = True
@@ -595,7 +600,7 @@ def validate_login(self, username, login_submission):
595600
)
596601

597602
if canonical_user_id:
598-
defer.returnValue(canonical_user_id)
603+
defer.returnValue((canonical_user_id, None))
599604

600605
if not known_login_type:
601606
raise SynapseError(400, "Unknown login type %s" % login_type)

synapse/rest/client/v1/login.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _do_other_login(self, login_submission):
219219
raise SynapseError(400, "User identifier is missing 'user' key")
220220

221221
auth_handler = self.auth_handler
222-
canonical_user_id = yield auth_handler.validate_login(
222+
canonical_user_id, callback = yield auth_handler.validate_login(
223223
identifier["user"],
224224
login_submission,
225225
)
@@ -238,6 +238,9 @@ def _do_other_login(self, login_submission):
238238
"device_id": device_id,
239239
}
240240

241+
if callback is not None:
242+
yield callback(result)
243+
241244
defer.returnValue((200, result))
242245

243246
@defer.inlineCallbacks

0 commit comments

Comments
 (0)