diff --git a/synapse/storage/databases/main/registration.py b/synapse/storage/databases/main/registration.py index aa83fcf9e2d5..8a3ab82694bf 100644 --- a/synapse/storage/databases/main/registration.py +++ b/synapse/storage/databases/main/registration.py @@ -1783,7 +1783,10 @@ async def registration_token_is_valid(self, token: str) -> bool: return False # Check if the token has been used up - if res["uses_allowed"] and res["pending"] + res["completed"] >= res["uses_allowed"]: + if ( + res["uses_allowed"] + and res["pending"] + res["completed"] >= res["uses_allowed"] + ): return False # Otherwise, the token is valid @@ -1815,7 +1818,10 @@ def _set_registration_token_pending_txn(txn): ) async def use_registration_token(self, token: str) -> None: - """Increment the completed registrations counter for a token. + """Complete a use of the given registration token. + + The `pending` counter will be decremented, and the `completed` + counter will be incremented. Args: token: The registration token to be 'used' diff --git a/synapse/storage/schema/main/delta/59/999create_registration_tokens.sql b/synapse/storage/schema/main/delta/59/999create_registration_tokens.sql index a9f34c7d6702..ee6cf958f4f3 100644 --- a/synapse/storage/schema/main/delta/59/999create_registration_tokens.sql +++ b/synapse/storage/schema/main/delta/59/999create_registration_tokens.sql @@ -16,8 +16,8 @@ CREATE TABLE IF NOT EXISTS registration_tokens( token TEXT NOT NULL, -- The token that can be used for authentication. uses_allowed INT, -- The total number of times this token can be used. NULL if no limit. - pending INT NOT NULL, -- The number of in progress registrations using this token. - completed INT NOT NULL, -- The number of times this token has been used to complete a registration. + pending INT NOT NULL, -- The number of in progress registrations using this token. + completed INT NOT NULL, -- The number of times this token has been used to complete a registration. expiry_time BIGINT, -- The latest time this token will be valid (epoch time in milliseconds). NULL if token doesn't expire. UNIQUE (token) ); diff --git a/tests/rest/client/v2_alpha/test_register.py b/tests/rest/client/v2_alpha/test_register.py index 2fff268f9e94..5e6a4ed955d3 100644 --- a/tests/rest/client/v2_alpha/test_register.py +++ b/tests/rest/client/v2_alpha/test_register.py @@ -342,7 +342,7 @@ def test_POST_registration_token_limit_uses(self): "session": session1, } self.make_request(b"POST", self.url, json.dumps(params1)) - # Repeat request to make sure pending isn't set increased again + # Repeat request to make sure pending isn't increased again self.make_request(b"POST", self.url, json.dumps(params1)) pending = self.get_success( store.db_pool.simple_select_one_onecol(