Skip to main content

Cross-Domain Media with authentication for Django

Project description

Cross-Domain Media with authentication for Django

The situation: You serve media files from a different domain than your main web application domain (good idea). You want to use nginx's internal redirect (X-Accel-Redirect) to authorize media file delivery.

The problem: You don't have access to the user's session on the media domain and can't authenticate or authorize media access.

The solution: You handle media URLs with an expiring token attached which temporarily authorizes access and can be refreshed via redirects when needed.

HTTP View

Here's how it works in HTTP:

  1. -> GET media.example.org/path/file.pdf
  2. <- 302 www.example.com/path/file.pdf
  3. -> GET www.example.com/path/file.pdf
    • if not authorized <- 403
    • if authorized <- 302 media.example.org/path/file.pdf?token=XYZ
  4. -> GET media.example.org/path/file.pdf?token=XYZ
  5. <- 200 file.pdf
  6. after expiry -> GET media.example.org/path/file.pdf?token=XYZ
  7. See step 2

Use in Django

# Development
MEDIA_URL = '/media/'

# Production

MEDIA_URL = 'https://media.example.org/media/
INTERNAL_MEDIA_PREFIX = '/protected/'
from crossdomainmedia import (
    CrossDomainMediaAuth, CrossDomainMediaMixin
)


class CustomCrossDomainMediaAuth(CrossDomainMediaAuth):
    '''
    Create your own custom CrossDomainMediaAuth class
    and implement at least these methods
    '''
    SITE_URL = 'https://www.example.com'

    def is_media_public(self):
        '''
        Determine if the media described by self.context
        needs authentication/authorization at all
        '''
        return self.context['object'].is_public

    def get_auth_url(self):
        '''
        Give URL path to authenticating view
        for the media described in context
        '''
        obj = self.context['object']
        raise reverse('view-name', kwargs={'pk': obj.pk})

    def get_media_file_path(self):
        '''
        Return the file path relative to MEDIA_ROOT
        '''
        obj = self.context['object']
        return obj.file.name


class CustomDetailView(CrossDomainMediaMixin, DetailView):
    '''
    Add the CrossDomainMediaMixin
    and set your custom media_auth_class
    '''
    media_auth_class = CustomCrossDomainMediaAuth

Some other useful methods

# Get your media URLs with token outside of view
mauth = CustomCrossDomainMediaAuth({'object': obj})
mauth.get_full_media_url(authorized=True)

# Send file via nginx internal redirect response
mauth.send_internal_file()

Nginx config

This is how an Nginx config could look like.

server {
    # Web server with session on domain
    listen              443 ssl http2;
    server_name         www.example.com;
    # ...

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $host;
        # etc...

        proxy_pass wsgi_server;
    }
}

server {
    # Media server with no session on domain

    listen 443 ssl http2;
    server_name media.example.org;
    # ...

    location /media/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $host;
        # etc...

        proxy_pass wsgi_server;
    }

    location /protected {
        internal;

        alias /var/www/media-root;
    }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-crossdomainmedia-0.0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

django_crossdomainmedia-0.0.2-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-crossdomainmedia-0.0.2.tar.gz.

File metadata

  • Download URL: django-crossdomainmedia-0.0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.6

File hashes

Hashes for django-crossdomainmedia-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b8a23b71fd909f6554c9809e62e63d10579c76d7340a46200e7f09d8f5607df6
MD5 dc8930e8b36f2382bc0a7ce71261d8c8
BLAKE2b-256 d96e9525b3cf47f08849af8e7f1610507a610d91fd2e9c4e50722bab4c6b3321

See more details on using hashes here.

File details

Details for the file django_crossdomainmedia-0.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_crossdomainmedia-0.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.6

File hashes

Hashes for django_crossdomainmedia-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 dcb7e66591dd7665f4240b4b72e93f7d8077918b3e11c55ebde483223d677c92
MD5 a166b4ad94131ce6a3a29808c9e0784e
BLAKE2b-256 b29bba138070d901cae3ca05dc7e57d5631b88eb47af0c03318343e1a5f66bfb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page