~mhall119/django-openid-auth/strict-username-requirements

« back to all changes in this revision

Viewing changes to django_openid_auth/views.py

allow a list of permitted external domains to be defined in settings and allow redirects to those external domains as well as local URLs (as before)

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
import re
31
31
import urllib
 
32
from urlparse import urlsplit
32
33
 
33
34
from django.conf import settings
34
35
from django.contrib.auth import (
64
65
    """Sanitise the redirection URL."""
65
66
    # Light security check -- make sure redirect_to isn't garbage.
66
67
    if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
67
 
        redirect_to = settings.LOGIN_REDIRECT_URL
 
68
        # Allow the redirect URL to be external if it's a permitted domain
 
69
        allowed_domains = getattr(settings, 
 
70
            "ALLOWED_EXTERNAL_OPENID_REDIRECT_DOMAINS", [])
 
71
        s, netloc, p, q, f = urlsplit(redirect_to)
 
72
        # allow it if netloc is blank or if the domain is allowed
 
73
        if netloc:
 
74
            # a domain was specified. Is it an allowed domain?
 
75
            if netloc.find(":") != -1:
 
76
                netloc, _ = netloc.split(":", 1)
 
77
            if netloc not in allowed_domains:
 
78
                redirect_to = settings.LOGIN_REDIRECT_URL
 
79
        else:
 
80
            # netloc is blank, so it's a local URL (possibly with another URL
 
81
            # passed in the querystring. Allow it.)
 
82
            pass
68
83
    return redirect_to
69
84
 
70
85