2017-01-17 08:34:47 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2022-11-14 09:16:30 +00:00
|
|
|
|
2023-10-07 05:07:47 +00:00
|
|
|
import time
|
|
|
|
|
2017-03-31 15:46:00 +00:00
|
|
|
from django.conf import settings
|
2022-11-14 01:48:55 +00:00
|
|
|
from django.core.cache import cache
|
2022-11-14 09:16:30 +00:00
|
|
|
from django.shortcuts import redirect, reverse
|
|
|
|
from django.urls import reverse_lazy
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2022-11-14 09:16:30 +00:00
|
|
|
from django.views.generic import FormView, RedirectView
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2023-10-07 05:07:47 +00:00
|
|
|
from authentication.errors import IntervalTooShort
|
2024-01-08 09:50:09 +00:00
|
|
|
from authentication.utils import check_user_property_is_correct
|
2024-02-02 08:52:57 +00:00
|
|
|
from common.const.choices import COUNTRY_CALLING_CODES
|
2023-07-24 03:52:25 +00:00
|
|
|
from common.utils import FlashMessageUtil, get_object_or_none, random_string
|
|
|
|
from common.utils.verify_code import SendAndVerifyCodeUtil
|
|
|
|
from users.notifications import ResetPasswordSuccessMsg
|
2022-11-14 09:16:30 +00:00
|
|
|
from ... import forms
|
|
|
|
from ...models import User
|
|
|
|
from ...utils import check_password_rules, get_password_check_rules
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2018-01-29 08:57:50 +00:00
|
|
|
__all__ = [
|
2022-11-14 09:16:30 +00:00
|
|
|
'UserLoginView',
|
|
|
|
'UserResetPasswordView',
|
|
|
|
'UserForgotPasswordView',
|
|
|
|
'UserForgotPasswordPreviewingView',
|
2018-01-29 08:57:50 +00:00
|
|
|
]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2019-02-27 00:45:00 +00:00
|
|
|
class UserLoginView(RedirectView):
|
2019-03-29 07:53:31 +00:00
|
|
|
url = reverse_lazy('authentication:login')
|
|
|
|
query_string = True
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2022-11-14 01:48:55 +00:00
|
|
|
class UserForgotPasswordPreviewingView(FormView):
|
|
|
|
template_name = 'users/forgot_password_previewing.html'
|
|
|
|
form_class = forms.UserForgotPasswordPreviewingForm
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_redirect_url(token):
|
|
|
|
return reverse('authentication:forgot-password') + '?token=%s' % token
|
|
|
|
|
2023-10-07 05:07:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def generate_previewing_token(user):
|
|
|
|
sent_ttl = 60
|
|
|
|
token_sent_at_key = '{}_send_at'.format(user.username)
|
|
|
|
token_sent_at = cache.get(token_sent_at_key, 0)
|
|
|
|
|
|
|
|
if token_sent_at:
|
|
|
|
raise IntervalTooShort(sent_ttl)
|
|
|
|
token = random_string(36)
|
|
|
|
user_info = {'username': user.username, 'phone': user.phone, 'email': user.email}
|
|
|
|
cache.set(token, user_info, 5 * 60)
|
|
|
|
cache.set(token_sent_at_key, time.time(), sent_ttl)
|
|
|
|
return token
|
|
|
|
|
2022-11-14 01:48:55 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
username = form.cleaned_data['username']
|
|
|
|
user = get_object_or_none(User, username=username)
|
|
|
|
if not user:
|
|
|
|
form.add_error('username', _('User does not exist: {}').format(username))
|
|
|
|
return super().form_invalid(form)
|
2022-11-16 12:34:52 +00:00
|
|
|
if settings.ONLY_ALLOW_AUTH_FROM_SOURCE and not user.is_local:
|
|
|
|
error = _('Non-local users can log in only from third-party platforms '
|
|
|
|
'and cannot change their passwords: {}').format(username)
|
|
|
|
form.add_error('username', error)
|
|
|
|
return super().form_invalid(form)
|
2022-11-14 01:48:55 +00:00
|
|
|
|
2023-10-07 05:07:47 +00:00
|
|
|
try:
|
|
|
|
token = self.generate_previewing_token(user)
|
|
|
|
except IntervalTooShort as e:
|
|
|
|
form.add_error('username', e)
|
|
|
|
return super().form_invalid(form)
|
2022-11-14 01:48:55 +00:00
|
|
|
return redirect(self.get_redirect_url(token))
|
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
class UserForgotPasswordView(FormView):
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/forgot_password.html'
|
2019-12-16 08:53:29 +00:00
|
|
|
form_class = forms.UserForgotPasswordForm
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2022-11-14 01:48:55 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
token = self.request.GET.get('token')
|
2022-11-14 09:16:30 +00:00
|
|
|
userinfo = cache.get(token)
|
|
|
|
if not userinfo:
|
2022-11-14 01:48:55 +00:00
|
|
|
return redirect(self.get_redirect_url(return_previewing=True))
|
|
|
|
else:
|
2022-11-14 09:16:30 +00:00
|
|
|
context = self.get_context_data(has_phone=bool(userinfo['phone']))
|
|
|
|
return self.render_to_response(context)
|
2022-11-14 01:48:55 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-11-14 09:16:30 +00:00
|
|
|
def get_validate_backends_context(has_phone):
|
|
|
|
validate_backends = [{'name': _('Email'), 'is_active': True, 'value': 'email'}]
|
2023-10-18 06:10:24 +00:00
|
|
|
if settings.XPACK_LICENSE_IS_VALID:
|
2022-11-14 09:16:30 +00:00
|
|
|
if settings.SMS_ENABLED and has_phone:
|
2022-11-14 01:48:55 +00:00
|
|
|
is_active = True
|
|
|
|
else:
|
|
|
|
is_active = False
|
|
|
|
sms_backend = {'name': _('SMS'), 'is_active': is_active, 'value': 'sms'}
|
|
|
|
validate_backends.append(sms_backend)
|
|
|
|
return {'validate_backends': validate_backends}
|
|
|
|
|
2022-11-14 09:16:30 +00:00
|
|
|
def get_context_data(self, has_phone=False, **kwargs):
|
2022-11-04 05:56:55 +00:00
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
form = context['form']
|
2022-11-14 09:16:30 +00:00
|
|
|
|
2022-11-04 05:56:55 +00:00
|
|
|
cleaned_data = getattr(form, 'cleaned_data', {})
|
|
|
|
for k, v in cleaned_data.items():
|
|
|
|
if v:
|
|
|
|
context[k] = v
|
2024-02-02 08:52:57 +00:00
|
|
|
context['countries'] = COUNTRY_CALLING_CODES
|
2022-11-14 09:16:30 +00:00
|
|
|
context['form_type'] = 'email'
|
|
|
|
context['XPACK_ENABLED'] = settings.XPACK_ENABLED
|
|
|
|
validate_backends = self.get_validate_backends_context(has_phone)
|
2022-11-14 01:48:55 +00:00
|
|
|
context.update(validate_backends)
|
2022-11-04 05:56:55 +00:00
|
|
|
return context
|
|
|
|
|
2021-04-29 09:15:46 +00:00
|
|
|
@staticmethod
|
2022-11-14 01:48:55 +00:00
|
|
|
def get_redirect_url(user=None, return_previewing=False):
|
|
|
|
if not user and return_previewing:
|
|
|
|
return reverse('authentication:forgot-previewing')
|
2022-11-04 05:56:55 +00:00
|
|
|
query_params = '?token=%s' % user.generate_reset_token()
|
|
|
|
reset_password_url = reverse('authentication:reset-password')
|
|
|
|
return reset_password_url + query_params
|
2021-04-29 09:15:46 +00:00
|
|
|
|
2023-10-07 05:07:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def safe_verify_code(token, target, form_type, code):
|
|
|
|
token_verified_key = '{}_verified'.format(token)
|
|
|
|
token_verified_times = cache.get(token_verified_key, 0)
|
|
|
|
|
|
|
|
if token_verified_times >= 3:
|
|
|
|
cache.delete(token)
|
|
|
|
raise ValueError('Verification code has been used more than 3 times, please re-verify')
|
|
|
|
cache.set(token_verified_key, token_verified_times + 1, 5 * 60)
|
|
|
|
sender_util = SendAndVerifyCodeUtil(target, backend=form_type)
|
|
|
|
return sender_util.verify(code)
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
def form_valid(self, form):
|
2022-11-14 01:48:55 +00:00
|
|
|
token = self.request.GET.get('token')
|
2023-10-07 05:07:47 +00:00
|
|
|
user_info = cache.get(token)
|
|
|
|
if not user_info:
|
2022-11-14 09:16:30 +00:00
|
|
|
return redirect(self.get_redirect_url(return_previewing=True))
|
|
|
|
|
2023-10-07 05:07:47 +00:00
|
|
|
username = user_info.get('username')
|
2022-11-04 05:56:55 +00:00
|
|
|
form_type = form.cleaned_data['form_type']
|
2022-11-14 01:48:55 +00:00
|
|
|
target = form.cleaned_data[form_type]
|
2022-11-14 09:16:30 +00:00
|
|
|
code = form.cleaned_data['code']
|
2024-04-09 08:08:47 +00:00
|
|
|
country_code = form.cleaned_data.get('country_code', '')
|
2022-11-14 09:16:30 +00:00
|
|
|
|
2023-09-25 07:54:15 +00:00
|
|
|
query_key = form_type
|
|
|
|
if form_type == 'sms':
|
|
|
|
query_key = 'phone'
|
2024-04-09 08:08:47 +00:00
|
|
|
target = '{}{}'.format(country_code, target)
|
2023-09-25 07:54:15 +00:00
|
|
|
|
2022-11-04 05:56:55 +00:00
|
|
|
try:
|
2023-10-07 05:07:47 +00:00
|
|
|
self.safe_verify_code(token, target, form_type, code)
|
|
|
|
except ValueError as e:
|
|
|
|
return redirect(self.get_redirect_url(return_previewing=True))
|
2022-11-04 05:56:55 +00:00
|
|
|
except Exception as e:
|
|
|
|
form.add_error('code', str(e))
|
|
|
|
return super().form_invalid(form)
|
|
|
|
|
2024-01-08 09:50:09 +00:00
|
|
|
user = check_user_property_is_correct(username, **{query_key: target})
|
2017-01-17 08:34:47 +00:00
|
|
|
if not user:
|
2022-11-14 09:16:30 +00:00
|
|
|
form.add_error('code', _('No user matched'))
|
2022-11-04 05:56:55 +00:00
|
|
|
return super().form_invalid(form)
|
|
|
|
|
|
|
|
return redirect(self.get_redirect_url(user))
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
class UserResetPasswordView(FormView):
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/reset_password.html'
|
2019-12-16 08:53:29 +00:00
|
|
|
form_class = forms.UserTokenResetPasswordForm
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2019-12-16 08:53:29 +00:00
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
errors = kwargs.get('errors')
|
|
|
|
if errors:
|
|
|
|
context['errors'] = errors
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
token = self.request.GET.get('token', '')
|
2019-04-15 06:37:45 +00:00
|
|
|
user = User.validate_reset_password_token(token)
|
2017-01-17 08:34:47 +00:00
|
|
|
if not user:
|
2019-12-16 08:53:29 +00:00
|
|
|
context['errors'] = _('Token invalid or expired')
|
|
|
|
context['token_invalid'] = True
|
2021-07-30 07:19:00 +00:00
|
|
|
else:
|
|
|
|
check_rules = get_password_check_rules(user)
|
|
|
|
context['password_check_rules'] = check_rules
|
2019-12-16 08:53:29 +00:00
|
|
|
return context
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
token = self.request.GET.get('token')
|
2019-04-15 06:37:45 +00:00
|
|
|
user = User.validate_reset_password_token(token)
|
|
|
|
if not user:
|
2020-04-17 03:26:29 +00:00
|
|
|
error = _('Token invalid or expired')
|
|
|
|
form.add_error('new_password', error)
|
|
|
|
return self.form_invalid(form)
|
2019-12-16 08:53:29 +00:00
|
|
|
|
2018-12-27 08:47:40 +00:00
|
|
|
if not user.can_update_password():
|
2020-04-17 15:41:50 +00:00
|
|
|
error = _('User auth from {}, go there change password')
|
|
|
|
form.add_error('new_password', error.format(user.get_source_display()))
|
2020-04-17 03:26:29 +00:00
|
|
|
return self.form_invalid(form)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
password = form.cleaned_data['new_password']
|
2021-08-17 08:01:27 +00:00
|
|
|
is_ok = check_password_rules(password, is_org_admin=user.is_org_admin)
|
2018-06-05 09:26:31 +00:00
|
|
|
if not is_ok:
|
2020-04-17 03:26:29 +00:00
|
|
|
error = _('* Your password does not meet the requirements')
|
|
|
|
form.add_error('new_password', error)
|
|
|
|
return self.form_invalid(form)
|
2018-06-05 09:26:31 +00:00
|
|
|
|
2021-04-28 09:03:20 +00:00
|
|
|
if user.is_history_password(password):
|
|
|
|
limit_count = settings.OLD_PASSWORD_HISTORY_LIMIT_COUNT
|
2022-11-14 09:16:30 +00:00
|
|
|
error = _('* The new password cannot be the last {} passwords').format(
|
|
|
|
limit_count
|
|
|
|
)
|
2021-04-28 09:03:20 +00:00
|
|
|
form.add_error('new_password', error)
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2017-01-17 08:34:47 +00:00
|
|
|
user.reset_password(password)
|
2019-04-15 06:37:45 +00:00
|
|
|
User.expired_reset_password_token(token)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
ResetPasswordSuccessMsg(user, self.request).publish_async()
|
2021-04-29 09:15:46 +00:00
|
|
|
url = self.get_redirect_url()
|
|
|
|
return redirect(url)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_redirect_url():
|
|
|
|
message_data = {
|
|
|
|
'title': _('Reset password success'),
|
|
|
|
'message': _('Reset password success, return to login page'),
|
|
|
|
'redirect_url': reverse('authentication:login'),
|
|
|
|
'auto_redirect': True,
|
|
|
|
}
|
|
|
|
return FlashMessageUtil.gen_message_url(message_data)
|