Skip to content

Verify that the request comes from GCP #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions django_cloud_tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

from django.apps import apps
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from gcp_pilot.base import DEFAULT_SERVICE_ACCOUNT
from gcp_pilot.pubsub import Message
from google.auth.transport import requests
from google.oauth2 import id_token

from django_cloud_tasks import exceptions
from django_cloud_tasks.exceptions import TaskNotFound
Expand All @@ -16,8 +21,26 @@
logger = logging.getLogger("django_cloud_tasks")


def verify_oidc_token(request: HttpRequest):
auth_header: str = request.headers.get("Authorization")

if not auth_header:
raise PermissionDenied("No auth header")

auth_type, creds = auth_header.split(" ", 1)
if auth_type.capitalize() != "Bearer":
raise PermissionDenied("Wrong auth_type " + auth_type)

claims = id_token.verify_token(creds, requests.Request())
if claims['email'] != DEFAULT_SERVICE_ACCOUNT:
raise PermissionDenied("Unauthorised user " + claims['user'])


class GoogleCloudTaskView(View):
@method_decorator(csrf_exempt)
def post(self, request, task_name, *args, **kwargs):
verify_oidc_token(request)

try:
task_class = self.get_task(name=task_name)
except TaskNotFound:
Expand Down