5
5
from collections .abc import Callable , Iterable
6
6
from typing import Any , ClassVar
7
7
8
+ import sentry_sdk
8
9
from django .conf import settings
9
10
from django .contrib .auth .models import AnonymousUser
10
11
from django .urls import resolve
@@ -422,7 +423,7 @@ def authenticate_token(self, request: Request, token_str: str) -> tuple[Any, Any
422
423
if application_is_inactive :
423
424
raise AuthenticationFailed ("UserApplication inactive or deleted" )
424
425
425
- if token .organization_id :
426
+ if token .scoping_organization_id :
426
427
# We need to make sure the organization to which the token has access is the same as the one in the URL
427
428
organization = None
428
429
organization_context = organization_service .get_organization_by_id (
@@ -439,29 +440,15 @@ def authenticate_token(self, request: Request, token_str: str) -> tuple[Any, Any
439
440
organization .slug != target_org_id_or_slug
440
441
and organization .id != target_org_id_or_slug
441
442
):
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." )
451
444
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."
458
448
)
459
449
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"
465
452
)
466
453
467
454
return self .transform_auth (
0 commit comments