Browse Source

Rename TokenAuthentication to UserAuthTokenAuthentication (#57132)

* To not be confused with Django's
[TokenAuthentication](https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication)
(same name)
* To better align with `OrgAuthTokenAuthentication` naming
Alexander Tarasov 1 year ago
parent
commit
57774175d5

+ 1 - 1
src/sentry/api/authentication.py

@@ -271,7 +271,7 @@ class ClientIdSecretAuthentication(QuietBasicAuthentication):
 
 
 @AuthenticationSiloLimit(SiloMode.REGION, SiloMode.CONTROL)
-class TokenAuthentication(StandardAuthentication):
+class UserAuthTokenAuthentication(StandardAuthentication):
     token_name = b"bearer"
 
     def accepts_auth(self, auth: list[bytes]) -> bool:

+ 6 - 2
src/sentry/api/base.py

@@ -44,7 +44,11 @@ from sentry.utils.http import (
 )
 from sentry.utils.sdk import capture_exception, merge_context_into_scope
 
-from .authentication import ApiKeyAuthentication, OrgAuthTokenAuthentication, TokenAuthentication
+from .authentication import (
+    ApiKeyAuthentication,
+    OrgAuthTokenAuthentication,
+    UserAuthTokenAuthentication,
+)
 from .paginator import BadPaginationError, Paginator
 from .permissions import NoPermission
 
@@ -76,7 +80,7 @@ CURSOR_LINK_HEADER = (
 )
 
 DEFAULT_AUTHENTICATION = (
-    TokenAuthentication,
+    UserAuthTokenAuthentication,
     OrgAuthTokenAuthentication,
     ApiKeyAuthentication,
     SessionAuthentication,

+ 1 - 1
src/sentry/apidocs/extensions.py

@@ -13,7 +13,7 @@ class TokenAuthExtension(OpenApiAuthenticationExtension):
     OpenAPI Schema.
     """
 
-    target_class = "sentry.api.authentication.TokenAuthentication"
+    target_class = "sentry.api.authentication.UserAuthTokenAuthentication"
     name = "auth_token"
 
     def get_security_requirement(self, auto_schema: AutoSchema) -> Dict[str, List[Any]]:

+ 1 - 1
src/sentry/conf/server.py

@@ -1335,7 +1335,7 @@ if os.environ.get("OPENAPIGENERATE", False):
         "DISABLE_ERRORS_AND_WARNINGS": False,
         "COMPONENT_SPLIT_REQUEST": False,
         "COMPONENT_SPLIT_PATCH": False,
-        "AUTHENTICATION_WHITELIST": ["sentry.api.authentication.TokenAuthentication"],
+        "AUTHENTICATION_WHITELIST": ["sentry.api.authentication.UserAuthTokenAuthentication"],
         "TAGS": OPENAPI_TAGS,
         "TITLE": "API Reference",
         "DESCRIPTION": "Sentry Public API",

+ 2 - 2
src/sentry/feedback/endpoints/feedback_ingest.py

@@ -15,7 +15,7 @@ from sentry.api.authentication import (
     ApiKeyAuthentication,
     DSNAuthentication,
     OrgAuthTokenAuthentication,
-    TokenAuthentication,
+    UserAuthTokenAuthentication,
 )
 from sentry.api.base import Endpoint, region_silo_endpoint
 from sentry.api.bases.project import ProjectPermission
@@ -96,7 +96,7 @@ class FeedbackIngestEndpoint(Endpoint):
     # Authentication code borrowed from the monitor endpoints (which will eventually be removed)
     authentication_classes = (
         DSNAuthentication,
-        TokenAuthentication,
+        UserAuthTokenAuthentication,
         OrgAuthTokenAuthentication,
         ApiKeyAuthentication,
     )

+ 2 - 2
src/sentry/middleware/auth.py

@@ -14,7 +14,7 @@ from rest_framework.exceptions import AuthenticationFailed
 from sentry.api.authentication import (
     ApiKeyAuthentication,
     OrgAuthTokenAuthentication,
-    TokenAuthentication,
+    UserAuthTokenAuthentication,
 )
 from sentry.models import UserIP
 from sentry.services.hybrid_cloud.auth import auth_service, authentication_request_from
@@ -90,7 +90,7 @@ class RequestAuthenticationMiddleware(MiddlewareMixin):
 
         if auth:
             for authenticator_class in [
-                TokenAuthentication,
+                UserAuthTokenAuthentication,
                 OrgAuthTokenAuthentication,
                 ApiKeyAuthentication,
             ]:

+ 2 - 2
src/sentry/monitors/endpoints/base.py

@@ -8,7 +8,7 @@ from sentry.api.authentication import (
     ApiKeyAuthentication,
     DSNAuthentication,
     OrgAuthTokenAuthentication,
-    TokenAuthentication,
+    UserAuthTokenAuthentication,
 )
 from sentry.api.base import Endpoint
 from sentry.api.bases.organization import OrganizationPermission
@@ -112,7 +112,7 @@ class MonitorIngestEndpoint(Endpoint):
 
     authentication_classes = (
         DSNAuthentication,
-        TokenAuthentication,
+        UserAuthTokenAuthentication,
         OrgAuthTokenAuthentication,
         ApiKeyAuthentication,
     )

+ 13 - 7
src/sentry/services/hybrid_cloud/auth/model.py

@@ -46,7 +46,7 @@ class RpcApiKey(RpcModel):
 
 class RpcAuthenticatorType(IntEnum):
     UNUSUED_ONE = 0
-    TOKEN_AUTHENTICATION = 1
+    USER_AUTH_TOKEN_AUTHENTICATION = 1
     SESSION_AUTHENTICATION = 2
     ORG_AUTH_TOKEN_AUTHENTICATION = 3
 
@@ -54,19 +54,25 @@ class RpcAuthenticatorType(IntEnum):
     def from_authenticator(
         self, auth: Type[BaseAuthentication]
     ) -> Optional["RpcAuthenticatorType"]:
-        from sentry.api.authentication import OrgAuthTokenAuthentication, TokenAuthentication
+        from sentry.api.authentication import (
+            OrgAuthTokenAuthentication,
+            UserAuthTokenAuthentication,
+        )
 
-        if auth == TokenAuthentication:
-            return RpcAuthenticatorType.TOKEN_AUTHENTICATION
+        if auth == UserAuthTokenAuthentication:
+            return RpcAuthenticatorType.USER_AUTH_TOKEN_AUTHENTICATION
         if auth == OrgAuthTokenAuthentication:
             return RpcAuthenticatorType.ORG_AUTH_TOKEN_AUTHENTICATION
         return None
 
     def as_authenticator(self) -> BaseAuthentication:
-        from sentry.api.authentication import OrgAuthTokenAuthentication, TokenAuthentication
+        from sentry.api.authentication import (
+            OrgAuthTokenAuthentication,
+            UserAuthTokenAuthentication,
+        )
 
-        if self == self.TOKEN_AUTHENTICATION:
-            return TokenAuthentication()
+        if self == self.USER_AUTH_TOKEN_AUTHENTICATION:
+            return UserAuthTokenAuthentication()
         if self == self.ORG_AUTH_TOKEN_AUTHENTICATION:
             return OrgAuthTokenAuthentication()
         else:

+ 2 - 2
tests/sentry/api/test_authentication.py

@@ -14,7 +14,7 @@ from sentry.api.authentication import (
     OrgAuthTokenAuthentication,
     RelayAuthentication,
     RpcSignatureAuthentication,
-    TokenAuthentication,
+    UserAuthTokenAuthentication,
 )
 from sentry.models import ProjectKeyStatus, Relay
 from sentry.models.apitoken import ApiToken
@@ -164,7 +164,7 @@ class TestTokenAuthentication(TestCase):
     def setUp(self):
         super().setUp()
 
-        self.auth = TokenAuthentication()
+        self.auth = UserAuthTokenAuthentication()
         self.org = self.create_organization(owner=self.user)
         self.token = "abc123"
         self.api_token = ApiToken.objects.create(