2019-02-27 00:45:00 +00:00
|
|
|
|
# ~*~ coding: utf-8 ~*~
|
2019-02-27 12:55:28 +00:00
|
|
|
|
#
|
2019-02-27 00:45:00 +00:00
|
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
import os
|
2019-10-30 05:18:11 +00:00
|
|
|
|
import datetime
|
2019-02-27 00:45:00 +00:00
|
|
|
|
from django.core.cache import cache
|
|
|
|
|
from django.contrib.auth import login as auth_login, logout as auth_logout
|
2019-02-27 12:55:28 +00:00
|
|
|
|
from django.http import HttpResponse
|
2019-02-27 00:45:00 +00:00
|
|
|
|
from django.shortcuts import reverse, redirect
|
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
from django.views.decorators.cache import never_cache
|
|
|
|
|
from django.views.decorators.csrf import csrf_protect
|
|
|
|
|
from django.views.decorators.debug import sensitive_post_parameters
|
2019-10-30 05:18:11 +00:00
|
|
|
|
from django.views.generic.base import TemplateView, RedirectView
|
2019-02-27 00:45:00 +00:00
|
|
|
|
from django.views.generic.edit import FormView
|
|
|
|
|
from django.conf import settings
|
2019-11-05 10:46:29 +00:00
|
|
|
|
from django.urls import reverse_lazy
|
2019-02-27 00:45:00 +00:00
|
|
|
|
|
2019-10-30 05:18:11 +00:00
|
|
|
|
from common.utils import get_request_ip, get_object_or_none
|
2019-02-27 12:55:28 +00:00
|
|
|
|
from users.utils import (
|
2020-03-12 08:24:38 +00:00
|
|
|
|
redirect_user_first_login_or_index
|
2019-02-27 12:55:28 +00:00
|
|
|
|
)
|
2020-07-24 07:47:01 +00:00
|
|
|
|
from .. import mixins, errors, utils
|
|
|
|
|
from ..forms import get_user_login_form_cls
|
2019-02-27 00:45:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
2019-11-05 10:46:29 +00:00
|
|
|
|
'UserLoginView', 'UserLogoutView',
|
2019-10-30 05:18:11 +00:00
|
|
|
|
'UserLoginGuardView', 'UserLoginWaitConfirmView',
|
2019-02-27 00:45:00 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(sensitive_post_parameters(), name='dispatch')
|
|
|
|
|
@method_decorator(csrf_protect, name='dispatch')
|
|
|
|
|
@method_decorator(never_cache, name='dispatch')
|
2019-11-05 10:46:29 +00:00
|
|
|
|
class UserLoginView(mixins.AuthMixin, FormView):
|
2019-02-27 00:45:00 +00:00
|
|
|
|
key_prefix_captcha = "_LOGIN_INVALID_{}"
|
2019-11-05 10:46:29 +00:00
|
|
|
|
redirect_field_name = 'next'
|
2019-02-27 00:45:00 +00:00
|
|
|
|
|
|
|
|
|
def get_template_names(self):
|
2019-03-27 04:20:43 +00:00
|
|
|
|
template_name = 'authentication/login.html'
|
2019-02-27 00:45:00 +00:00
|
|
|
|
if not settings.XPACK_ENABLED:
|
|
|
|
|
return template_name
|
|
|
|
|
|
|
|
|
|
from xpack.plugins.license.models import License
|
|
|
|
|
if not License.has_valid_license():
|
|
|
|
|
return template_name
|
|
|
|
|
|
2019-10-25 11:20:28 +00:00
|
|
|
|
template_name = 'authentication/xpack_login.html'
|
2019-02-27 00:45:00 +00:00
|
|
|
|
return template_name
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
def get_redirect_url_if_need(self, request):
|
|
|
|
|
redirect_url = ''
|
|
|
|
|
# show jumpserver login page if request http://{JUMP-SERVER}/?admin=1
|
|
|
|
|
if self.request.GET.get("admin", 0):
|
|
|
|
|
return None
|
|
|
|
|
if settings.AUTH_OPENID:
|
2020-04-26 12:36:17 +00:00
|
|
|
|
redirect_url = reverse(settings.AUTH_OPENID_AUTH_LOGIN_URL_NAME)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
elif settings.AUTH_CAS:
|
|
|
|
|
redirect_url = reverse(settings.CAS_LOGIN_URL_NAME)
|
|
|
|
|
|
|
|
|
|
if redirect_url:
|
|
|
|
|
query_string = request.GET.urlencode()
|
|
|
|
|
redirect_url = "{}?{}".format(redirect_url, query_string)
|
|
|
|
|
return redirect_url
|
|
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
if request.user.is_staff:
|
|
|
|
|
return redirect(redirect_user_first_login_or_index(
|
|
|
|
|
request, self.redirect_field_name)
|
|
|
|
|
)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
redirect_url = self.get_redirect_url_if_need(request)
|
|
|
|
|
if redirect_url:
|
|
|
|
|
return redirect(redirect_url)
|
2019-02-27 00:45:00 +00:00
|
|
|
|
request.session.set_test_cookie()
|
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
if not self.request.session.test_cookie_worked():
|
|
|
|
|
return HttpResponse(_("Please enable cookies and try again."))
|
2019-11-05 10:46:29 +00:00
|
|
|
|
try:
|
|
|
|
|
self.check_user_auth()
|
|
|
|
|
except errors.AuthFailedError as e:
|
|
|
|
|
form.add_error(None, e.msg)
|
|
|
|
|
ip = self.get_request_ip()
|
|
|
|
|
cache.set(self.key_prefix_captcha.format(ip), 1, 3600)
|
2020-07-24 07:47:01 +00:00
|
|
|
|
form_cls = get_user_login_form_cls(captcha=True)
|
|
|
|
|
new_form = form_cls(data=form.data)
|
2019-11-11 02:58:11 +00:00
|
|
|
|
new_form._errors = form.errors
|
|
|
|
|
context = self.get_context_data(form=new_form)
|
2019-11-05 10:46:29 +00:00
|
|
|
|
return self.render_to_response(context)
|
2019-10-30 05:18:11 +00:00
|
|
|
|
return self.redirect_to_guard_view()
|
2019-02-27 00:45:00 +00:00
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
|
def redirect_to_guard_view(self):
|
|
|
|
|
guard_url = reverse('authentication:login-guard')
|
|
|
|
|
args = self.request.META.get('QUERY_STRING', '')
|
2019-11-06 06:40:41 +00:00
|
|
|
|
if args:
|
2019-11-05 10:46:29 +00:00
|
|
|
|
guard_url = "%s?%s" % (guard_url, args)
|
|
|
|
|
return redirect(guard_url)
|
2019-10-25 11:20:28 +00:00
|
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
|
def get_form_class(self):
|
|
|
|
|
ip = get_request_ip(self.request)
|
|
|
|
|
if cache.get(self.key_prefix_captcha.format(ip)):
|
2020-07-24 07:47:01 +00:00
|
|
|
|
return get_user_login_form_cls(captcha=True)
|
2019-02-27 00:45:00 +00:00
|
|
|
|
else:
|
2020-07-24 07:47:01 +00:00
|
|
|
|
return get_user_login_form_cls()
|
2019-02-27 00:45:00 +00:00
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2020-06-30 09:12:38 +00:00
|
|
|
|
# 生成加解密密钥对,public_key传递给前端,private_key存入session中供解密使用
|
2020-07-29 09:46:43 +00:00
|
|
|
|
rsa_private_key = self.request.session.get('rsa_private_key')
|
|
|
|
|
rsa_public_key = self.request.session.get('rsa_public_key')
|
|
|
|
|
if not all((rsa_private_key, rsa_public_key)):
|
|
|
|
|
rsa_private_key, rsa_public_key = utils.gen_key_pair()
|
|
|
|
|
rsa_public_key = rsa_public_key.replace('\n', '\\n')
|
|
|
|
|
self.request.session['rsa_private_key'] = rsa_private_key
|
|
|
|
|
self.request.session['rsa_public_key'] = rsa_public_key
|
|
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
|
context = {
|
|
|
|
|
'demo_mode': os.environ.get("DEMO_MODE"),
|
|
|
|
|
'AUTH_OPENID': settings.AUTH_OPENID,
|
2020-07-29 09:46:43 +00:00
|
|
|
|
'rsa_public_key': rsa_public_key
|
2019-02-27 00:45:00 +00:00
|
|
|
|
}
|
|
|
|
|
kwargs.update(context)
|
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
|
class UserLoginGuardView(mixins.AuthMixin, RedirectView):
|
2019-10-25 11:20:28 +00:00
|
|
|
|
redirect_field_name = 'next'
|
2019-11-05 10:46:29 +00:00
|
|
|
|
login_url = reverse_lazy('authentication:login')
|
|
|
|
|
login_otp_url = reverse_lazy('authentication:login-otp')
|
|
|
|
|
login_confirm_url = reverse_lazy('authentication:login-wait-confirm')
|
|
|
|
|
|
|
|
|
|
def format_redirect_url(self, url):
|
|
|
|
|
args = self.request.META.get('QUERY_STRING', '')
|
|
|
|
|
if args and self.query_string:
|
|
|
|
|
url = "%s?%s" % (url, args)
|
|
|
|
|
return url
|
2019-10-25 11:20:28 +00:00
|
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2019-11-08 12:17:25 +00:00
|
|
|
|
try:
|
|
|
|
|
user = self.check_user_auth_if_need()
|
|
|
|
|
self.check_user_mfa_if_need(user)
|
|
|
|
|
self.check_user_login_confirm_if_need(user)
|
2020-04-20 02:37:07 +00:00
|
|
|
|
except (errors.CredentialError, errors.SessionEmptyError):
|
2019-11-05 10:46:29 +00:00
|
|
|
|
return self.format_redirect_url(self.login_url)
|
2019-11-08 12:17:25 +00:00
|
|
|
|
except errors.MFARequiredError:
|
2019-11-05 10:46:29 +00:00
|
|
|
|
return self.format_redirect_url(self.login_otp_url)
|
2019-11-08 12:17:25 +00:00
|
|
|
|
except errors.LoginConfirmBaseError:
|
|
|
|
|
return self.format_redirect_url(self.login_confirm_url)
|
2020-03-12 08:24:38 +00:00
|
|
|
|
except errors.MFAUnsetError as e:
|
|
|
|
|
return e.url
|
2019-02-27 00:45:00 +00:00
|
|
|
|
else:
|
2020-01-03 07:26:38 +00:00
|
|
|
|
auth_login(self.request, user)
|
|
|
|
|
self.send_auth_signal(success=True, user=user)
|
|
|
|
|
self.clear_auth_mark()
|
2019-11-08 12:17:25 +00:00
|
|
|
|
url = redirect_user_first_login_or_index(
|
|
|
|
|
self.request, self.redirect_field_name
|
2019-02-27 00:45:00 +00:00
|
|
|
|
)
|
2019-11-08 12:17:25 +00:00
|
|
|
|
return url
|
2019-02-27 00:45:00 +00:00
|
|
|
|
|
|
|
|
|
|
2019-10-25 11:20:28 +00:00
|
|
|
|
class UserLoginWaitConfirmView(TemplateView):
|
|
|
|
|
template_name = 'authentication/login_wait_confirm.html'
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2019-11-15 10:55:35 +00:00
|
|
|
|
from tickets.models import Ticket
|
2019-11-07 10:06:58 +00:00
|
|
|
|
ticket_id = self.request.session.get("auth_ticket_id")
|
|
|
|
|
if not ticket_id:
|
|
|
|
|
ticket = None
|
2019-10-30 05:18:11 +00:00
|
|
|
|
else:
|
2019-11-15 10:55:35 +00:00
|
|
|
|
ticket = get_object_or_none(Ticket, pk=ticket_id)
|
2019-10-30 05:18:11 +00:00
|
|
|
|
context = super().get_context_data(**kwargs)
|
2019-11-07 10:06:58 +00:00
|
|
|
|
if ticket:
|
|
|
|
|
timestamp_created = datetime.datetime.timestamp(ticket.date_created)
|
2019-11-15 10:55:35 +00:00
|
|
|
|
ticket_detail_url = reverse('tickets:ticket-detail', kwargs={'pk': ticket_id})
|
2019-10-30 05:18:11 +00:00
|
|
|
|
msg = _("""Wait for <b>{}</b> confirm, You also can copy link to her/him <br/>
|
2019-11-07 10:06:58 +00:00
|
|
|
|
Don't close this page""").format(ticket.assignees_display)
|
2019-10-30 05:18:11 +00:00
|
|
|
|
else:
|
|
|
|
|
timestamp_created = 0
|
2019-11-07 10:06:58 +00:00
|
|
|
|
ticket_detail_url = ''
|
|
|
|
|
msg = _("No ticket found")
|
2019-10-30 05:18:11 +00:00
|
|
|
|
context.update({
|
|
|
|
|
"msg": msg,
|
|
|
|
|
"timestamp": timestamp_created,
|
2019-11-07 10:06:58 +00:00
|
|
|
|
"ticket_detail_url": ticket_detail_url
|
2019-10-30 05:18:11 +00:00
|
|
|
|
})
|
|
|
|
|
return context
|
2019-10-25 11:20:28 +00:00
|
|
|
|
|
|
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
|
@method_decorator(never_cache, name='dispatch')
|
|
|
|
|
class UserLogoutView(TemplateView):
|
|
|
|
|
template_name = 'flash_message_standalone.html'
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def get_backend_logout_url():
|
2020-04-26 12:36:17 +00:00
|
|
|
|
if settings.AUTH_OPENID:
|
|
|
|
|
return settings.AUTH_OPENID_AUTH_LOGOUT_URL_NAME
|
2020-03-19 03:11:18 +00:00
|
|
|
|
# if settings.AUTH_CAS:
|
|
|
|
|
# return settings.CAS_LOGOUT_URL_NAME
|
2020-04-26 12:36:17 +00:00
|
|
|
|
return None
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
|
def get(self, request, *args, **kwargs):
|
2020-03-12 08:24:38 +00:00
|
|
|
|
backend_logout_url = self.get_backend_logout_url()
|
|
|
|
|
if backend_logout_url:
|
|
|
|
|
return redirect(backend_logout_url)
|
2020-04-21 16:22:24 +00:00
|
|
|
|
|
2020-04-26 12:36:17 +00:00
|
|
|
|
auth_logout(request)
|
2019-02-27 00:45:00 +00:00
|
|
|
|
response = super().get(request, *args, **kwargs)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = {
|
|
|
|
|
'title': _('Logout success'),
|
|
|
|
|
'messages': _('Logout success, return login page'),
|
|
|
|
|
'interval': 1,
|
2019-02-27 12:55:28 +00:00
|
|
|
|
'redirect_url': reverse('authentication:login'),
|
2019-02-27 00:45:00 +00:00
|
|
|
|
'auto_redirect': True,
|
|
|
|
|
}
|
|
|
|
|
kwargs.update(context)
|
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|