basic implementation of random suffix

This commit is contained in:
devStorm 2021-05-13 16:13:19 -07:00
parent cb3bc8bc36
commit 3fc250018d
No known key found for this signature in database
GPG Key ID: D52E1B66F336AC57
4 changed files with 27 additions and 4 deletions

View File

@ -374,3 +374,5 @@ except Exception:
ALIAS_LIMIT = os.environ.get("ALIAS_LIMIT") or "100/day;50/hour;5/minute"
ENABLE_SPAM_ASSASSIN = "ENABLE_SPAM_ASSASSIN" in os.environ
ALIAS_RAND_SUFFIX_LENGTH = int(os.environ.get("ALIAS_RAND_SUFFIX_LENGTH", 5))

View File

@ -10,6 +10,7 @@ from app.config import (
DISABLE_ALIAS_SUFFIX,
CUSTOM_ALIAS_SECRET,
ALIAS_LIMIT,
ALIAS_RAND_SUFFIX_LENGTH,
)
from app.dashboard.base import dashboard_bp
from app.extensions import db, limiter
@ -23,7 +24,7 @@ from app.models import (
AliasMailbox,
DomainDeletedAlias,
)
from app.utils import random_word, word_exist
from app.utils import random_word, word_exist, random_string
signer = TimestampSigner(CUSTOM_ALIAS_SECRET)
@ -54,7 +55,7 @@ def get_available_suffixes(user: User) -> [SuffixInfo]:
# for each user domain, generate both the domain and a random suffix version
for custom_domain in user_custom_domains:
if custom_domain.random_prefix_generation:
suffix = "." + random_word() + "@" + custom_domain.domain
suffix = "." + get_suffix(user) + "@" + custom_domain.domain
suffix_info = SuffixInfo(True, suffix, signer.sign(suffix).decode(), False)
if user.default_alias_custom_domain_id == custom_domain.id:
suffixes.insert(0, suffix_info)
@ -77,7 +78,7 @@ def get_available_suffixes(user: User) -> [SuffixInfo]:
# then SimpleLogin domain
for sl_domain in user.get_sl_domains():
suffix = (
("" if DISABLE_ALIAS_SUFFIX else "." + random_word())
("" if DISABLE_ALIAS_SUFFIX else "." + get_suffix(user))
+ "@"
+ sl_domain.domain
)
@ -249,6 +250,18 @@ def custom_alias():
mailboxes=mailboxes,
)
def get_suffix(user: User) -> str:
"""Get random suffix for an alias based on user's preference.
Args:
user (User): the user who is trying to create an alias
Returns:
str: the random suffix generated
"""
if user.random_alias_suffix:
return random_string(ALIAS_RAND_SUFFIX_LENGTH, include_digits = True)
return random_word()
def verify_prefix_suffix(user: User, alias_prefix, alias_suffix) -> bool:
"""verify if user could create an alias with the given prefix and suffix"""

View File

@ -287,6 +287,11 @@ class User(db.Model, ModelMixin, UserMixin):
db.Boolean, default=False, nullable=False, server_default="0"
)
# whether to use random string or random word as suffix
random_alias_suffix = db.Column(
db.Boolean, default=True, nullable=False, server_default="1"
)
@classmethod
def create(cls, email, name="", password=None, **kwargs):
user: User = super(User, cls).create(email=email, name=name, **kwargs)

View File

@ -27,9 +27,12 @@ def random_words():
return "_".join([random.choice(_words) for i in range(nb_words)])
def random_string(length=10):
def random_string(length=10, include_digits=False):
"""Generate a random string of fixed length """
letters = string.ascii_lowercase
if include_digits:
letters += string.digits
return "".join(random.choice(letters) for _ in range(length))