Skip to content

Commit 5cbb25f

Browse files
feat(auth): raise exception if token has wrong org scope
1 parent 89d4135 commit 5cbb25f

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

src/sentry/api/authentication.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections.abc import Callable, Iterable
66
from typing import Any, ClassVar
77

8+
import sentry_sdk
89
from django.conf import settings
910
from django.contrib.auth.models import AnonymousUser
1011
from django.urls import resolve
@@ -422,7 +423,7 @@ def authenticate_token(self, request: Request, token_str: str) -> tuple[Any, Any
422423
if application_is_inactive:
423424
raise AuthenticationFailed("UserApplication inactive or deleted")
424425

425-
if token.organization_id:
426+
if token.scoping_organization_id:
426427
# We need to make sure the organization to which the token has access is the same as the one in the URL
427428
organization = None
428429
organization_context = organization_service.get_organization_by_id(
@@ -439,29 +440,15 @@ def authenticate_token(self, request: Request, token_str: str) -> tuple[Any, Any
439440
organization.slug != target_org_id_or_slug
440441
and organization.id != target_org_id_or_slug
441442
):
442-
# TODO (@athena): We want to raise auth excecption here but to be sure
443-
# I soft launch this by only logging the error for now
444-
# raise AuthenticationFailed("Unauthorized organization access")
445-
logger.info(
446-
"Token has access to organization %s but wants to get access to organization %s: %s",
447-
organization.slug,
448-
target_org_id_or_slug,
449-
request.path_info,
450-
)
443+
raise AuthenticationFailed("Unauthorized organization access.")
451444
else:
452-
# TODO (@athena): We want to limit org level token's access to org level endpoints only
453-
# so in the future this will be an auth exception but for now we soft launch by logging an error
454-
logger.info(
455-
"Token has only access to organization %s but is calling an endpoint for multiple organizations: %s",
456-
organization.slug,
457-
request.path_info,
445+
# We want to limit org scoped tokens access to org level endpoints only
446+
raise AuthenticationFailed(
447+
"This token access is limited to organization endpoints."
458448
)
459449
else:
460-
# TODO (@athena): If there is an organization token we should be able to fetch organization context
461-
# Otherwise we should raise an exception
462-
# For now adding logging to investigate if this is a valid case we need to address
463-
logger.info(
464-
"Token has access to an unknown organization: %s", token.organization_id
450+
sentry_sdk.capture_message(
451+
"Could not resolve organization for organization scoped token", level="warning"
465452
)
466453

467454
return self.transform_auth(

0 commit comments

Comments
 (0)