#Allow any Hosts / origin on a specific view

13 messages · Page 1 of 1 (latest)

spark swan
#

handle that specific host under your view?

cerulean goblet
spark swan
#

what exactly are you trying to do?

cerulean goblet
#

read my question sir

spark swan
#

I did,

cerulean goblet
#

so: basically

#

I use the global settings for allowed hosts, allowed origins,.. from my global settings.py which is default django.

But I have one view/api-path I want to allow from anywhere.

spark swan
#

you are using CorsHeaders ?

cerulean goblet
#

yes

spark swan
#

inherit cors middleware, overwrite the method that checks if origin is allowed, add your condition for that particular route

#

I think that would work

cerulean goblet
#

pls give me any reference? @spark swan

spark swan
#
# settings.py
CORS_OPTIONAL_ORIGINS = {
  "https://foobar.com": ["path-to-view"]
}


# middleware.py
from django.conf import settings

from corsheaders.middleware import CorsMiddleware as DjangoCorsMiddleware


class CorsMiddleware(DjangoCorsMiddleware):
  def _url_in_optional_whitelist(self, origin: str) -> bool:
    return origin in settings.SCORS_OPTIONAL_ORIGINS

  def origin_found_in_white_lists(self, origin: str, url: SplitResult) -> bool: # overwrite
    if self._url_in_optional_whitelist(origin):
       # check requested path is allowed route
       return condition

    return super().origin_found_in_white_list(origin, url)