-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.py
62 lines (43 loc) · 1.79 KB
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from google.appengine.api import modules
from . import utils
class GoogleUserMiddleware(object):
"""Sets a user_email attribute on the request object, which is the
currently logged in Google Auth user email (if any).
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
email = utils.get_current_user_email()
request.user_email = email
response = self.get_response(request)
return response
class CSPHostnameMiddleware(object):
"""Rewrites CSP headers set by django-csp, substituting the hostname
from the request.
This lets you have a directive with a path without knowing the server name.
CSP_SCRIPT_SRC = ('{host}/static/app.js',)
N.B. This must come _before_ csp.middleware.CSPMiddleware so that it can
process the response.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
headers = ('Content-Security-Policy', 'Content-Security-Policy-Report-Only')
scheme = 'https://' if request.is_secure() else 'http://'
host = scheme + request.get_host()
for header in headers:
if header in response:
value = response[header]
response[header] = value.format(host=host)
return response
class PastyVersionMiddleware(object):
"""Adds a X-Pasty-Version header to the response."""
key = 'X-Pasty-Version'
version = modules.get_current_version_name()
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
response[self.key] = self.version
return response