Feat: Use only sfw words with a number suffix (#1625)

* Feat: Use only sfw words with a number suffix

* Updated also custom aliases to have a number suffix

* do not use _ as separator

* use _ as separator for words-based suffix

---------

Co-authored-by: Adrià Casajús <adria.casajus@proton.ch>
Co-authored-by: Son <nguyenkims@users.noreply.github.com>
This commit is contained in:
Adrià Casajús 2023-03-13 19:55:16 +01:00 committed by GitHub
parent 432fb3fcf7
commit 66388e72e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7779 additions and 321667 deletions

View File

@ -44,7 +44,6 @@ from app.utils import (
random_string,
random_words,
sanitize_email,
random_word,
)
Base = declarative_base()
@ -1010,7 +1009,7 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle):
"""
if self.random_alias_suffix == AliasSuffixEnum.random_string.value:
return random_string(config.ALIAS_RANDOM_SUFFIX_LENGTH, include_digits=True)
return random_word()
return random_words(1, 3)
def __repr__(self):
return f"<User {self.id} {self.name} {self.email}>"
@ -1269,7 +1268,7 @@ def generate_email(
name = uuid.uuid4().hex if in_hex else uuid.uuid4().__str__()
random_email = name + "@" + alias_domain
else:
random_email = random_words() + "@" + alias_domain
random_email = random_words(2, 3) + "@" + alias_domain
random_email = random_email.lower().strip()

View File

@ -1,3 +1,4 @@
import random
import re
import secrets
import string
@ -25,11 +26,16 @@ def word_exist(word):
return word in _words
def random_words():
def random_words(words: int = 2, numbers: int = 0):
"""Generate a random words. Used to generate user-facing string, for ex email addresses"""
# nb_words = random.randint(2, 3)
nb_words = 2
return "_".join([secrets.choice(_words) for i in range(nb_words)])
fields = [secrets.choice(_words) for i in range(words)]
if numbers > 0:
fields.append("".join([str(random.randint(0, 9)) for i in range(numbers)]))
return "".join(fields)
else:
return "_".join(fields)
def random_string(length=10, include_digits=False):

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,12 @@ from app.utils import random_string, random_words, sanitize_next_url, canonicali
def test_random_words():
s = random_words()
assert len(s) > 0
assert s.find("_") > 0
assert s.count("_") == 1
assert len(s) > 3
s = random_words(2, 3)
assert s.count("_") == 0
assert s[-1] in (str(i) for i in range(10))
def test_random_string():