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

Don't push if an user account has expired #8353

Merged
merged 4 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8353.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Don't push if an user account has expired.
MatMaul marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 1 addition & 5 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,7 @@ async def get_user_by_req(
# Deny the request if the user account has expired.
if self._account_validity.enabled and not allow_expired:
user_id = user.to_string()
expiration_ts = await self.store.get_expiration_ts_for_user(user_id)
if (
expiration_ts is not None
and self.clock.time_msec() >= expiration_ts
):
if await self.store.is_account_expired(user_id, self.clock.time_msec()):
raise AuthError(
403, "User account has expired", errcode=Codes.EXPIRED_ACCOUNT
)
Expand Down
18 changes: 18 additions & 0 deletions synapse/push/pusherpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def __init__(self, hs: "HomeServer"):
self.store = self.hs.get_datastore()
self.clock = self.hs.get_clock()

self._account_validity = hs.config.account_validity

# We shard the handling of push notifications by user ID.
self._pusher_shard_config = hs.config.push.pusher_shard_config
self._instance_name = hs.get_instance_name()
Expand Down Expand Up @@ -202,6 +204,14 @@ async def on_new_notifications(self, max_stream_id: int):
)

for u in users_affected:
# Don't push if the user account has expired
if self._account_validity.enabled:
expired = await self.store.is_account_expired(
u, self.clock.time_msec()
)
if expired:
continue

if u in self.pushers:
for p in self.pushers[u].values():
p.on_new_notifications(max_stream_id)
Expand All @@ -222,6 +232,14 @@ async def on_new_receipts(self, min_stream_id, max_stream_id, affected_room_ids)
)

for u in users_affected:
# Don't push if the user account has expired
if self._account_validity.enabled:
expired = await self.store.is_account_expired(
u, self.clock.time_msec()
)
if expired:
continue

if u in self.pushers:
for p in self.pushers[u].values():
p.on_new_receipts(min_stream_id, max_stream_id)
Expand Down
16 changes: 16 additions & 0 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ async def get_expiration_ts_for_user(self, user_id: str) -> Optional[int]:
desc="get_expiration_ts_for_user",
)

async def is_account_expired(self, user_id: str, current_ts: int) -> bool:
"""
Returns whether an user account is expired.

Args:
user_id: The user's ID
current_ts: The current timestamp

Returns:
Deferred[bool]: whether the user account has expired
MatMaul marked this conversation as resolved.
Show resolved Hide resolved
"""
expiration_ts = await self.get_expiration_ts_for_user(user_id)
if expiration_ts is not None and current_ts >= expiration_ts:
return True
return False
MatMaul marked this conversation as resolved.
Show resolved Hide resolved

async def set_account_validity_for_user(
self,
user_id: str,
Expand Down