2022-05-07 08:20:12 +00:00
|
|
|
|
import base64
|
|
|
|
|
|
2023-05-12 09:22:18 +00:00
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.contrib.auth import logout as auth_logout
|
|
|
|
|
from django.http import HttpResponse
|
2022-08-09 03:24:28 +00:00
|
|
|
|
from django.shortcuts import redirect, reverse, render
|
2022-04-15 13:33:15 +00:00
|
|
|
|
from django.utils.deprecation import MiddlewareMixin
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from django.utils.translation import gettext as _
|
2021-08-26 07:01:43 +00:00
|
|
|
|
|
2022-08-09 03:24:28 +00:00
|
|
|
|
from apps.authentication import mixins
|
2023-10-16 03:28:53 +00:00
|
|
|
|
from audits.signal_handlers import send_login_info_to_reviewers
|
2023-05-12 09:22:18 +00:00
|
|
|
|
from authentication.signals import post_auth_failed
|
2022-05-07 08:20:12 +00:00
|
|
|
|
from common.utils import gen_key_pair
|
2022-08-09 03:24:28 +00:00
|
|
|
|
from common.utils import get_request_ip
|
2022-05-07 08:20:12 +00:00
|
|
|
|
|
2021-08-26 07:01:43 +00:00
|
|
|
|
|
|
|
|
|
class MFAMiddleware:
|
2021-11-25 05:20:13 +00:00
|
|
|
|
"""
|
|
|
|
|
这个 中间件 是用来全局拦截开启了 MFA 却没有认证的,如 OIDC, CAS,使用第三方库做的登录,直接 login 了,
|
|
|
|
|
所以只能在 Middleware 中控制
|
|
|
|
|
"""
|
2022-08-09 03:24:28 +00:00
|
|
|
|
|
2021-08-26 07:01:43 +00:00
|
|
|
|
def __init__(self, get_response):
|
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
|
response = self.get_response(request)
|
2021-11-25 05:20:13 +00:00
|
|
|
|
# 没有校验
|
|
|
|
|
if not request.session.get('auth_mfa_required'):
|
|
|
|
|
return response
|
|
|
|
|
# 没有认证过,证明不是从 第三方 来的
|
|
|
|
|
if request.user.is_anonymous:
|
|
|
|
|
return response
|
2021-11-24 09:25:10 +00:00
|
|
|
|
|
2021-11-25 05:20:13 +00:00
|
|
|
|
# 这个是 mfa 登录页需要的请求, 也得放出来, 用户其实已经在 CAS/OIDC 中完成登录了
|
|
|
|
|
white_urls = [
|
2021-11-25 08:00:09 +00:00
|
|
|
|
'login/mfa', 'mfa/select', 'jsi18n/', '/static/',
|
|
|
|
|
'/profile/otp', '/logout/',
|
2021-11-25 05:20:13 +00:00
|
|
|
|
]
|
2021-11-24 09:25:10 +00:00
|
|
|
|
for url in white_urls:
|
|
|
|
|
if request.path.find(url) > -1:
|
|
|
|
|
return response
|
2021-11-25 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
# 因为使用 CAS/OIDC 登录的,不小心去了别的页面就回不来了
|
|
|
|
|
if request.path.find('users/profile') > -1:
|
|
|
|
|
return HttpResponse('', status=401)
|
|
|
|
|
|
|
|
|
|
url = reverse('authentication:login-mfa') + '?_=middleware'
|
|
|
|
|
return redirect(url)
|
2022-04-15 13:33:15 +00:00
|
|
|
|
|
|
|
|
|
|
2022-08-09 03:24:28 +00:00
|
|
|
|
class ThirdPartyLoginMiddleware(mixins.AuthMixin):
|
|
|
|
|
"""OpenID、CAS、SAML2登录规则设置验证"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, get_response):
|
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
|
response = self.get_response(request)
|
|
|
|
|
# 没有认证过,证明不是从 第三方 来的
|
|
|
|
|
if request.user.is_anonymous:
|
|
|
|
|
return response
|
|
|
|
|
if not request.session.get('auth_third_party_required'):
|
|
|
|
|
return response
|
2023-02-09 11:38:28 +00:00
|
|
|
|
white_urls = [
|
|
|
|
|
'jsi18n/', '/static/',
|
|
|
|
|
'login/guard', 'login/wait-confirm',
|
|
|
|
|
'login-confirm-ticket/status',
|
|
|
|
|
'settings/public/open',
|
|
|
|
|
'core/auth/login', 'core/auth/logout'
|
|
|
|
|
]
|
|
|
|
|
for url in white_urls:
|
|
|
|
|
if request.path.find(url) > -1:
|
|
|
|
|
return response
|
|
|
|
|
|
2022-08-09 03:24:28 +00:00
|
|
|
|
ip = get_request_ip(request)
|
|
|
|
|
try:
|
2022-08-10 03:03:51 +00:00
|
|
|
|
self.request = request
|
2023-05-12 09:22:18 +00:00
|
|
|
|
self._check_third_party_login_acl()
|
2022-08-09 03:24:28 +00:00
|
|
|
|
self._check_login_acl(request.user, ip)
|
|
|
|
|
except Exception as e:
|
2023-05-12 09:22:18 +00:00
|
|
|
|
if getattr(request, 'user_need_delete', False):
|
|
|
|
|
request.user.delete()
|
|
|
|
|
else:
|
|
|
|
|
error_message = getattr(e, 'msg', None)
|
|
|
|
|
error_message = error_message or str(e)
|
|
|
|
|
post_auth_failed.send(
|
|
|
|
|
sender=self.__class__, username=request.user.username,
|
|
|
|
|
request=self.request, reason=error_message
|
|
|
|
|
)
|
2022-08-09 03:24:28 +00:00
|
|
|
|
auth_logout(request)
|
|
|
|
|
context = {
|
|
|
|
|
'title': _('Authentication failed'),
|
|
|
|
|
'message': _('Authentication failed (before login check failed): {}').format(e),
|
|
|
|
|
'interval': 10,
|
2023-10-09 10:55:05 +00:00
|
|
|
|
'redirect_url': reverse('authentication:login') + '?admin=1',
|
2022-08-09 03:24:28 +00:00
|
|
|
|
'auto_redirect': True,
|
|
|
|
|
}
|
2022-08-10 11:30:54 +00:00
|
|
|
|
response = render(request, 'authentication/auth_fail_flash_message_standalone.html', context)
|
2022-08-09 03:24:28 +00:00
|
|
|
|
else:
|
2023-10-18 10:51:28 +00:00
|
|
|
|
if not self.request.session.get('auth_confirm_required'):
|
2022-08-10 11:30:54 +00:00
|
|
|
|
return response
|
2022-08-09 03:24:28 +00:00
|
|
|
|
guard_url = reverse('authentication:login-guard')
|
|
|
|
|
args = request.META.get('QUERY_STRING', '')
|
|
|
|
|
if args:
|
|
|
|
|
guard_url = "%s?%s" % (guard_url, args)
|
|
|
|
|
response = redirect(guard_url)
|
|
|
|
|
finally:
|
2023-10-16 03:28:53 +00:00
|
|
|
|
if request.session.get('can_send_notifications') and \
|
2023-10-19 04:24:56 +00:00
|
|
|
|
self.request.session.get('auth_notice_required'):
|
2023-10-16 03:28:53 +00:00
|
|
|
|
request.session['can_send_notifications'] = False
|
|
|
|
|
user_log_id = self.request.session.get('user_log_id')
|
|
|
|
|
auth_acl_id = self.request.session.get('auth_acl_id')
|
|
|
|
|
send_login_info_to_reviewers(user_log_id, auth_acl_id)
|
2022-08-09 03:24:28 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
2022-04-15 13:33:15 +00:00
|
|
|
|
class SessionCookieMiddleware(MiddlewareMixin):
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2022-05-17 09:02:16 +00:00
|
|
|
|
def set_cookie_public_key(request, response):
|
2022-06-25 02:14:13 +00:00
|
|
|
|
if request.path.startswith('/api'):
|
|
|
|
|
return
|
2022-05-07 08:20:12 +00:00
|
|
|
|
pub_key_name = settings.SESSION_RSA_PUBLIC_KEY_NAME
|
|
|
|
|
public_key = request.session.get(pub_key_name)
|
|
|
|
|
cookie_key = request.COOKIES.get(pub_key_name)
|
|
|
|
|
if public_key and public_key == cookie_key:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
pri_key_name = settings.SESSION_RSA_PRIVATE_KEY_NAME
|
|
|
|
|
private_key, public_key = gen_key_pair()
|
|
|
|
|
public_key_decode = base64.b64encode(public_key.encode()).decode()
|
|
|
|
|
request.session[pub_key_name] = public_key_decode
|
|
|
|
|
request.session[pri_key_name] = private_key
|
|
|
|
|
response.set_cookie(pub_key_name, public_key_decode)
|
|
|
|
|
|
2022-05-17 09:02:16 +00:00
|
|
|
|
@staticmethod
|
2022-05-18 06:42:54 +00:00
|
|
|
|
def set_cookie_session_prefix(request, response):
|
2022-05-17 09:02:16 +00:00
|
|
|
|
key = settings.SESSION_COOKIE_NAME_PREFIX_KEY
|
|
|
|
|
value = settings.SESSION_COOKIE_NAME_PREFIX
|
|
|
|
|
if request.COOKIES.get(key) == value:
|
|
|
|
|
return response
|
|
|
|
|
response.set_cookie(key, value)
|
|
|
|
|
|
2022-05-18 06:42:54 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def set_cookie_session_expire(request, response):
|
|
|
|
|
if not request.session.get('auth_session_expiration_required'):
|
|
|
|
|
return
|
|
|
|
|
value = 'age'
|
|
|
|
|
if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE_FORCE or \
|
|
|
|
|
not request.session.get('auto_login', False):
|
|
|
|
|
value = 'close'
|
|
|
|
|
|
|
|
|
|
age = request.session.get_expiry_age()
|
|
|
|
|
response.set_cookie('jms_session_expire', value, max_age=age)
|
|
|
|
|
request.session.pop('auth_session_expiration_required', None)
|
|
|
|
|
|
2022-05-17 09:02:16 +00:00
|
|
|
|
def process_response(self, request, response: HttpResponse):
|
2022-05-18 06:42:54 +00:00
|
|
|
|
self.set_cookie_session_prefix(request, response)
|
|
|
|
|
self.set_cookie_public_key(request, response)
|
|
|
|
|
self.set_cookie_session_expire(request, response)
|
2022-05-07 08:20:12 +00:00
|
|
|
|
return response
|