Skip to content

Commit ef23437

Browse files
committed
feat(security): Replace random password generation with user_id and secret key combination
In this commit, a feature is added to the security system. The previous method of generating random passwords is replaced with a new method that generates a password based on a combination of the user's ID and the Django secret key. By using a combination of the user's ID and the Django secret key, the user can be reliably authorized without having to replace existing password each time.
1 parent d124fdd commit ef23437

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

django_appwrite/middleware.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import random
2-
import string
31
from django.contrib.auth import authenticate
42
from django.contrib.auth import get_user_model
53
from django.conf import settings
@@ -10,11 +8,6 @@
108
User = get_user_model()
119

1210

13-
def get_random_string(length):
14-
characters = string.ascii_letters + string.digits + string.punctuation
15-
return ''.join(random.choice(characters) for i in range(length))
16-
17-
1811
class AppwriteMiddleware(MiddlewareMixin):
1912
def __init__(self, get_response):
2013
self.get_response = get_response
@@ -61,25 +54,20 @@ def __call__(self, request, *args, **kwargs):
6154

6255
# If the user information was retrieved successfully
6356
if user_info:
57+
email = user_info['email']
58+
password = settings.SECRET_KEY+user_id
6459
# Get the Django user by its email
65-
user = User.objects.filter(username=user_info['email']).first()
66-
67-
# Generate a random password for the user
68-
password = get_random_string(16)
60+
user = User.objects.filter(username=email).first()
6961

7062
# If the user doesn't exist, create it
7163
if not user:
7264
user = User.objects.create_user(
73-
username=user_info['email'],
65+
username=email,
7466
password=password,
75-
email=user_info['email'])
76-
77-
# Set the user's password to the random password and save it
78-
user.set_password(password)
79-
user.save()
67+
email=email)
8068

8169
# Authenticate the user using the email as the username
82-
user = authenticate(request, username=user_info['email'], password=password)
70+
user = authenticate(request, username=email, password=password)
8371

8472
# If the authentication was successful, log the user in
8573
if user:

0 commit comments

Comments
 (0)