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

Commit

Permalink
Try again if generated token already exists
Browse files Browse the repository at this point in the history
Signed-off-by: Callum Brown <callum@calcuode.com>
  • Loading branch information
govynnus committed Aug 20, 2021
1 parent 5bfc707 commit b5608c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
29 changes: 28 additions & 1 deletion synapse/rest/admin/registration_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,34 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
400, "expiry_time must not be in the past", Codes.INVALID_PARAM
)

await self.store.create_registration_token(token, uses_allowed, expiry_time)
created = await self.store.create_registration_token(

This comment has been minimized.

Copy link
@anoadragon453

anoadragon453 Aug 20, 2021

Member

Looks functional, though for a slightly simpler implementation, I'd probably move the retry logic into another storage function, say generate_registration_token, and then either return the token or None if generation failed.

token, uses_allowed, expiry_time
)

if "token" not in body:
# The token was generated. If it could not be created because
# that token already exists, then try a few more times before
# reporting a failure.
i = 0
while not created and i < 3:
token = "".join(random.choices(self.allowed_chars, k=length))
created = await self.store.create_registration_token(
token, uses_allowed, expiry_time
)
i += 1
if not created:
raise SynapseError(
500,
"The generated token already exists. Try again with a greater length.",

This comment has been minimized.

Copy link
@anoadragon453

anoadragon453 Aug 20, 2021

Member

I might reword this to "Unable to generate a registration token. Try again with a greater length". Technically we're performing multiple attempts, rather than trying one generation and giving up.

Codes.UNKNOWN,
)

elif not created:
# The token was specified in the request, but it already exists
# so could not be created.
raise SynapseError(
400, f"Token already exists: {token}", Codes.INVALID_PARAM
)

resp = {
"token": token,
Expand Down
9 changes: 5 additions & 4 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ async def get_one_registration_token(self, token: str) -> Optional[Dict[str, Any

async def create_registration_token(
self, token: str, uses_allowed: Optional[int], expiry_time: Optional[int]
) -> None:
) -> bool:
"""Create a new registration token. Used by the admin API.
Args:
Expand All @@ -1354,9 +1354,8 @@ def _create_registration_token_txn(txn):
)

if row is not None:
raise SynapseError(
400, f"Token already exists: {token}", Codes.INVALID_PARAM
)
# Token already exists
return False

self.db_pool.simple_insert_txn(
txn,
Expand All @@ -1370,6 +1369,8 @@ def _create_registration_token_txn(txn):
},
)

return True

return await self.db_pool.runInteraction(
"create_registration_token", _create_registration_token_txn
)
Expand Down

0 comments on commit b5608c3

Please sign in to comment.