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

Commit b19d9e2

Browse files
authored
Merge pull request #2624 from matrix-org/rav/password_provider_notify_logout
Notify auth providers on logout
2 parents 1f080a6 + bc8a5c0 commit b19d9e2

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

docs/password_auth_providers.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,13 @@ Password auth provider classes may optionally provide the following methods.
8787

8888
The method should return a Twisted ``Deferred`` object, which resolves to
8989
``True`` if authentication is successful, and ``False`` if not.
90+
91+
``someprovider.on_logged_out``\(*user_id*, *device_id*, *access_token*)
92+
93+
This method, if implemented, is called when a user logs out. It is passed
94+
the qualified user ID, the ID of the deactivated device (if any: access
95+
tokens are occasionally created without an associated device ID), and the
96+
(now deactivated) access token.
97+
98+
It may return a Twisted ``Deferred`` object; the logout request will wait
99+
for the deferred to complete but the result is ignored.

synapse/handlers/auth.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ def deactivate_account(self, user_id):
687687
yield self.store.user_delete_threepids(user_id)
688688
yield self.store.user_set_password_hash(user_id, None)
689689

690+
@defer.inlineCallbacks
690691
def delete_access_token(self, access_token):
691692
"""Invalidate a single access token
692693
@@ -696,8 +697,19 @@ def delete_access_token(self, access_token):
696697
Returns:
697698
Deferred
698699
"""
699-
return self.store.delete_access_token(access_token)
700+
user_info = yield self.auth.get_user_by_access_token(access_token)
701+
yield self.store.delete_access_token(access_token)
702+
703+
# see if any of our auth providers want to know about this
704+
for provider in self.password_providers:
705+
if hasattr(provider, "on_logged_out"):
706+
yield provider.on_logged_out(
707+
user_id=str(user_info["user"]),
708+
device_id=user_info["device_id"],
709+
access_token=access_token,
710+
)
700711

712+
@defer.inlineCallbacks
701713
def delete_access_tokens_for_user(self, user_id, except_token_id=None,
702714
device_id=None):
703715
"""Invalidate access tokens belonging to a user
@@ -712,10 +724,20 @@ def delete_access_tokens_for_user(self, user_id, except_token_id=None,
712724
Returns:
713725
Deferred
714726
"""
715-
return self.store.user_delete_access_tokens(
727+
tokens_and_devices = yield self.store.user_delete_access_tokens(
716728
user_id, except_token_id=except_token_id, device_id=device_id,
717729
)
718730

731+
# see if any of our auth providers want to know about this
732+
for provider in self.password_providers:
733+
if hasattr(provider, "on_logged_out"):
734+
for token, device_id in tokens_and_devices:
735+
yield provider.on_logged_out(
736+
user_id=user_id,
737+
device_id=device_id,
738+
access_token=token,
739+
)
740+
719741
@defer.inlineCallbacks
720742
def add_threepid(self, user_id, medium, address, validated_at):
721743
# 'Canonicalise' email addresses down to lower case.

synapse/storage/registration.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ def user_delete_access_tokens(self, user_id, except_token_id=None,
255255
If None, tokens associated with any device (or no device) will
256256
be deleted
257257
Returns:
258-
defer.Deferred:
258+
defer.Deferred[list[str, str|None]]: a list of the deleted tokens
259+
and device IDs
259260
"""
260261
def f(txn):
261262
keyvalues = {
@@ -272,21 +273,23 @@ def f(txn):
272273
values.append(except_token_id)
273274

274275
txn.execute(
275-
"SELECT token FROM access_tokens WHERE %s" % where_clause,
276+
"SELECT token, device_id FROM access_tokens WHERE %s" % where_clause,
276277
values
277278
)
278-
rows = self.cursor_to_dict(txn)
279+
tokens_and_devices = [(r[0], r[1]) for r in txn]
279280

280-
for row in rows:
281+
for token, _ in tokens_and_devices:
281282
self._invalidate_cache_and_stream(
282-
txn, self.get_user_by_access_token, (row["token"],)
283+
txn, self.get_user_by_access_token, (token,)
283284
)
284285

285286
txn.execute(
286287
"DELETE FROM access_tokens WHERE %s" % where_clause,
287288
values
288289
)
289290

291+
return tokens_and_devices
292+
290293
yield self.runInteraction(
291294
"user_delete_access_tokens", f,
292295
)

0 commit comments

Comments
 (0)