Skip to content

Commit

Permalink
Fix username autogeneration from email
Browse files Browse the repository at this point in the history
  • Loading branch information
varmar05 committed Jan 13, 2025
1 parent 8f6a611 commit 1b12240
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/mergin/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import datetime
from typing import List, Optional
import bcrypt
import re
from flask import current_app, request
from sqlalchemy import or_, func, text

Expand Down Expand Up @@ -196,6 +197,10 @@ def generate_username(cls, email: str) -> Optional[str]:
if not "@" in email:
return
username = email.split("@")[0].strip().lower()
# remove forbidden chars
username = re.sub(
r"[\@\#\$\%\^\&\*\(\)\{\}\[\]\?\'\"`,;\:\+\=\~\\\/\|\<\>]", "", username
)
# check if we already do not have existing usernames
suffix = db.session.execute(
text(
Expand Down
3 changes: 3 additions & 0 deletions server/mergin/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,9 @@ def test_username_generation(client):
user = add_user("user25", "user")
assert User.generate_username(user.email) == user.username + "1"

# generate username from email containing invalid chars for username, e.g. +
assert User.generate_username("tralala+test@example.com") == "tralalatest"


def test_server_usage(client):
"""Test server usage endpoint"""
Expand Down

0 comments on commit 1b12240

Please sign in to comment.