Add CORS origin management

This commit is contained in:
R. Miles McCain 2020-04-24 14:07:34 -04:00
parent e693406114
commit 17cdf052d8
No known key found for this signature in database
GPG Key ID: 24F9B6A2588C5408
2 changed files with 18 additions and 1 deletions

View File

@ -8,7 +8,9 @@ from django.utils import timezone
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView, View from django.views.generic import TemplateView, View
from django.core.cache import cache
from ipware import get_client_ip from ipware import get_client_ip
from core.models import Service
from ..tasks import ingress_request from ..tasks import ingress_request
@ -58,8 +60,15 @@ class PixelView(View):
@method_decorator(csrf_exempt, name="dispatch") @method_decorator(csrf_exempt, name="dispatch")
class ScriptView(View): class ScriptView(View):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
service_uuid = self.kwargs.get("service_uuid")
origins = cache.get(f"service_origins_{service_uuid}")
if origins is None:
service = Service.objects.get(uuid=service_uuid)
origins = service.origins
cache.set(f"service_origins_{service_uuid}", origins, timeout=3600)
resp = super().dispatch(request, *args, **kwargs) resp = super().dispatch(request, *args, **kwargs)
resp["Access-Control-Allow-Origin"] = "*" resp["Access-Control-Allow-Origin"] = origins
resp["Access-Control-Allow-Methods"] = "GET,HEAD,OPTIONS,POST" resp["Access-Control-Allow-Methods"] = "GET,HEAD,OPTIONS,POST"
resp[ resp[
"Access-Control-Allow-Headers" "Access-Control-Allow-Headers"

View File

@ -12,6 +12,7 @@ from django.views.generic import (
UpdateView, UpdateView,
) )
from rules.contrib.views import PermissionRequiredMixin from rules.contrib.views import PermissionRequiredMixin
from django.core.cache import cache
from analytics.models import Session from analytics.models import Session
from core.models import Service from core.models import Service
@ -77,6 +78,13 @@ class ServiceUpdateView(
def get_success_url(self): def get_success_url(self):
return reverse("dashboard:service", kwargs={"pk": self.object.uuid}) return reverse("dashboard:service", kwargs={"pk": self.object.uuid})
def form_valid(self, *args, **kwargs):
resp = super().form_valid(*args, **kwargs)
cache.set(
f"service_origins_{self.object.uuid}", self.object.origins, timeout=3600
)
return resp
class ServiceDeleteView( class ServiceDeleteView(
LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, DeleteView LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, DeleteView