Skip to content

Commit

Permalink
Merge pull request #905 from weni-ai/fix/commerce-password
Browse files Browse the repository at this point in the history
Fix: enhance password generation for Keycloak user creation
  • Loading branch information
ericosta-dev authored Dec 5, 2024
2 parents 0412120 + 9352c09 commit 0cc6da6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
6 changes: 2 additions & 4 deletions connect/api/v2/commerce/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ def create(self, validated_data):
)
create_keycloak_user_use_case = CreateKeycloakUserUseCase(user_dto)
user_info = create_keycloak_user_use_case.execute()
except Exception:
raise serializers.ValidationError(
{"user_email": "User already exists in Keycloak"}
)
except Exception as e:
raise serializers.ValidationError({"keycloak_error": str(e)})

# Create organization
organization = Organization.objects.create(
Expand Down
19 changes: 18 additions & 1 deletion connect/usecases/users/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,24 @@ class CreateKeycloakUserUseCase:
user_dto: KeycloakUserDTO

def generate_password(self) -> str:
return "".join(random.choices(string.ascii_letters + string.digits, k=10))
uppercase = string.ascii_uppercase
lowercase = string.ascii_lowercase
digits = string.digits
special = "!@#$%^&*()_+-=[]{}|;:,.<>?"

password = [
random.choice(uppercase), # 1 uppercase
random.choice(lowercase), # 1 lowercase
random.choice(digits), # 1 digit
random.choice(special), # 1 special
]

all_chars = uppercase + lowercase + digits + special
password.extend(random.choices(all_chars, k=4))

random.shuffle(password)

return "".join(password)

def execute(self) -> dict:
try:
Expand Down

0 comments on commit 0cc6da6

Please sign in to comment.