mirror of
https://github.com/simple-login/app.git
synced 2024-11-18 22:53:27 +08:00
replace random_word with get_suffix(user)
This commit is contained in:
parent
4a0fc8380f
commit
f7bef3941a
4 changed files with 23 additions and 23 deletions
|
@ -9,7 +9,7 @@ from app.dashboard.views.custom_alias import (
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.log import LOG
|
from app.log import LOG
|
||||||
from app.models import AliasUsedOn, Alias, User
|
from app.models import AliasUsedOn, Alias, User
|
||||||
from app.utils import convert_to_id, random_word
|
from app.utils import convert_to_id, random_word, get_suffix
|
||||||
|
|
||||||
|
|
||||||
@api_bp.route("/alias/options")
|
@api_bp.route("/alias/options")
|
||||||
|
@ -76,7 +76,7 @@ def options():
|
||||||
if DISABLE_ALIAS_SUFFIX:
|
if DISABLE_ALIAS_SUFFIX:
|
||||||
ret["custom"]["suffixes"].append(f"@{domain}")
|
ret["custom"]["suffixes"].append(f"@{domain}")
|
||||||
else:
|
else:
|
||||||
ret["custom"]["suffixes"].append(f".{random_word()}@{domain}")
|
ret["custom"]["suffixes"].append(f".{get_suffix(user)}@{domain}")
|
||||||
|
|
||||||
for custom_domain in user.verified_custom_domains():
|
for custom_domain in user.verified_custom_domains():
|
||||||
ret["custom"]["suffixes"].append("@" + custom_domain.domain)
|
ret["custom"]["suffixes"].append("@" + custom_domain.domain)
|
||||||
|
@ -156,7 +156,7 @@ def options_v2():
|
||||||
if DISABLE_ALIAS_SUFFIX:
|
if DISABLE_ALIAS_SUFFIX:
|
||||||
ret["suffixes"].append(f"@{domain}")
|
ret["suffixes"].append(f"@{domain}")
|
||||||
else:
|
else:
|
||||||
ret["suffixes"].append(f".{random_word()}@{domain}")
|
ret["suffixes"].append(f".{get_suffix(user)}@{domain}")
|
||||||
|
|
||||||
for custom_domain in user.verified_custom_domains():
|
for custom_domain in user.verified_custom_domains():
|
||||||
ret["suffixes"].append("@" + custom_domain.domain)
|
ret["suffixes"].append("@" + custom_domain.domain)
|
||||||
|
@ -232,7 +232,7 @@ def options_v3():
|
||||||
if DISABLE_ALIAS_SUFFIX:
|
if DISABLE_ALIAS_SUFFIX:
|
||||||
ret["suffixes"].append(f"@{domain}")
|
ret["suffixes"].append(f"@{domain}")
|
||||||
else:
|
else:
|
||||||
ret["suffixes"].append(f".{random_word()}@{domain}")
|
ret["suffixes"].append(f".{get_suffix(user)}@{domain}")
|
||||||
|
|
||||||
for custom_domain in user.verified_custom_domains():
|
for custom_domain in user.verified_custom_domains():
|
||||||
ret["suffixes"].append("@" + custom_domain.domain)
|
ret["suffixes"].append("@" + custom_domain.domain)
|
||||||
|
|
|
@ -10,7 +10,6 @@ from app.config import (
|
||||||
DISABLE_ALIAS_SUFFIX,
|
DISABLE_ALIAS_SUFFIX,
|
||||||
CUSTOM_ALIAS_SECRET,
|
CUSTOM_ALIAS_SECRET,
|
||||||
ALIAS_LIMIT,
|
ALIAS_LIMIT,
|
||||||
ALIAS_RANDOM_SUFFIX_LENGTH,
|
|
||||||
)
|
)
|
||||||
from app.dashboard.base import dashboard_bp
|
from app.dashboard.base import dashboard_bp
|
||||||
from app.extensions import db, limiter
|
from app.extensions import db, limiter
|
||||||
|
@ -23,9 +22,8 @@ from app.models import (
|
||||||
User,
|
User,
|
||||||
AliasMailbox,
|
AliasMailbox,
|
||||||
DomainDeletedAlias,
|
DomainDeletedAlias,
|
||||||
AliasSuffixEnum,
|
|
||||||
)
|
)
|
||||||
from app.utils import random_word, random_string
|
from app.utils import get_suffix
|
||||||
|
|
||||||
signer = TimestampSigner(CUSTOM_ALIAS_SECRET)
|
signer = TimestampSigner(CUSTOM_ALIAS_SECRET)
|
||||||
|
|
||||||
|
@ -252,20 +250,6 @@ def custom_alias():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
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 == AliasSuffixEnum.random_string.value:
|
|
||||||
return random_string(ALIAS_RANDOM_SUFFIX_LENGTH, include_digits=True)
|
|
||||||
return random_word()
|
|
||||||
|
|
||||||
|
|
||||||
def verify_prefix_suffix(user: User, alias_prefix, alias_suffix) -> bool:
|
def verify_prefix_suffix(user: User, alias_prefix, alias_suffix) -> bool:
|
||||||
"""verify if user could create an alias with the given prefix and suffix"""
|
"""verify if user could create an alias with the given prefix and suffix"""
|
||||||
if not alias_prefix or not alias_suffix: # should be caught on frontend
|
if not alias_prefix or not alias_suffix: # should be caught on frontend
|
||||||
|
|
|
@ -36,6 +36,7 @@ from app.utils import (
|
||||||
random_words,
|
random_words,
|
||||||
random_word,
|
random_word,
|
||||||
sanitize_email,
|
sanitize_email,
|
||||||
|
get_suffix,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1124,7 +1125,7 @@ class Alias(db.Model, ModelMixin):
|
||||||
|
|
||||||
# find the right suffix - avoid infinite loop by running this at max 1000 times
|
# find the right suffix - avoid infinite loop by running this at max 1000 times
|
||||||
for i in range(1000):
|
for i in range(1000):
|
||||||
suffix = random_word()
|
suffix = get_suffix(user)
|
||||||
email = f"{prefix}.{suffix}@{FIRST_ALIAS_DOMAIN}"
|
email = f"{prefix}.{suffix}@{FIRST_ALIAS_DOMAIN}"
|
||||||
|
|
||||||
if not cls.get_by(email=email) and not DeletedAlias.get_by(email=email):
|
if not cls.get_by(email=email) and not DeletedAlias.get_by(email=email):
|
||||||
|
|
17
app/utils.py
17
app/utils.py
|
@ -4,8 +4,9 @@ import urllib.parse
|
||||||
|
|
||||||
from unidecode import unidecode
|
from unidecode import unidecode
|
||||||
|
|
||||||
from .config import WORDS_FILE_PATH
|
from .config import WORDS_FILE_PATH, ALIAS_RANDOM_SUFFIX_LENGTH
|
||||||
from .log import LOG
|
from .log import LOG
|
||||||
|
from .models import User, AliasSuffixEnum
|
||||||
|
|
||||||
with open(WORDS_FILE_PATH) as f:
|
with open(WORDS_FILE_PATH) as f:
|
||||||
LOG.d("load words file: %s", WORDS_FILE_PATH)
|
LOG.d("load words file: %s", WORDS_FILE_PATH)
|
||||||
|
@ -16,6 +17,20 @@ def random_word():
|
||||||
return random.choice(_words)
|
return random.choice(_words)
|
||||||
|
|
||||||
|
|
||||||
|
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 == AliasSuffixEnum.random_string.value:
|
||||||
|
return random_string(ALIAS_RANDOM_SUFFIX_LENGTH, include_digits=True)
|
||||||
|
return random_word()
|
||||||
|
|
||||||
|
|
||||||
def word_exist(word):
|
def word_exist(word):
|
||||||
return word in _words
|
return word in _words
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue