2019-12-04 01:03:02 +01:00
|
|
|
from flask import g
|
|
|
|
from flask import jsonify, request
|
2020-05-02 16:22:17 +02:00
|
|
|
from itsdangerous import SignatureExpired
|
2019-12-04 01:03:02 +01:00
|
|
|
|
2022-01-06 15:29:37 +01:00
|
|
|
from app.alias_utils import check_alias_prefix
|
2020-04-24 14:08:00 +02:00
|
|
|
from app.api.base import api_bp, require_api_auth
|
2020-05-23 19:18:50 +02:00
|
|
|
from app.api.serializer import (
|
|
|
|
serialize_alias_info_v2,
|
|
|
|
get_alias_info_v2,
|
|
|
|
)
|
2021-03-24 16:26:42 +01:00
|
|
|
from app.config import MAX_NB_EMAIL_FREE_PLAN, ALIAS_LIMIT
|
2020-05-02 16:22:17 +02:00
|
|
|
from app.dashboard.views.custom_alias import verify_prefix_suffix, signer
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
|
|
|
from app.extensions import limiter
|
2019-12-04 01:03:02 +01:00
|
|
|
from app.log import LOG
|
2020-05-23 12:02:01 +02:00
|
|
|
from app.models import (
|
|
|
|
Alias,
|
|
|
|
AliasUsedOn,
|
|
|
|
User,
|
|
|
|
DeletedAlias,
|
|
|
|
DomainDeletedAlias,
|
2020-06-02 09:33:56 +02:00
|
|
|
Mailbox,
|
|
|
|
AliasMailbox,
|
2020-05-23 12:02:01 +02:00
|
|
|
)
|
2019-12-04 01:03:02 +01:00
|
|
|
from app.utils import convert_to_id
|
|
|
|
|
|
|
|
|
2021-03-25 19:18:50 +01:00
|
|
|
@api_bp.route("/v2/alias/custom/new", methods=["POST"])
|
|
|
|
@limiter.limit(ALIAS_LIMIT)
|
|
|
|
@require_api_auth
|
|
|
|
def new_custom_alias_v2():
|
|
|
|
"""
|
|
|
|
Create a new custom alias
|
|
|
|
Same as v1 but signed_suffix is actually the suffix with signature, e.g.
|
|
|
|
.random_word@SL.co.Xq19rQ.s99uWQ7jD1s5JZDZqczYI5TbNNU
|
|
|
|
Input:
|
|
|
|
alias_prefix, for ex "www_groupon_com"
|
|
|
|
signed_suffix, either .random_letters@simplelogin.co or @my-domain.com
|
|
|
|
optional "hostname" in args
|
|
|
|
optional "note"
|
|
|
|
Output:
|
|
|
|
201 if success
|
|
|
|
409 if the alias already exists
|
|
|
|
|
|
|
|
"""
|
|
|
|
user: User = g.user
|
|
|
|
if not user.can_create_new_alias():
|
|
|
|
LOG.d("user %s cannot create any custom alias", user)
|
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
error="You have reached the limitation of a free account with the maximum of "
|
|
|
|
f"{MAX_NB_EMAIL_FREE_PLAN} aliases, please upgrade your plan to create more aliases"
|
|
|
|
),
|
|
|
|
400,
|
|
|
|
)
|
|
|
|
|
|
|
|
hostname = request.args.get("hostname")
|
|
|
|
|
|
|
|
data = request.get_json()
|
|
|
|
if not data:
|
|
|
|
return jsonify(error="request body cannot be empty"), 400
|
|
|
|
|
|
|
|
alias_prefix = data.get("alias_prefix", "").strip().lower().replace(" ", "")
|
|
|
|
signed_suffix = data.get("signed_suffix", "").strip()
|
|
|
|
note = data.get("note")
|
|
|
|
alias_prefix = convert_to_id(alias_prefix)
|
|
|
|
|
|
|
|
# hypothesis: user will click on the button in the 600 secs
|
|
|
|
try:
|
|
|
|
alias_suffix = signer.unsign(signed_suffix, max_age=600).decode()
|
|
|
|
except SignatureExpired:
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.w("Alias creation time expired for %s", user)
|
2021-03-25 19:18:50 +01:00
|
|
|
return jsonify(error="Alias creation time is expired, please retry"), 412
|
|
|
|
except Exception:
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.w("Alias suffix is tampered, user %s", user)
|
2021-03-25 19:18:50 +01:00
|
|
|
return jsonify(error="Tampered suffix"), 400
|
|
|
|
|
|
|
|
if not verify_prefix_suffix(user, alias_prefix, alias_suffix):
|
|
|
|
return jsonify(error="wrong alias prefix or suffix"), 400
|
|
|
|
|
|
|
|
full_alias = alias_prefix + alias_suffix
|
|
|
|
if (
|
|
|
|
Alias.get_by(email=full_alias)
|
|
|
|
or DeletedAlias.get_by(email=full_alias)
|
|
|
|
or DomainDeletedAlias.get_by(email=full_alias)
|
|
|
|
):
|
|
|
|
LOG.d("full alias already used %s", full_alias)
|
|
|
|
return jsonify(error=f"alias {full_alias} already exists"), 409
|
|
|
|
|
2021-12-31 11:15:24 +01:00
|
|
|
if ".." in full_alias:
|
|
|
|
return (
|
|
|
|
jsonify(error="2 consecutive dot signs aren't allowed in an email address"),
|
|
|
|
400,
|
|
|
|
)
|
|
|
|
|
2021-03-25 19:18:50 +01:00
|
|
|
alias = Alias.create(
|
|
|
|
user_id=user.id,
|
|
|
|
email=full_alias,
|
|
|
|
mailbox_id=user.default_mailbox_id,
|
|
|
|
note=note,
|
|
|
|
)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-03-25 19:18:50 +01:00
|
|
|
|
|
|
|
if hostname:
|
|
|
|
AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-03-25 19:18:50 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
jsonify(alias=full_alias, **serialize_alias_info_v2(get_alias_info_v2(alias))),
|
|
|
|
201,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-06-02 09:33:56 +02:00
|
|
|
@api_bp.route("/v3/alias/custom/new", methods=["POST"])
|
2021-03-24 16:26:42 +01:00
|
|
|
@limiter.limit(ALIAS_LIMIT)
|
2020-06-02 09:33:56 +02:00
|
|
|
@require_api_auth
|
|
|
|
def new_custom_alias_v3():
|
|
|
|
"""
|
|
|
|
Create a new custom alias
|
|
|
|
Same as v2 but accept a list of mailboxes as input
|
|
|
|
Input:
|
|
|
|
alias_prefix, for ex "www_groupon_com"
|
|
|
|
signed_suffix, either .random_letters@simplelogin.co or @my-domain.com
|
2020-06-02 20:06:32 +02:00
|
|
|
mailbox_ids: list of int
|
2020-06-02 09:33:56 +02:00
|
|
|
optional "hostname" in args
|
|
|
|
optional "note"
|
2020-06-03 21:22:29 +02:00
|
|
|
optional "name"
|
2020-06-02 09:33:56 +02:00
|
|
|
|
|
|
|
Output:
|
|
|
|
201 if success
|
|
|
|
409 if the alias already exists
|
|
|
|
|
|
|
|
"""
|
|
|
|
user: User = g.user
|
|
|
|
if not user.can_create_new_alias():
|
|
|
|
LOG.d("user %s cannot create any custom alias", user)
|
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
error="You have reached the limitation of a free account with the maximum of "
|
|
|
|
f"{MAX_NB_EMAIL_FREE_PLAN} aliases, please upgrade your plan to create more aliases"
|
|
|
|
),
|
|
|
|
400,
|
|
|
|
)
|
|
|
|
|
|
|
|
hostname = request.args.get("hostname")
|
|
|
|
|
|
|
|
data = request.get_json()
|
|
|
|
if not data:
|
|
|
|
return jsonify(error="request body cannot be empty"), 400
|
|
|
|
|
2021-10-23 15:52:17 +02:00
|
|
|
if type(data) is not dict:
|
|
|
|
return jsonify(error="request body does not follow the required format"), 400
|
|
|
|
|
2020-09-02 09:56:16 +02:00
|
|
|
alias_prefix = data.get("alias_prefix", "").strip().lower().replace(" ", "")
|
2021-12-15 17:12:27 +01:00
|
|
|
signed_suffix = data.get("signed_suffix", "") or ""
|
|
|
|
signed_suffix = signed_suffix.strip()
|
|
|
|
|
2020-06-02 20:06:32 +02:00
|
|
|
mailbox_ids = data.get("mailbox_ids")
|
2020-06-02 09:33:56 +02:00
|
|
|
note = data.get("note")
|
2020-06-03 21:22:29 +02:00
|
|
|
name = data.get("name")
|
2020-12-02 12:25:23 +01:00
|
|
|
if name:
|
|
|
|
name = name.replace("\n", "")
|
2020-06-02 09:33:56 +02:00
|
|
|
alias_prefix = convert_to_id(alias_prefix)
|
|
|
|
|
2020-11-03 10:39:08 +01:00
|
|
|
if not check_alias_prefix(alias_prefix):
|
2020-11-18 10:38:35 +01:00
|
|
|
return jsonify(error="alias prefix invalid format or too long"), 400
|
2020-11-03 10:39:08 +01:00
|
|
|
|
2020-06-02 09:33:56 +02:00
|
|
|
# check if mailbox is not tempered with
|
2021-10-23 15:55:39 +02:00
|
|
|
if type(mailbox_ids) is not list:
|
|
|
|
return jsonify(error="mailbox_ids must be an array of id"), 400
|
2020-06-02 09:33:56 +02:00
|
|
|
mailboxes = []
|
|
|
|
for mailbox_id in mailbox_ids:
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if not mailbox or mailbox.user_id != user.id or not mailbox.verified:
|
|
|
|
return jsonify(error="Errors with Mailbox"), 400
|
|
|
|
mailboxes.append(mailbox)
|
|
|
|
|
|
|
|
if not mailboxes:
|
|
|
|
return jsonify(error="At least one mailbox must be selected"), 400
|
|
|
|
|
|
|
|
# hypothesis: user will click on the button in the 600 secs
|
|
|
|
try:
|
|
|
|
alias_suffix = signer.unsign(signed_suffix, max_age=600).decode()
|
|
|
|
except SignatureExpired:
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.w("Alias creation time expired for %s", user)
|
2020-07-11 19:23:56 +02:00
|
|
|
return jsonify(error="Alias creation time is expired, please retry"), 412
|
2020-06-02 09:33:56 +02:00
|
|
|
except Exception:
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.w("Alias suffix is tampered, user %s", user)
|
2020-06-02 09:33:56 +02:00
|
|
|
return jsonify(error="Tampered suffix"), 400
|
|
|
|
|
|
|
|
if not verify_prefix_suffix(user, alias_prefix, alias_suffix):
|
|
|
|
return jsonify(error="wrong alias prefix or suffix"), 400
|
|
|
|
|
|
|
|
full_alias = alias_prefix + alias_suffix
|
|
|
|
if (
|
|
|
|
Alias.get_by(email=full_alias)
|
|
|
|
or DeletedAlias.get_by(email=full_alias)
|
|
|
|
or DomainDeletedAlias.get_by(email=full_alias)
|
|
|
|
):
|
|
|
|
LOG.d("full alias already used %s", full_alias)
|
|
|
|
return jsonify(error=f"alias {full_alias} already exists"), 409
|
|
|
|
|
2021-12-02 16:17:41 +01:00
|
|
|
if ".." in full_alias:
|
|
|
|
return (
|
|
|
|
jsonify(error="2 consecutive dot signs aren't allowed in an email address"),
|
|
|
|
400,
|
|
|
|
)
|
|
|
|
|
2020-06-02 09:33:56 +02:00
|
|
|
alias = Alias.create(
|
|
|
|
user_id=user.id,
|
|
|
|
email=full_alias,
|
|
|
|
note=note,
|
2020-06-03 21:22:29 +02:00
|
|
|
name=name or None,
|
2020-06-02 09:33:56 +02:00
|
|
|
mailbox_id=mailboxes[0].id,
|
|
|
|
)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.flush()
|
2020-06-02 09:33:56 +02:00
|
|
|
|
|
|
|
for i in range(1, len(mailboxes)):
|
|
|
|
AliasMailbox.create(
|
2020-08-27 10:20:48 +02:00
|
|
|
alias_id=alias.id,
|
|
|
|
mailbox_id=mailboxes[i].id,
|
2020-06-02 09:33:56 +02:00
|
|
|
)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-06-02 09:33:56 +02:00
|
|
|
|
|
|
|
if hostname:
|
|
|
|
AliasUsedOn.create(alias_id=alias.id, hostname=hostname, user_id=alias.user_id)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-06-02 09:33:56 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
jsonify(alias=full_alias, **serialize_alias_info_v2(get_alias_info_v2(alias))),
|
|
|
|
201,
|
|
|
|
)
|