Skip to content

Commit c052cb1

Browse files
committed
ref: store result of rate check in variable for readability
1 parent 8a7aaf6 commit c052cb1

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/sentry/api/authentication.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,11 @@ def _find_or_update_token_by_hash(self, token_str: str) -> ApiToken | ApiTokenRe
342342

343343
rate = options.get("apitoken.use-and-update-hash-rate")
344344
random_rate = random.random()
345+
use_hashed_token = rate > random_rate
345346

346347
if SiloMode.get_current_mode() == SiloMode.REGION:
347348
try:
348-
if rate > random_rate:
349+
if use_hashed_token:
349350
# Try to find the token by its hashed value first
350351
return ApiTokenReplica.objects.get(hashed_token=hashed_token)
351352
else:
@@ -360,7 +361,7 @@ def _find_or_update_token_by_hash(self, token_str: str) -> ApiToken | ApiTokenRe
360361
else:
361362
try:
362363
# Try to find the token by its hashed value first
363-
if rate > random_rate:
364+
if use_hashed_token:
364365
return ApiToken.objects.select_related("user", "application").get(
365366
hashed_token=hashed_token
366367
)
@@ -376,7 +377,7 @@ def _find_or_update_token_by_hash(self, token_str: str) -> ApiToken | ApiTokenRe
376377
# If the token does not exist by plaintext either, it is not a valid token
377378
raise AuthenticationFailed("Invalid token")
378379
else:
379-
if rate > random_rate:
380+
if use_hashed_token:
380381
# Update it with the hashed value if found by plaintext
381382
api_token.hashed_token = hashed_token
382383
api_token.save(update_fields=["hashed_token"])

0 commit comments

Comments
 (0)