From 0cd69f701a675aecbf1c5abff1da40bbc1753922 Mon Sep 17 00:00:00 2001 From: Joseph C Wang Date: Sun, 30 Apr 2023 11:39:17 +0800 Subject: [PATCH 1/2] patch to force kyc on login --- core/serializers/auth.py | 17 ++++++++++++++++- exchange/settings/kyc.py | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/serializers/auth.py b/core/serializers/auth.py index 8e31f2e..9fc81a9 100644 --- a/core/serializers/auth.py +++ b/core/serializers/auth.py @@ -6,6 +6,7 @@ from allauth.account.utils import setup_user_email from django.conf import settings from django.contrib.auth import get_user_model +from django.contrib.auth.models import User from django.core.cache import cache from django.db.models import Model from django.db.transaction import atomic @@ -38,6 +39,7 @@ from core.tasks.facade import notify_user_ip_changed from core.utils.auth import RegisterUserCheck from exchange.loggers import DynamicFieldFilter +from exchange.settings.kyc import IS_KYC_REQUIRED from lib.serializers import LANG_FIELD from lib.utils import generate_random_string @@ -272,6 +274,8 @@ def validate(self, attrs): 'token': current_token }) + self.validate_kyc(user) + attrs['user'] = user captcher.set_captcha_passed() @@ -320,6 +324,17 @@ def validate(self, attrs): return attrs + def validate_kyc(self, user: User) -> None: + if not IS_KYC_REQUIRED: + return + if not UserKYC.valid_for_user(user): + msg = _('Unable to log in due to KYC restrictions.') + raise AccountNotActive( + { + 'message': msg, + 'type': 'account_block' + } + ) class RegisterSerializer(BaseRegisterSerializer): first_name = serializers.CharField(required=True) @@ -499,4 +514,4 @@ def get_kyc_enabled(self, obj): return settings.IS_KYC_ENABLED def get_sms_enabled(self, obj): - return settings.IS_SMS_ENABLED \ No newline at end of file + return settings.IS_SMS_ENABLED diff --git a/exchange/settings/kyc.py b/exchange/settings/kyc.py index e16e560..a8f1bb2 100644 --- a/exchange/settings/kyc.py +++ b/exchange/settings/kyc.py @@ -1,6 +1,7 @@ from exchange.settings import env IS_KYC_ENABLED = env('IS_KYC_ENABLED', default=False) +IS_KYC_REQUIRED = env('IS_KYC_REQUIRED', default=False) SUMSUB_SECRET_KEY = env('SUMSUB_SECRET_KEY') SUMSUB_APP_TOKEN = env('SUMSUB_APP_TOKEN') SUMSUM_CALLBACK_VALIDATION_SECRET = env('SUMSUM_CALLBACK_VALIDATION_SECRET') From f0134c9b56d9a636837e9c76fa3e72d0ffa5181f Mon Sep 17 00:00:00 2001 From: Joseph C Wang Date: Mon, 1 May 2023 11:35:40 +0000 Subject: [PATCH 2/2] trigger error for wrong data --- core/serializers/auth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/serializers/auth.py b/core/serializers/auth.py index 9fc81a9..25d03bf 100644 --- a/core/serializers/auth.py +++ b/core/serializers/auth.py @@ -329,10 +329,10 @@ def validate_kyc(self, user: User) -> None: return if not UserKYC.valid_for_user(user): msg = _('Unable to log in due to KYC restrictions.') - raise AccountNotActive( + raise ValidationError( { 'message': msg, - 'type': 'account_block' + 'type': 'wrong_data' } )