-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmiddleware.py
55 lines (39 loc) · 1.77 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
from django.conf import settings
from django.contrib.auth.middleware import AuthenticationMiddleware
import adnpy
class LazyApi(object):
migrations = set()
def __init__(self):
self.enabled_migrations = '&'.join('%s=1' % m for m in self.migrations)
@staticmethod
# stolen from paniolo
def get_adn_api(access_token=None, headers=None):
verify_ssl = True
extra_headers = {
'Host': 'api.%s' % settings.PARENT_HOST,
'X-ADN-Proxied': '1', # we never want to allow our server to make jsonp or CORS requests to the api
}
if headers:
extra_headers.update(headers)
return adnpy.api.build_api(api_root=settings.ALPHA_API_ROOT, access_token=access_token, verify_ssl=verify_ssl,
extra_headers=extra_headers)
def __get__(self, request, obj_type=None):
if not request:
return
if not hasattr(request, '_cached_api'):
# Build an adnpy api object. We really should save this on the request object so we're not recreating this object for every
# api request we make in one alpha request
try:
token = request.session['OMG_NEW_TOKEN_SPOT_omo_oauth2_token']
except:
token = settings.APP_TOKEN
headers = {
'X-ADN-Migration-Overrides': self.enabled_migrations
}
api = self.get_adn_api(access_token=token, headers=headers)
request._cached_api = api
return request._cached_api
class AlphaAuthenticationMiddleware(AuthenticationMiddleware):
def process_request(self, request):
super(AlphaAuthenticationMiddleware, self).process_request(request)
request.__class__.omo_api = LazyApi()