Skip to content

Commit 31d51be

Browse files
committed
🐛(auth) allow several auth backend on m2m API
The previous `ServerToServerAuthentication` was raising authentication failed error if anything is wrong (the header, the token) which prevents any possibility to have several authentication backends.
1 parent 30e7dd0 commit 31d51be

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/backend/core/api/viewsets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ def trashbin(self, request, *args, **kwargs):
689689
authentication_classes=[authentication.ServerToServerAuthentication],
690690
detail=False,
691691
methods=["post"],
692-
permission_classes=[],
692+
permission_classes=[permissions.IsAuthenticated],
693693
url_path="create-for-owner",
694694
)
695695
@transaction.atomic

src/backend/core/authentication/__init__.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
from rest_framework.exceptions import AuthenticationFailed
77

88

9+
class AuthenticatedServer:
10+
"""
11+
Simple class to represent an authenticated server to be used along the
12+
IsAuthenticated permission.
13+
"""
14+
15+
is_authenticated = True
16+
17+
918
class ServerToServerAuthentication(BaseAuthentication):
1019
"""
1120
Custom authentication class for server-to-server requests.
@@ -39,13 +48,16 @@ def authenticate(self, request):
3948
# Validate token format and existence
4049
auth_parts = auth_header.split(" ")
4150
if len(auth_parts) != 2 or auth_parts[0] != self.TOKEN_TYPE:
42-
raise AuthenticationFailed("Invalid authorization header.")
51+
# Do not raise here to leave the door open for other authentication methods
52+
return None
4353

4454
token = auth_parts[1]
4555
if token not in settings.SERVER_TO_SERVER_API_TOKENS:
46-
raise AuthenticationFailed("Invalid server-to-server token.")
56+
# Do not raise here to leave the door open for other authentication methods
57+
return None
4758

48-
# Authentication is successful, but no user is authenticated
59+
# Authentication is successful
60+
return AuthenticatedServer(), token
4961

5062
def authenticate_header(self, request):
5163
"""Return the WWW-Authenticate header value."""

0 commit comments

Comments
 (0)