This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try again if generated token already exists
Signed-off-by: Callum Brown <callum@calcuode.com>
- Loading branch information
Showing
2 changed files
with
33 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong. |
||
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.
Sorry, something went wrong.
anoadragon453
Member
|
||
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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 orNone
if generation failed.