From 8250ab2f2213c8ba45f4d3475e1991c7df172176 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 4 Dec 2019 00:03:02 +0000 Subject: [PATCH] add /alias/custom/new and /alias/random/new --- app/api/__init__.py | 2 +- app/api/views/new_custom_alias.py | 79 +++++++++++++++++++++++++++++++ app/api/views/new_random_alias.py | 36 ++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 app/api/views/new_custom_alias.py create mode 100644 app/api/views/new_random_alias.py diff --git a/app/api/__init__.py b/app/api/__init__.py index d3f61d00..6af4ba88 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -1 +1 @@ -from .views import index, alias_options +from .views import index, alias_options, new_custom_alias, new_random_alias diff --git a/app/api/views/new_custom_alias.py b/app/api/views/new_custom_alias.py new file mode 100644 index 00000000..5a0a28a9 --- /dev/null +++ b/app/api/views/new_custom_alias.py @@ -0,0 +1,79 @@ +from flask import g +from flask import jsonify, request +from flask_cors import cross_origin + +from app.api.base import api_bp, verify_api_key +from app.config import EMAIL_DOMAIN +from app.extensions import db +from app.log import LOG +from app.models import GenEmail, AliasUsedOn +from app.utils import convert_to_id + + +@api_bp.route("/alias/custom/new", methods=["POST"]) +@cross_origin() +@verify_api_key +def new_custom_alias(): + """ + Create a new custom alias + Input: + alias_prefix, for ex "www_groupon_com" + alias_suffix, either .random_letters@simplelogin.co or @my-domain.com + optional "hostname" in args + Output: + 201 if success + 409 if alias already exists + + """ + user = g.user + if not user.can_create_new_custom_alias(): + LOG.d("user %s cannot create custom alias", user) + return jsonify(error="no more quota for custom alias"), 400 + + user_custom_domains = [cd.domain for cd in user.verified_custom_domains()] + hostname = request.args.get("hostname") + + data = request.get_json() + alias_prefix = data["alias_prefix"] + alias_suffix = data["alias_suffix"] + + # make sure alias_prefix is more than 3 chars + alias_prefix = alias_prefix.strip() + alias_prefix = convert_to_id(alias_prefix) + if len(alias_prefix) < 3: # should be checked on frontend + LOG.d("user %s submits too short alias prefix %s", user, alias_prefix) + return jsonify(error="alias prefix must be at least 3 letters"), 400 + + # make sure alias_suffix is either .random_letters@simplelogin.co or @my-domain.com + alias_suffix = alias_suffix.strip() + if alias_suffix.startswith("@"): + custom_domain = alias_suffix[1:] + if custom_domain not in user_custom_domains: + LOG.d("user %s submits wrong custom domain %s ", user, custom_domain) + return jsonify(error="error"), 400 + else: + if not alias_suffix.startswith("."): + LOG.d("user %s submits wrong alias suffix %s", user, alias_suffix) + return jsonify(error="error"), 400 + if not alias_suffix.endswith(EMAIL_DOMAIN): + LOG.d("user %s submits wrong alias suffix %s", user, alias_suffix) + return jsonify(error="error"), 400 + + random_letters = alias_suffix[1 : alias_suffix.find("@")] + if len(random_letters) < 5: + LOG.d("user %s submits wrong alias suffix %s", user, alias_suffix) + return jsonify(error="error"), 400 + + full_alias = alias_prefix + alias_suffix + if GenEmail.get_by(email=full_alias): + LOG.d("full alias already used %s", full_alias) + return jsonify(error=f"alias {full_alias} already exists"), 409 + + gen_email = GenEmail.create(user_id=user.id, email=full_alias) + db.session.commit() + + if hostname: + AliasUsedOn.create(gen_email_id=gen_email.id, hostname=hostname) + db.session.commit() + + return jsonify(alias=full_alias), 201 diff --git a/app/api/views/new_random_alias.py b/app/api/views/new_random_alias.py new file mode 100644 index 00000000..519fd1c4 --- /dev/null +++ b/app/api/views/new_random_alias.py @@ -0,0 +1,36 @@ +from flask import g +from flask import jsonify, request +from flask_cors import cross_origin + +from app.api.base import api_bp, verify_api_key +from app.extensions import db +from app.log import LOG +from app.models import GenEmail, AliasUsedOn + + +@api_bp.route("/alias/random/new", methods=["POST"]) +@cross_origin() +@verify_api_key +def new_random_alias(): + """ + Create a new random alias + Input: + optional "hostname" in args + Output: + 201 if success + 409 if alias already exists + + """ + user = g.user + if not user.can_create_new_random_alias(): + LOG.d("user %s cannot create random alias", user) + return jsonify(error="no more quota for random alias"), 400 + + hostname = request.args.get("hostname") + gen_email = GenEmail.create_new_gen_email(user_id=user.id) + + if hostname: + AliasUsedOn.create(gen_email_id=gen_email.id, hostname=hostname) + db.session.commit() + + return jsonify(alias=gen_email.email), 201