-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmiddleware.py
More file actions
71 lines (56 loc) · 2.36 KB
/
middleware.py
File metadata and controls
71 lines (56 loc) · 2.36 KB
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
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
# Copyright 2017, Digital Reasoning
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import unicode_literals
import logging
from django.conf import settings
from django.http import HttpResponseRedirect
logger = logging.getLogger(__name__)
class LoginRedirectMiddleware(object):
"""
Redirect any requests that aren't authenticated to the login URL
"""
def process_request(self, request):
assert hasattr(request, 'user'), (
"The login redirect middleware requires authentication middleware "
"to be installed. Edit your MIDDLEWARE_CLASSES setting to insert "
"'django.contrib.auth.middleware.AuthenticationMiddleware' before "
"'stackdio.core.middleware.LoginRedirectMiddleware'."
)
if not self.should_allow(request) and not self.allow_from_user_agent(request):
# Request not allowed - redirect to login
return HttpResponseRedirect(self.get_redirect_url(request))
@staticmethod
def allow_from_user_agent(request):
user_agent = request.META.get('HTTP_USER_AGENT', '')
if user_agent in settings.USER_AGENT_WHITELIST:
request.META['ALLOWED_FROM_USER_AGENT'] = True
return True
else:
return False
def should_allow(self, request):
user = request.user
path = request.get_full_path()
return path.startswith(settings.LOGIN_URL) \
or path.startswith(settings.STATIC_URL) \
or self.is_api_endpoint(path) \
or user.is_authenticated()
def is_api_endpoint(self, path):
return path.startswith('/api/')
def get_redirect_url(self, request):
redirect_url = settings.LOGIN_URL
if request.path != '/':
redirect_url = '{0}?next={1}'.format(redirect_url, request.path)
return redirect_url