Skip to content

Commit 1464771

Browse files
🛠️ apply pre-commit fixes
1 parent 617ac41 commit 1464771

File tree

2 files changed

+16
-50
lines changed

2 files changed

+16
-50
lines changed

src/sentry/web/frontend/oauth_token.py

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
from sentry import options
1818
from sentry.locks import locks
1919
from sentry.models.apiapplication import ApiApplication, ApiApplicationStatus
20-
from sentry.models.apidevicecode import (
21-
DEFAULT_INTERVAL,
22-
ApiDeviceCode,
23-
DeviceCodeStatus,
24-
)
20+
from sentry.models.apidevicecode import DEFAULT_INTERVAL, ApiDeviceCode, DeviceCodeStatus
2521
from sentry.models.apigrant import ApiGrant, ExpiredGrantError, InvalidGrantError
2622
from sentry.models.apitoken import ApiToken
2723
from sentry.ratelimits import backend as ratelimiter
@@ -128,9 +124,7 @@ def post(self, request: Request) -> HttpResponse:
128124

129125
# Validate grant_type first (needed to determine auth requirements)
130126
if not grant_type:
131-
return self.error(
132-
request=request, name="invalid_request", reason="missing grant_type"
133-
)
127+
return self.error(request=request, name="invalid_request", reason="missing grant_type")
134128
if grant_type not in [
135129
GrantTypes.AUTHORIZATION,
136130
GrantTypes.REFRESH,
@@ -139,9 +133,7 @@ def post(self, request: Request) -> HttpResponse:
139133
return self.error(request=request, name="unsupported_grant_type")
140134

141135
# Determine client credentials from header or body (mutually exclusive).
142-
(client_id, client_secret), cred_error = self._extract_basic_auth_credentials(
143-
request
144-
)
136+
(client_id, client_secret), cred_error = self._extract_basic_auth_credentials(request)
145137
if cred_error is not None:
146138
return cred_error
147139

@@ -214,9 +206,7 @@ def post(self, request: Request) -> HttpResponse:
214206
"oauth_token.post.invalid",
215207
sample_rate=1.0,
216208
)
217-
logger.warning(
218-
"Invalid client_id / secret pair", extra={"client_id": client_id}
219-
)
209+
logger.warning("Invalid client_id / secret pair", extra={"client_id": client_id})
220210
return self.error(
221211
request=request,
222212
name="invalid_client",
@@ -242,9 +232,7 @@ def post(self, request: Request) -> HttpResponse:
242232
# Use unguarded_write because deleting the grant triggers SET_NULL on
243233
# SentryAppInstallation.api_grant, which is a cross-model write
244234
with unguarded_write(using=router.db_for_write(ApiGrant)):
245-
ApiGrant.objects.filter(
246-
application=application, code=code
247-
).delete()
235+
ApiGrant.objects.filter(application=application, code=code).delete()
248236
# For device_code, invalidate the device code
249237
elif grant_type == GrantTypes.DEVICE_CODE:
250238
device_code_value = request.POST.get("device_code")
@@ -281,17 +269,11 @@ def post(self, request: Request) -> HttpResponse:
281269
)
282270

283271
if grant_type == GrantTypes.AUTHORIZATION:
284-
token_data = self.get_access_tokens(
285-
request=request, application=application
286-
)
272+
token_data = self.get_access_tokens(request=request, application=application)
287273
elif grant_type == GrantTypes.DEVICE_CODE:
288-
return self.handle_device_code_grant(
289-
request=request, application=application
290-
)
274+
return self.handle_device_code_grant(request=request, application=application)
291275
else:
292-
token_data = self.get_refresh_token(
293-
request=request, application=application
294-
)
276+
token_data = self.get_refresh_token(request=request, application=application)
295277
if "error" in token_data:
296278
return self.error(
297279
request=request,
@@ -356,28 +338,22 @@ def _extract_basic_auth_credentials(
356338
# avoid excessive memory use on decode.
357339
b64 = param.strip()
358340
if len(b64) > MAX_BASIC_AUTH_B64_LEN:
359-
logger.warning(
360-
"Invalid Basic auth header: too long", extra={"client_id": None}
361-
)
341+
logger.warning("Invalid Basic auth header: too long", extra={"client_id": None})
362342
return (None, None), self.error(
363343
request=request,
364344
name="invalid_client",
365345
reason="invalid basic auth (too long)",
366346
status=401,
367347
)
368348
try:
369-
decoded = base64.b64decode(
370-
b64.encode("ascii"), validate=True
371-
).decode("utf-8")
349+
decoded = base64.b64decode(b64.encode("ascii"), validate=True).decode("utf-8")
372350
# format: client_id:client_secret (client_secret may be empty)
373351
if ":" not in decoded:
374352
raise ValueError("missing colon in basic credentials")
375353
client_id, client_secret = decoded.split(":", 1)
376354
return (client_id, client_secret), None
377355
except Exception:
378-
logger.warning(
379-
"Invalid Basic auth header", extra={"client_id": None}
380-
)
356+
logger.warning("Invalid Basic auth header", extra={"client_id": None})
381357
return (None, None), self.error(
382358
request=request,
383359
name="invalid_client",

tests/sentry/web/frontend/test_oauth_token.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ def test_no_get(self) -> None:
2828
def test_missing_grant_type(self) -> None:
2929
self.login_as(self.user)
3030

31-
resp = self.client.post(
32-
self.path, {"client_id": "abcd", "client_secret": "abcd"}
33-
)
31+
resp = self.client.post(self.path, {"client_id": "abcd", "client_secret": "abcd"})
3432

3533
assert resp.status_code == 400
3634
assert json.loads(resp.content) == {"error": "invalid_request"}
@@ -289,9 +287,7 @@ def test_grant_lock(self) -> None:
289287

290288
# Simulate a concurrent request by using an existing grant
291289
# that has its grant lock taken out.
292-
lock = locks.get(
293-
ApiGrant.get_lock_key(self.grant.id), duration=10, name="api_grant"
294-
)
290+
lock = locks.get(ApiGrant.get_lock_key(self.grant.id), duration=10, name="api_grant")
295291
lock.acquire()
296292

297293
# Attempt to create a token with the same grant
@@ -344,9 +340,7 @@ def test_inactive_application_rejects_token_creation(self) -> None:
344340
assert not ApiGrant.objects.filter(id=self.grant.id).exists()
345341

346342
# Verify no token was created
347-
assert not ApiToken.objects.filter(
348-
application=self.application, user=self.user
349-
).exists()
343+
assert not ApiToken.objects.filter(application=self.application, user=self.user).exists()
350344

351345
def test_invalid_redirect_uri(self) -> None:
352346
self.login_as(self.user)
@@ -462,9 +456,7 @@ def test_expires_in_value(self) -> None:
462456
assert expires_in > 0, "expires_in should be positive (seconds until expiry)"
463457
# Allow for a few seconds of test execution time, but should be close to 30 days
464458
expected_seconds = 30 * 24 * 60 * 60 # 2,592,000 seconds
465-
assert expires_in >= expected_seconds - 60, (
466-
"expires_in should be close to 30 days"
467-
)
459+
assert expires_in >= expected_seconds - 60, "expires_in should be close to 30 days"
468460
assert expires_in <= expected_seconds, "expires_in should not exceed 30 days"
469461

470462
def test_valid_params_id_token(self) -> None:
@@ -1371,9 +1363,7 @@ def test_inactive_application_rejects_device_code_grant(self) -> None:
13711363
assert not ApiDeviceCode.objects.filter(id=self.device_code.id).exists()
13721364

13731365
# No token should be created
1374-
assert not ApiToken.objects.filter(
1375-
application=self.application, user=self.user
1376-
).exists()
1366+
assert not ApiToken.objects.filter(application=self.application, user=self.user).exists()
13771367

13781368
def test_public_client_success(self) -> None:
13791369
"""Public clients (without client_secret) should be able to exchange device codes.

0 commit comments

Comments
 (0)