chore: workspace api token permission check

This commit is contained in:
sangeethailango
2025-12-23 15:57:31 +05:30
parent 0a20a47b9d
commit bc52fa1cc4

View File

@@ -1,13 +1,14 @@
# Django imports
from django.utils import timezone
from django.db.models import Q
from django.urls import resolve
# Third party imports
from rest_framework import authentication
from rest_framework.exceptions import AuthenticationFailed
# Module imports
from plane.db.models import APIToken
from plane.db.models import APIToken, Workspace
class APIKeyAuthentication(authentication.BaseAuthentication):
@@ -22,13 +23,20 @@ class APIKeyAuthentication(authentication.BaseAuthentication):
def get_api_token(self, request):
return request.headers.get(self.auth_header_name)
def validate_api_token(self, token):
def validate_api_token(self, token, workspace_slug):
try:
api_token = APIToken.objects.get(
Q(Q(expired_at__gt=timezone.now()) | Q(expired_at__isnull=True)),
token=token,
is_active=True,
)
if workspace_slug:
workspace = Workspace.objects.get(slug=workspace_slug)
if api_token.workspace_id != workspace.id:
raise AuthenticationFailed("Given API token is not valid")
except APIToken.DoesNotExist:
raise AuthenticationFailed("Given API token is not valid")
@@ -38,10 +46,12 @@ class APIKeyAuthentication(authentication.BaseAuthentication):
return (api_token.user, api_token.token)
def authenticate(self, request):
workspace_slug = resolve(request.path_info).kwargs.get("slug")
token = self.get_api_token(request=request)
if not token:
return None
# Validate the API token
user, token = self.validate_api_token(token)
user, token = self.validate_api_token(token, workspace_slug)
return user, token