Skip to content

Commit 74c4545

Browse files
committed
feat: enhance user authentication with RSA key handling and encrypted data support
1 parent a0118ca commit 74c4545

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

apps/chat/serializers/chat_authentication.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from common.constants.cache_version import Cache_Version
2121
from common.database_model_manage.database_model_manage import DatabaseModelManage
2222
from common.exception.app_exception import NotFound404, AppUnauthorizedFailed
23+
from common.utils.rsa_util import get_key_pair_by_sql
2324

2425

2526
class AnonymousAuthenticationSerializer(serializers.Serializer):
@@ -82,7 +83,8 @@ def profile(self):
8283
'authentication_type': application_access_token.authentication_value.get(
8384
'type', 'password'),
8485
'max_attempts': max_attempts,
85-
'login_value': final_login_value
86+
'login_value': final_login_value,
87+
'rasKey' : get_key_pair_by_sql().get('key')
8688
}
8789
return profile
8890

apps/common/forms/switch_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ def __init__(self, label: str or BaseLabel,
2828
@param props_info:
2929
"""
3030

31-
super().__init__('Switch', label, required, default_value, relation_show_field_dict,
31+
super().__init__('SwitchInput', label, required, default_value, relation_show_field_dict,
3232
{},
3333
TriggerType.OPTION_LIST, attrs, props_info)

apps/system_manage/serializers/system.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
from common.constants.cache_version import Cache_Version
1616
from common.database_model_manage.database_model_manage import DatabaseModelManage
17+
from common.utils.rsa_util import get_key_pair_by_sql
1718
from maxkb import settings
19+
from system_manage.models import SystemSetting
1820

1921

2022
class SettingType(models.CharField):
@@ -38,4 +40,5 @@ def profile():
3840
version = os.environ.get('MAXKB_VERSION')
3941
license_is_valid = DatabaseModelManage.get_model('license_is_valid') or (lambda: False)
4042
return {'version': version, 'edition': settings.edition,
41-
'license_is_valid': license_is_valid() if license_is_valid() is not None else False}
43+
'license_is_valid': license_is_valid() if license_is_valid() is not None else False,
44+
'ras': get_key_pair_by_sql().get('key')}

apps/users/serializers/login.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from common.database_model_manage.database_model_manage import DatabaseModelManage
2323
from common.exception.app_exception import AppApiException
2424
from common.utils.common import password_encrypt, get_random_chars
25+
from common.utils.rsa_util import encrypt, decrypt
2526
from maxkb.const import CONFIG
2627
from users.models import User
2728

@@ -31,6 +32,9 @@ class LoginRequest(serializers.Serializer):
3132
password = serializers.CharField(required=True, max_length=128, label=_("Password"))
3233
captcha = serializers.CharField(required=False, max_length=64, label=_('captcha'), allow_null=True,
3334
allow_blank=True)
35+
encryptedData = serializers.CharField(required=False, label=_('encryptedData'), allow_null=True,
36+
allow_blank=True)
37+
3438

3539

3640
system_version, system_get_key = Cache_Version.SYSTEM.value
@@ -60,6 +64,10 @@ class LoginSerializer(serializers.Serializer):
6064
@staticmethod
6165
def login(instance):
6266
username = instance.get("username", "")
67+
encryptedData = instance.get("encryptedData", "")
68+
if encryptedData:
69+
json_data = json.loads(decrypt(encryptedData))
70+
instance.update(json_data)
6371
try:
6472
LoginRequest(data=instance).is_valid(raise_exception=True)
6573
except Exception as e:
@@ -99,7 +107,7 @@ def login(instance):
99107
if captcha_cache is None or captcha.lower() != captcha_cache:
100108
raise AppApiException(1005, _("Captcha code error or expiration"))
101109

102-
user = QuerySet(User).filter(username=username, password=password).first()
110+
user = QuerySet(User).filter(username=username, password=password_encrypt(password)).first()
103111
if user is None:
104112
record_login_fail(username)
105113
raise AppApiException(500, _('The username or password is incorrect'))

0 commit comments

Comments
 (0)