From 96323c58293fb55cefd36ff924219ee1ab63f85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=8D?= <79096808+0321minji@users.noreply.github.com> Date: Thu, 22 Aug 2024 04:13:46 +0900 Subject: [PATCH] Fix/1/super user auth (#91) * #1 fix : modify super user's password encryption * #1 fix : super user password verification --- Cognisle/settings/development.py | 5 +++++ users/auth_backends.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 users/auth_backends.py diff --git a/Cognisle/settings/development.py b/Cognisle/settings/development.py index c5d68b4..a56b3d5 100644 --- a/Cognisle/settings/development.py +++ b/Cognisle/settings/development.py @@ -170,6 +170,11 @@ 'django.contrib.auth.hashers.ScryptPasswordHasher', ] +AUTHENTICATION_BACKENDS = [ + 'users.auth_backends.BcryptSuperuserBackend', # 커스터마이즈된 인증 백엔드 + 'django.contrib.auth.backends.ModelBackend', # 기본 백엔드 +] + # Internationalization # https://docs.djangoproject.com/en/4.0/topics/i18n/ diff --git a/users/auth_backends.py b/users/auth_backends.py new file mode 100644 index 0000000..bff8a63 --- /dev/null +++ b/users/auth_backends.py @@ -0,0 +1,24 @@ +# users/auth_backends.py + +from django.contrib.auth.backends import BaseBackend +from django.contrib.auth import get_user_model +import bcrypt + +User = get_user_model() + +class BcryptSuperuserBackend(BaseBackend): + def authenticate(self, request, username=None, password=None, **kwargs): + try: + user = User.objects.get(email=username) + # 비밀번호가 맞는지 bcrypt로 검증 + print(password,user.password) + if bcrypt.checkpw(password.encode('utf-8'), user.password.encode('utf-8')): + return user + except User.DoesNotExist: + return None + + def get_user(self, user_id): + try: + return User.objects.get(pk=user_id) + except User.DoesNotExist: + return None