2016-08-09 09:27:37 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
#
|
2018-04-18 04:48:07 +00:00
|
|
|
import os
|
2018-06-05 09:26:31 +00:00
|
|
|
import re
|
2018-04-18 04:48:07 +00:00
|
|
|
import pyotp
|
2016-12-19 17:19:50 +00:00
|
|
|
import base64
|
2016-08-30 17:00:20 +00:00
|
|
|
import logging
|
2020-06-22 09:04:07 +00:00
|
|
|
import time
|
2016-08-30 17:00:20 +00:00
|
|
|
|
2016-09-13 13:45:10 +00:00
|
|
|
from django.conf import settings
|
2016-09-03 16:51:36 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2016-12-19 17:19:50 +00:00
|
|
|
from django.core.cache import cache
|
2018-11-22 10:02:12 +00:00
|
|
|
from datetime import datetime
|
2016-08-28 15:58:22 +00:00
|
|
|
|
2016-08-31 17:12:02 +00:00
|
|
|
from common.tasks import send_mail_async
|
2020-09-29 07:31:45 +00:00
|
|
|
from common.utils import reverse, get_object_or_none, get_request_ip_or_data, get_request_user_agent
|
2020-03-12 08:24:38 +00:00
|
|
|
from .models import User
|
2016-08-30 17:00:20 +00:00
|
|
|
|
2018-06-05 09:26:31 +00:00
|
|
|
|
2016-08-30 17:00:20 +00:00
|
|
|
logger = logging.getLogger('jumpserver')
|
|
|
|
|
2016-08-28 15:58:22 +00:00
|
|
|
|
2019-05-24 10:12:58 +00:00
|
|
|
def construct_user_created_email_body(user):
|
|
|
|
default_body = _("""
|
2019-10-21 09:02:16 +00:00
|
|
|
<div>
|
|
|
|
<p>Your account has been created successfully</p>
|
|
|
|
<div>
|
|
|
|
Username: %(username)s
|
|
|
|
<br/>
|
|
|
|
Password: <a href="%(rest_password_url)s?token=%(rest_password_token)s">
|
|
|
|
click here to set your password</a>
|
|
|
|
(This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one</a>)
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<p>---</p>
|
2019-05-24 10:12:58 +00:00
|
|
|
<a href="%(login_url)s">Login direct</a>
|
2019-10-21 09:02:16 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-05-24 10:12:58 +00:00
|
|
|
""") % {
|
2018-05-25 10:13:48 +00:00
|
|
|
'username': user.username,
|
2020-06-03 03:06:44 +00:00
|
|
|
'rest_password_url': reverse('authentication:reset-password', external=True),
|
2016-09-01 15:09:58 +00:00
|
|
|
'rest_password_token': user.generate_reset_token(),
|
2020-06-03 03:06:44 +00:00
|
|
|
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
2016-09-01 15:09:58 +00:00
|
|
|
'email': user.email,
|
2019-02-27 12:55:28 +00:00
|
|
|
'login_url': reverse('authentication:login', external=True),
|
2016-09-01 15:09:58 +00:00
|
|
|
}
|
2019-05-24 10:12:58 +00:00
|
|
|
|
|
|
|
if settings.EMAIL_CUSTOM_USER_CREATED_BODY:
|
|
|
|
custom_body = '<p style="text-indent:2em">' + settings.EMAIL_CUSTOM_USER_CREATED_BODY + '</p>'
|
|
|
|
else:
|
|
|
|
custom_body = ''
|
|
|
|
body = custom_body + default_body
|
|
|
|
return body
|
|
|
|
|
|
|
|
|
|
|
|
def send_user_created_mail(user):
|
|
|
|
recipient_list = [user.email]
|
|
|
|
subject = _('Create account successfully')
|
|
|
|
if settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT:
|
|
|
|
subject = settings.EMAIL_CUSTOM_USER_CREATED_SUBJECT
|
|
|
|
|
|
|
|
honorific = '<p>' + _('Hello %(name)s') % {'name': user.name} + ':</p>'
|
|
|
|
if settings.EMAIL_CUSTOM_USER_CREATED_HONORIFIC:
|
|
|
|
honorific = '<p>' + settings.EMAIL_CUSTOM_USER_CREATED_HONORIFIC + ':</p>'
|
|
|
|
|
|
|
|
body = construct_user_created_email_body(user)
|
|
|
|
|
|
|
|
signature = '<p style="float:right">jumpserver</p>'
|
|
|
|
if settings.EMAIL_CUSTOM_USER_CREATED_SIGNATURE:
|
|
|
|
signature = '<p style="float:right">' + settings.EMAIL_CUSTOM_USER_CREATED_SIGNATURE + '</p>'
|
|
|
|
|
|
|
|
message = honorific + body + signature
|
2017-12-12 04:19:45 +00:00
|
|
|
if settings.DEBUG:
|
2018-01-26 08:06:23 +00:00
|
|
|
try:
|
|
|
|
print(message)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2016-09-01 15:09:58 +00:00
|
|
|
|
|
|
|
send_mail_async.delay(subject, message, recipient_list, html_message=message)
|
|
|
|
|
|
|
|
|
|
|
|
def send_reset_password_mail(user):
|
2016-09-03 16:51:36 +00:00
|
|
|
subject = _('Reset password')
|
2016-09-01 15:09:58 +00:00
|
|
|
recipient_list = [user.email]
|
2016-09-03 16:51:36 +00:00
|
|
|
message = _("""
|
|
|
|
Hello %(name)s:
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2016-09-03 16:51:36 +00:00
|
|
|
Please click the link below to reset your password, if not your request, concern your account security
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2016-09-03 16:51:36 +00:00
|
|
|
<a href="%(rest_password_url)s?token=%(rest_password_token)s">Click here reset password</a>
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2016-11-09 11:29:15 +00:00
|
|
|
This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one</a>
|
2016-09-01 15:09:58 +00:00
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2016-09-01 15:09:58 +00:00
|
|
|
---
|
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2016-09-03 16:51:36 +00:00
|
|
|
<a href="%(login_url)s">Login direct</a>
|
2016-09-01 15:09:58 +00:00
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2016-09-03 16:51:36 +00:00
|
|
|
""") % {
|
2016-09-01 15:09:58 +00:00
|
|
|
'name': user.name,
|
2020-06-03 03:06:44 +00:00
|
|
|
'rest_password_url': reverse('authentication:reset-password', external=True),
|
2016-09-01 15:09:58 +00:00
|
|
|
'rest_password_token': user.generate_reset_token(),
|
2020-06-03 03:06:44 +00:00
|
|
|
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
2016-08-31 17:12:02 +00:00
|
|
|
'email': user.email,
|
2019-02-27 12:55:28 +00:00
|
|
|
'login_url': reverse('authentication:login', external=True),
|
2016-08-31 17:12:02 +00:00
|
|
|
}
|
2016-09-13 13:45:10 +00:00
|
|
|
if settings.DEBUG:
|
2018-11-22 10:02:12 +00:00
|
|
|
logger.debug(message)
|
|
|
|
|
|
|
|
send_mail_async.delay(subject, message, recipient_list, html_message=message)
|
|
|
|
|
|
|
|
|
2020-09-29 07:31:45 +00:00
|
|
|
def send_reset_password_success_mail(request, user):
|
|
|
|
subject = _('Reset password success')
|
|
|
|
recipient_list = [user.email]
|
|
|
|
message = _("""
|
|
|
|
|
|
|
|
Hi %(name)s:
|
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
|
|
<br>
|
|
|
|
Your JumpServer password has just been successfully updated.
|
|
|
|
<br>
|
|
|
|
|
|
|
|
<br>
|
|
|
|
If the password update was not initiated by you, your account may have security issues.
|
|
|
|
It is recommended that you log on to the JumpServer immediately and change your password.
|
|
|
|
<br>
|
|
|
|
|
|
|
|
<br>
|
|
|
|
If you have any questions, you can contact the administrator.
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
---
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
IP Address: %(ip_address)s
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
Browser: %(browser)s
|
|
|
|
<br>
|
|
|
|
|
|
|
|
""") % {
|
|
|
|
'name': user.name,
|
|
|
|
'ip_address': get_request_ip_or_data(request),
|
|
|
|
'browser': get_request_user_agent(request),
|
|
|
|
}
|
2020-09-30 08:11:03 +00:00
|
|
|
if settings.DEBUG:
|
|
|
|
logger.debug(message)
|
|
|
|
|
2020-09-29 07:31:45 +00:00
|
|
|
send_mail_async.delay(subject, message, recipient_list, html_message=message)
|
|
|
|
|
|
|
|
|
2018-11-22 10:02:12 +00:00
|
|
|
def send_password_expiration_reminder_mail(user):
|
|
|
|
subject = _('Security notice')
|
|
|
|
recipient_list = [user.email]
|
|
|
|
message = _("""
|
|
|
|
Hello %(name)s:
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2018-11-22 10:02:12 +00:00
|
|
|
Your password will expire in %(date_password_expired)s,
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2018-11-22 10:02:12 +00:00
|
|
|
For your account security, please click on the link below to update your password in time
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2018-11-22 10:02:12 +00:00
|
|
|
<a href="%(update_password_url)s">Click here update password</a>
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2018-11-22 10:02:12 +00:00
|
|
|
If your password has expired, please click
|
|
|
|
<a href="%(forget_password_url)s?email=%(email)s">Password expired</a>
|
|
|
|
to apply for a password reset email.
|
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2018-11-22 10:02:12 +00:00
|
|
|
---
|
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2018-11-22 10:02:12 +00:00
|
|
|
<a href="%(login_url)s">Login direct</a>
|
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2018-11-22 10:02:12 +00:00
|
|
|
""") % {
|
|
|
|
'name': user.name,
|
|
|
|
'date_password_expired': datetime.fromtimestamp(datetime.timestamp(
|
|
|
|
user.date_password_expired)).strftime('%Y-%m-%d %H:%M'),
|
|
|
|
'update_password_url': reverse('users:user-password-update', external=True),
|
2020-06-03 03:06:44 +00:00
|
|
|
'forget_password_url': reverse('authentication:forgot-password', external=True),
|
2018-11-22 10:02:12 +00:00
|
|
|
'email': user.email,
|
2019-02-27 12:55:28 +00:00
|
|
|
'login_url': reverse('authentication:login', external=True),
|
2018-11-22 10:02:12 +00:00
|
|
|
}
|
|
|
|
if settings.DEBUG:
|
2016-09-13 13:45:10 +00:00
|
|
|
logger.debug(message)
|
2016-08-31 17:12:02 +00:00
|
|
|
|
|
|
|
send_mail_async.delay(subject, message, recipient_list, html_message=message)
|
|
|
|
|
2016-08-30 17:00:20 +00:00
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
def send_user_expiration_reminder_mail(user):
|
|
|
|
subject = _('Expiration notice')
|
|
|
|
recipient_list = [user.email]
|
|
|
|
message = _("""
|
|
|
|
Hello %(name)s:
|
|
|
|
<br>
|
|
|
|
Your account will expire in %(date_expired)s,
|
|
|
|
<br>
|
|
|
|
In order not to affect your normal work, please contact the administrator for confirmation.
|
|
|
|
<br>
|
|
|
|
""") % {
|
|
|
|
'name': user.name,
|
|
|
|
'date_expired': datetime.fromtimestamp(datetime.timestamp(
|
|
|
|
user.date_expired)).strftime('%Y-%m-%d %H:%M'),
|
|
|
|
}
|
|
|
|
if settings.DEBUG:
|
|
|
|
logger.debug(message)
|
|
|
|
|
|
|
|
send_mail_async.delay(subject, message, recipient_list, html_message=message)
|
|
|
|
|
|
|
|
|
2016-09-15 08:54:00 +00:00
|
|
|
def send_reset_ssh_key_mail(user):
|
|
|
|
subject = _('SSH Key Reset')
|
|
|
|
recipient_list = [user.email]
|
|
|
|
message = _("""
|
|
|
|
Hello %(name)s:
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2016-09-15 08:54:00 +00:00
|
|
|
Your ssh public key has been reset by site administrator.
|
|
|
|
Please login and reset your ssh public key.
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2016-09-15 08:54:00 +00:00
|
|
|
<a href="%(login_url)s">Login direct</a>
|
|
|
|
|
2019-07-31 08:57:21 +00:00
|
|
|
<br>
|
2016-09-15 08:54:00 +00:00
|
|
|
""") % {
|
|
|
|
'name': user.name,
|
2019-02-27 12:55:28 +00:00
|
|
|
'login_url': reverse('authentication:login', external=True),
|
2016-09-15 08:54:00 +00:00
|
|
|
}
|
|
|
|
if settings.DEBUG:
|
|
|
|
logger.debug(message)
|
|
|
|
|
|
|
|
send_mail_async.delay(subject, message, recipient_list, html_message=message)
|
|
|
|
|
|
|
|
|
2021-02-25 01:19:31 +00:00
|
|
|
def send_reset_mfa_mail(user):
|
|
|
|
subject = _('MFA Reset')
|
|
|
|
recipient_list = [user.email]
|
|
|
|
message = _("""
|
|
|
|
Hello %(name)s:
|
|
|
|
<br>
|
|
|
|
Your MFA has been reset by site administrator.
|
|
|
|
Please login and reset your MFA.
|
|
|
|
<br>
|
|
|
|
<a href="%(login_url)s">Login direct</a>
|
|
|
|
|
|
|
|
<br>
|
|
|
|
""") % {
|
|
|
|
'name': user.name,
|
|
|
|
'login_url': reverse('authentication:login', external=True),
|
|
|
|
}
|
|
|
|
if settings.DEBUG:
|
|
|
|
logger.debug(message)
|
|
|
|
|
|
|
|
send_mail_async.delay(subject, message, recipient_list, html_message=message)
|
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def get_user_or_pre_auth_user(request):
|
2018-04-19 09:20:53 +00:00
|
|
|
user = request.user
|
|
|
|
if user.is_authenticated:
|
|
|
|
return user
|
2020-03-12 08:24:38 +00:00
|
|
|
pre_auth_user_id = request.session.get('user_id')
|
|
|
|
user = None
|
|
|
|
if pre_auth_user_id:
|
|
|
|
user = get_object_or_none(User, pk=pre_auth_user_id)
|
2018-04-18 04:48:07 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def redirect_user_first_login_or_index(request, redirect_field_name):
|
2020-06-03 04:44:45 +00:00
|
|
|
# if request.user.is_first_login:
|
|
|
|
# return reverse('authentication:user-first-login')
|
2019-11-05 10:46:29 +00:00
|
|
|
url_in_post = request.POST.get(redirect_field_name)
|
|
|
|
if url_in_post:
|
|
|
|
return url_in_post
|
|
|
|
url_in_get = request.GET.get(redirect_field_name, reverse('index'))
|
|
|
|
return url_in_get
|
2018-04-18 04:48:07 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def generate_otp_uri(username, otp_secret_key=None, issuer="JumpServer"):
|
|
|
|
if otp_secret_key is None:
|
2018-04-19 03:13:11 +00:00
|
|
|
otp_secret_key = base64.b32encode(os.urandom(10)).decode('utf-8')
|
2018-04-18 04:48:07 +00:00
|
|
|
totp = pyotp.TOTP(otp_secret_key)
|
2018-12-10 04:03:42 +00:00
|
|
|
otp_issuer_name = settings.OTP_ISSUER_NAME or issuer
|
2020-03-12 08:24:38 +00:00
|
|
|
uri = totp.provisioning_uri(name=username, issuer_name=otp_issuer_name)
|
|
|
|
return uri, otp_secret_key
|
2018-04-18 04:48:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_otp_code(otp_secret_key, otp_code):
|
2018-09-03 03:24:25 +00:00
|
|
|
if not otp_secret_key or not otp_code:
|
|
|
|
return False
|
2018-04-18 04:48:07 +00:00
|
|
|
totp = pyotp.TOTP(otp_secret_key)
|
2018-12-17 06:26:00 +00:00
|
|
|
otp_valid_window = settings.OTP_VALID_WINDOW or 0
|
|
|
|
return totp.verify(otp=otp_code, valid_window=otp_valid_window)
|
2018-06-05 09:26:31 +00:00
|
|
|
|
|
|
|
|
2021-07-30 07:19:00 +00:00
|
|
|
def get_password_check_rules(user):
|
2018-06-05 09:26:31 +00:00
|
|
|
check_rules = []
|
2018-11-22 04:27:27 +00:00
|
|
|
for rule in settings.SECURITY_PASSWORD_RULES:
|
|
|
|
key = "id_{}".format(rule.lower())
|
2021-07-30 07:19:00 +00:00
|
|
|
if user.is_org_admin and rule == 'SECURITY_PASSWORD_MIN_LENGTH':
|
|
|
|
rule = 'SECURITY_ADMIN_USER_PASSWORD_MIN_LENGTH'
|
2018-11-22 04:27:27 +00:00
|
|
|
value = getattr(settings, rule)
|
|
|
|
if not value:
|
|
|
|
continue
|
|
|
|
check_rules.append({'key': key, 'value': int(value)})
|
|
|
|
return check_rules
|
2018-06-05 09:26:31 +00:00
|
|
|
|
|
|
|
|
2021-07-30 07:19:00 +00:00
|
|
|
def check_password_rules(password, user):
|
2018-11-22 04:27:27 +00:00
|
|
|
pattern = r"^"
|
|
|
|
if settings.SECURITY_PASSWORD_UPPER_CASE:
|
|
|
|
pattern += '(?=.*[A-Z])'
|
|
|
|
if settings.SECURITY_PASSWORD_LOWER_CASE:
|
|
|
|
pattern += '(?=.*[a-z])'
|
|
|
|
if settings.SECURITY_PASSWORD_NUMBER:
|
|
|
|
pattern += '(?=.*\d)'
|
|
|
|
if settings.SECURITY_PASSWORD_SPECIAL_CHAR:
|
|
|
|
pattern += '(?=.*[`~!@#\$%\^&\*\(\)-=_\+\[\]\{\}\|;:\'\",\.<>\/\?])'
|
|
|
|
pattern += '[a-zA-Z\d`~!@#\$%\^&\*\(\)-=_\+\[\]\{\}\|;:\'\",\.<>\/\?]'
|
2021-07-30 07:19:00 +00:00
|
|
|
if user.is_org_admin:
|
|
|
|
min_length = settings.SECURITY_ADMIN_USER_PASSWORD_MIN_LENGTH
|
|
|
|
else:
|
2021-08-05 02:37:29 +00:00
|
|
|
min_length = settings.SECURITY_PASSWORD_MIN_LENGTH
|
2021-07-30 07:19:00 +00:00
|
|
|
pattern += '.{' + str(min_length-1) + ',}$'
|
2018-06-05 09:26:31 +00:00
|
|
|
match_obj = re.match(pattern, password)
|
|
|
|
return bool(match_obj)
|
2018-07-05 08:23:33 +00:00
|
|
|
|
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
class BlockUtil:
|
|
|
|
BLOCK_KEY_TMPL: str
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
def __init__(self, username):
|
|
|
|
self.block_key = self.BLOCK_KEY_TMPL.format(username)
|
|
|
|
self.key_ttl = int(settings.SECURITY_LOGIN_LIMIT_TIME) * 60
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
def block(self):
|
|
|
|
cache.set(self.block_key, True, self.key_ttl)
|
2018-07-05 08:23:33 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
def is_block(self):
|
|
|
|
return bool(cache.get(self.block_key))
|
2018-07-16 04:13:13 +00:00
|
|
|
|
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
class BlockUtilBase:
|
|
|
|
LIMIT_KEY_TMPL: str
|
|
|
|
BLOCK_KEY_TMPL: str
|
2019-07-30 11:10:06 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
def __init__(self, username, ip):
|
|
|
|
self.username = username
|
|
|
|
self.ip = ip
|
|
|
|
self.limit_key = self.LIMIT_KEY_TMPL.format(username, ip)
|
|
|
|
self.block_key = self.BLOCK_KEY_TMPL.format(username)
|
|
|
|
self.key_ttl = int(settings.SECURITY_LOGIN_LIMIT_TIME) * 60
|
2019-07-30 11:10:06 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
def get_remainder_times(self):
|
|
|
|
times_up = settings.SECURITY_LOGIN_LIMIT_COUNT
|
|
|
|
times_failed = self.get_failed_count()
|
|
|
|
times_remainder = int(times_up) - int(times_failed)
|
|
|
|
return times_remainder
|
2018-07-05 08:23:33 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
def incr_failed_count(self):
|
|
|
|
limit_key = self.limit_key
|
|
|
|
count = cache.get(limit_key, 0)
|
|
|
|
count += 1
|
|
|
|
cache.set(limit_key, count, self.key_ttl)
|
2018-07-05 08:23:33 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
limit_count = settings.SECURITY_LOGIN_LIMIT_COUNT
|
|
|
|
if count >= limit_count:
|
|
|
|
cache.set(self.block_key, True, self.key_ttl)
|
2018-07-05 08:23:33 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
def get_failed_count(self):
|
|
|
|
count = cache.get(self.limit_key, 0)
|
|
|
|
return count
|
2018-07-05 08:23:33 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
def clean_failed_count(self):
|
|
|
|
cache.delete(self.limit_key)
|
|
|
|
cache.delete(self.block_key)
|
2018-07-16 04:13:13 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
@classmethod
|
|
|
|
def unblock_user(cls, username):
|
|
|
|
key_limit = cls.LIMIT_KEY_TMPL.format(username, '*')
|
|
|
|
key_block = cls.BLOCK_KEY_TMPL.format(username)
|
|
|
|
# Redis 尽量不要用通配
|
|
|
|
cache.delete_pattern(key_limit)
|
|
|
|
cache.delete(key_block)
|
2018-07-16 04:13:13 +00:00
|
|
|
|
2021-04-08 04:47:49 +00:00
|
|
|
@classmethod
|
|
|
|
def is_user_block(cls, username):
|
|
|
|
block_key = cls.BLOCK_KEY_TMPL.format(username)
|
|
|
|
return bool(cache.get(block_key))
|
|
|
|
|
|
|
|
def is_block(self):
|
|
|
|
return bool(cache.get(self.block_key))
|
|
|
|
|
|
|
|
|
|
|
|
class LoginBlockUtil(BlockUtilBase):
|
|
|
|
LIMIT_KEY_TMPL = "_LOGIN_LIMIT_{}_{}"
|
|
|
|
BLOCK_KEY_TMPL = "_LOGIN_BLOCK_{}"
|
|
|
|
|
|
|
|
|
|
|
|
class MFABlockUtils(BlockUtilBase):
|
|
|
|
LIMIT_KEY_TMPL = "_MFA_LIMIT_{}_{}"
|
|
|
|
BLOCK_KEY_TMPL = "_MFA_BLOCK_{}"
|
2019-05-24 03:11:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def construct_user_email(username, email):
|
|
|
|
if '@' not in email:
|
|
|
|
if '@' in username:
|
|
|
|
email = username
|
|
|
|
else:
|
|
|
|
email = '{}@{}'.format(username, settings.EMAIL_SUFFIX)
|
|
|
|
return email
|
|
|
|
|
2019-10-18 07:05:45 +00:00
|
|
|
|
|
|
|
def get_current_org_members(exclude=()):
|
|
|
|
from orgs.utils import current_org
|
2020-07-20 02:42:22 +00:00
|
|
|
return current_org.get_members(exclude=exclude)
|
2019-12-16 08:53:29 +00:00
|
|
|
|
|
|
|
|
2020-06-22 09:04:07 +00:00
|
|
|
def is_auth_time_valid(session, key):
|
|
|
|
return True if session.get(key, 0) > time.time() else False
|
|
|
|
|
|
|
|
|
|
|
|
def is_auth_password_time_valid(session):
|
|
|
|
return is_auth_time_valid(session, 'auth_password_expired_at')
|
|
|
|
|
|
|
|
|
|
|
|
def is_auth_otp_time_valid(session):
|
|
|
|
return is_auth_time_valid(session, 'auth_opt_expired_at')
|