2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-09-09 06:00:50 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2021-11-18 14:20:35 +00:00
|
|
|
from acls.serializers.rules import ip_group_help_text, ip_group_child_validator
|
2021-11-11 11:03:01 +00:00
|
|
|
|
2023-08-15 05:45:44 +00:00
|
|
|
__all__ = [
|
|
|
|
'SecurityPasswordRuleSerializer', 'SecuritySessionSerializer',
|
|
|
|
'SecurityAuthSerializer', 'SecuritySettingSerializer',
|
|
|
|
'SecurityLoginLimitSerializer', 'SecurityBasicSerializer',
|
|
|
|
]
|
|
|
|
|
2021-09-09 06:00:50 +00:00
|
|
|
|
|
|
|
class SecurityPasswordRuleSerializer(serializers.Serializer):
|
2023-08-15 05:45:44 +00:00
|
|
|
SECURITY_PASSWORD_EXPIRATION_TIME = serializers.IntegerField(
|
|
|
|
min_value=1, max_value=99999, required=True,
|
|
|
|
label=_('User password expiration (day)'),
|
|
|
|
help_text=_(
|
|
|
|
'If the user does not update the password during the time, '
|
|
|
|
'the user password will expire failure;The password expiration reminder mail will be '
|
|
|
|
'automatic sent to the user by system within 5 days (daily) before the password expires'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
OLD_PASSWORD_HISTORY_LIMIT_COUNT = serializers.IntegerField(
|
|
|
|
min_value=0, max_value=99999, required=True,
|
|
|
|
label=_('Number of repeated historical passwords'),
|
|
|
|
help_text=_(
|
|
|
|
'Tip: When the user resets the password, it cannot be '
|
|
|
|
'the previous n historical passwords of the user'
|
|
|
|
)
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
SECURITY_PASSWORD_MIN_LENGTH = serializers.IntegerField(
|
|
|
|
min_value=6, max_value=30, required=True,
|
|
|
|
label=_('Password minimum length')
|
|
|
|
)
|
|
|
|
SECURITY_ADMIN_USER_PASSWORD_MIN_LENGTH = serializers.IntegerField(
|
|
|
|
min_value=6, max_value=30, required=True,
|
|
|
|
label=_('Admin user password minimum length')
|
|
|
|
)
|
|
|
|
SECURITY_PASSWORD_UPPER_CASE = serializers.BooleanField(
|
|
|
|
required=False, label=_('Must contain capital')
|
|
|
|
)
|
2021-11-11 11:03:01 +00:00
|
|
|
SECURITY_PASSWORD_LOWER_CASE = serializers.BooleanField(
|
|
|
|
required=False, label=_('Must contain lowercase')
|
|
|
|
)
|
|
|
|
SECURITY_PASSWORD_NUMBER = serializers.BooleanField(
|
|
|
|
required=False, label=_('Must contain numeric')
|
|
|
|
)
|
|
|
|
SECURITY_PASSWORD_SPECIAL_CHAR = serializers.BooleanField(
|
|
|
|
required=False, label=_('Must contain special')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-18 14:20:35 +00:00
|
|
|
login_ip_limit_time_help_text = _(
|
2023-05-30 09:02:16 +00:00
|
|
|
'If the user has failed to log in for a limited number of times, '
|
2021-11-18 14:20:35 +00:00
|
|
|
'no login is allowed during this time interval.'
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
|
|
|
|
|
2023-08-15 05:45:44 +00:00
|
|
|
class SecurityLoginLimitSerializer(serializers.Serializer):
|
2021-09-09 06:00:50 +00:00
|
|
|
SECURITY_LOGIN_LIMIT_COUNT = serializers.IntegerField(
|
|
|
|
min_value=3, max_value=99999,
|
2021-11-18 14:20:35 +00:00
|
|
|
label=_('Limit the number of user login failures')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
SECURITY_LOGIN_LIMIT_TIME = serializers.IntegerField(
|
|
|
|
min_value=5, max_value=99999, required=True,
|
2023-05-30 09:02:16 +00:00
|
|
|
label=_('Block user login interval (minute)'),
|
2021-11-18 14:20:35 +00:00
|
|
|
help_text=login_ip_limit_time_help_text
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
2023-08-15 05:45:44 +00:00
|
|
|
|
2021-11-18 14:20:35 +00:00
|
|
|
SECURITY_LOGIN_IP_LIMIT_COUNT = serializers.IntegerField(
|
|
|
|
min_value=3, max_value=99999,
|
|
|
|
label=_('Limit the number of IP login failures')
|
|
|
|
)
|
|
|
|
SECURITY_LOGIN_IP_LIMIT_TIME = serializers.IntegerField(
|
|
|
|
min_value=5, max_value=99999, required=True,
|
2023-05-30 09:02:16 +00:00
|
|
|
label=_('Block IP login interval (minute)'),
|
2021-11-18 14:20:35 +00:00
|
|
|
help_text=login_ip_limit_time_help_text
|
2021-11-18 14:20:35 +00:00
|
|
|
)
|
|
|
|
SECURITY_LOGIN_IP_WHITE_LIST = serializers.ListField(
|
|
|
|
default=[], label=_('Login IP White List'), allow_empty=True,
|
2021-11-18 14:20:35 +00:00
|
|
|
child=serializers.CharField(max_length=1024, validators=[ip_group_child_validator]),
|
|
|
|
help_text=ip_group_help_text
|
2021-11-18 14:20:35 +00:00
|
|
|
)
|
2021-11-11 11:03:01 +00:00
|
|
|
SECURITY_LOGIN_IP_BLACK_LIST = serializers.ListField(
|
2021-11-18 08:55:23 +00:00
|
|
|
default=[], label=_('Login IP Black List'), allow_empty=True,
|
2021-11-18 14:20:35 +00:00
|
|
|
child=serializers.CharField(max_length=1024, validators=[ip_group_child_validator]),
|
|
|
|
help_text=ip_group_help_text
|
2021-11-11 11:03:01 +00:00
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
USER_LOGIN_SINGLE_MACHINE_ENABLED = serializers.BooleanField(
|
|
|
|
required=False, default=False, label=_("Only single device login"),
|
2023-05-16 07:21:15 +00:00
|
|
|
help_text=_("After the user logs in on the new device, other logged-in devices will automatically log out")
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
ONLY_ALLOW_EXIST_USER_AUTH = serializers.BooleanField(
|
|
|
|
required=False, default=False, label=_("Only exist user login"),
|
2023-07-24 03:52:25 +00:00
|
|
|
help_text=_(
|
2023-08-14 11:40:21 +00:00
|
|
|
"If enabled, non-existent users will not be allowed to log in; if disabled, "
|
|
|
|
"users of other authentication methods except local authentication methods are allowed "
|
|
|
|
"to log in and automatically create users (if the user does not exist)"
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
ONLY_ALLOW_AUTH_FROM_SOURCE = serializers.BooleanField(
|
|
|
|
required=False, default=False, label=_("Only from source login"),
|
2023-07-24 03:52:25 +00:00
|
|
|
help_text=_(
|
2023-08-14 11:40:21 +00:00
|
|
|
"If it is enabled, the user will only authenticate to the source when logging in; "
|
|
|
|
"if it is disabled, the user will authenticate all the enabled authentication methods "
|
|
|
|
"in a certain order when logging in, and as long as one of the authentication methods is successful, "
|
|
|
|
"they can log in directly"
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
2023-08-15 05:45:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SecurityAuthSerializer(serializers.Serializer):
|
|
|
|
SECURITY_MFA_AUTH = serializers.ChoiceField(
|
|
|
|
choices=(
|
|
|
|
[0, _('Not enabled')],
|
|
|
|
[1, _('All users')],
|
|
|
|
[2, _('Only admin users')],
|
|
|
|
),
|
|
|
|
required=False, label=_("Global MFA auth")
|
|
|
|
)
|
|
|
|
SECURITY_MFA_AUTH_ENABLED_FOR_THIRD_PARTY = serializers.BooleanField(
|
|
|
|
required=False, default=True,
|
|
|
|
label=_('Third-party login users perform MFA authentication'),
|
|
|
|
help_text=_('The third-party login modes include OIDC, CAS, and SAML2'),
|
|
|
|
)
|
|
|
|
OTP_ISSUER_NAME = serializers.CharField(
|
|
|
|
required=False, max_length=16, label=_('OTP issuer name'),
|
|
|
|
)
|
|
|
|
OTP_VALID_WINDOW = serializers.IntegerField(
|
|
|
|
min_value=1, max_value=10,
|
|
|
|
label=_("OTP valid window")
|
|
|
|
)
|
2021-09-15 08:22:51 +00:00
|
|
|
SECURITY_MFA_VERIFY_TTL = serializers.IntegerField(
|
2021-10-18 10:41:41 +00:00
|
|
|
min_value=5, max_value=60 * 60 * 10,
|
2023-08-14 11:40:21 +00:00
|
|
|
label=_("MFA verify TTL"),
|
2021-11-11 11:03:01 +00:00
|
|
|
help_text=_(
|
2023-08-14 11:40:21 +00:00
|
|
|
"Unit: second, The verification MFA takes effect only when you view the account password"
|
2021-11-11 11:03:01 +00:00
|
|
|
)
|
2021-10-18 10:41:41 +00:00
|
|
|
)
|
2023-08-15 05:45:44 +00:00
|
|
|
SECURITY_MFA_IN_LOGIN_PAGE = serializers.BooleanField(
|
|
|
|
required=False, default=False,
|
|
|
|
label=_("MFA in login page"),
|
|
|
|
help_text=_("Eu security regulations(GDPR) require MFA to be on the login page")
|
|
|
|
)
|
2023-01-16 11:02:09 +00:00
|
|
|
VERIFY_CODE_TTL = serializers.IntegerField(
|
|
|
|
min_value=5, max_value=60 * 60 * 10,
|
2023-08-14 11:40:21 +00:00
|
|
|
label=_("Verify code TTL (second)"),
|
|
|
|
help_text=_("Reset password and send SMS code expiration time")
|
2023-01-16 11:02:09 +00:00
|
|
|
)
|
2021-10-18 10:41:41 +00:00
|
|
|
SECURITY_LOGIN_CHALLENGE_ENABLED = serializers.BooleanField(
|
|
|
|
required=False, default=False,
|
|
|
|
label=_("Enable Login dynamic code"),
|
|
|
|
help_text=_("The password and additional code are sent to a third party "
|
|
|
|
"authentication system for verification")
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
SECURITY_LOGIN_CAPTCHA_ENABLED = serializers.BooleanField(
|
2021-10-18 10:41:41 +00:00
|
|
|
required=False, default=False, label=_("Enable Login captcha"),
|
|
|
|
help_text=_("Enable captcha to prevent robot authentication")
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
2023-08-15 05:45:44 +00:00
|
|
|
SECURITY_CHECK_DIFFERENT_CITY_LOGIN = serializers.BooleanField(
|
|
|
|
required=False, label=_('Remote Login Protection'),
|
|
|
|
help_text=_(
|
|
|
|
'The system determines whether the login IP address belongs to a common login city. '
|
|
|
|
'If the account is logged in from a common login city, the system sends a remote login reminder'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
SECURITY_UNCOMMON_USERS_TTL = serializers.IntegerField(
|
|
|
|
min_value=30, max_value=99999, required=False,
|
|
|
|
label=_('Unused user timeout (day)'),
|
|
|
|
help_text=_("Detect infrequent users daily and disable them if they exceed the predetermined time limit.")
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
|
2021-10-18 10:41:41 +00:00
|
|
|
def validate(self, attrs):
|
|
|
|
if attrs.get('SECURITY_MFA_AUTH') != 1:
|
|
|
|
attrs['SECURITY_MFA_IN_LOGIN_PAGE'] = False
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
data = super().to_representation(instance)
|
|
|
|
if data['SECURITY_LOGIN_CHALLENGE_ENABLED']:
|
|
|
|
data['SECURITY_MFA_IN_LOGIN_PAGE'] = False
|
|
|
|
data['SECURITY_LOGIN_CAPTCHA_ENABLED'] = False
|
|
|
|
elif data['SECURITY_MFA_IN_LOGIN_PAGE']:
|
|
|
|
data['SECURITY_LOGIN_CAPTCHA_ENABLED'] = False
|
|
|
|
return data
|
|
|
|
|
2021-09-09 06:00:50 +00:00
|
|
|
|
2023-08-15 05:45:44 +00:00
|
|
|
class SecuritySessionSerializer(serializers.Serializer):
|
2021-09-09 06:00:50 +00:00
|
|
|
SECURITY_WATERMARK_ENABLED = serializers.BooleanField(
|
2021-10-20 11:45:37 +00:00
|
|
|
required=True, label=_('Enable watermark'),
|
|
|
|
help_text=_('Enabled, the web session and replay contains watermark information')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
|
|
|
SECURITY_MAX_IDLE_TIME = serializers.IntegerField(
|
|
|
|
min_value=1, max_value=99999, required=False,
|
2023-05-30 09:02:16 +00:00
|
|
|
label=_('Connection max idle time (minute)'),
|
|
|
|
help_text=_('If idle time more than it, disconnect connection.')
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
2023-08-08 10:11:25 +00:00
|
|
|
SECURITY_MAX_SESSION_TIME = serializers.IntegerField(
|
|
|
|
min_value=1, max_value=99999, required=False,
|
|
|
|
label=_('Session max connection time (hour)'),
|
|
|
|
help_text=_('If session connection time more than it, disconnect connection.')
|
|
|
|
)
|
2021-09-09 06:00:50 +00:00
|
|
|
SECURITY_LUNA_REMEMBER_AUTH = serializers.BooleanField(
|
|
|
|
label=_("Remember manual auth")
|
|
|
|
)
|
2023-08-15 05:45:44 +00:00
|
|
|
SECURITY_SESSION_SHARE = serializers.BooleanField(
|
|
|
|
required=True, label=_('Session share'),
|
|
|
|
help_text=_("Enabled, Allows user active session to be shared with other users")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SecurityBasicSerializer(serializers.Serializer):
|
2021-09-09 06:00:50 +00:00
|
|
|
SECURITY_INSECURE_COMMAND = serializers.BooleanField(
|
|
|
|
required=False, label=_('Insecure command alert')
|
|
|
|
)
|
|
|
|
SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER = serializers.CharField(
|
|
|
|
max_length=8192, required=False, allow_blank=True, label=_('Email recipient'),
|
|
|
|
help_text=_('Multiple user using , split')
|
|
|
|
)
|
2023-08-15 05:45:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SecuritySettingSerializer(
|
|
|
|
SecurityPasswordRuleSerializer, SecurityAuthSerializer,
|
|
|
|
SecuritySessionSerializer, SecurityBasicSerializer,
|
|
|
|
SecurityLoginLimitSerializer,
|
|
|
|
):
|
|
|
|
PREFIX_TITLE = _('Security')
|