new_custom_alias.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from flask import g
  2. from flask import jsonify, request
  3. from flask_cors import cross_origin
  4. from app.api.base import api_bp, verify_api_key
  5. from app.config import EMAIL_DOMAIN
  6. from app.extensions import db
  7. from app.log import LOG
  8. from app.models import GenEmail, AliasUsedOn
  9. from app.utils import convert_to_id
  10. @api_bp.route("/alias/custom/new", methods=["POST"])
  11. @cross_origin()
  12. @verify_api_key
  13. def new_custom_alias():
  14. """
  15. Create a new custom alias
  16. Input:
  17. alias_prefix, for ex "www_groupon_com"
  18. alias_suffix, either .random_letters@simplelogin.co or @my-domain.com
  19. optional "hostname" in args
  20. Output:
  21. 201 if success
  22. 409 if the alias already exists
  23. """
  24. user = g.user
  25. if not user.can_create_new_alias():
  26. LOG.d("user %s cannot create any custom alias", user)
  27. return (
  28. jsonify(
  29. error="You have reached the limitation of a free account with the maximum of "
  30. "3 custom aliases, please upgrade your plan to create more custom aliases"
  31. ),
  32. 400,
  33. )
  34. user_custom_domains = [cd.domain for cd in user.verified_custom_domains()]
  35. hostname = request.args.get("hostname")
  36. data = request.get_json()
  37. alias_prefix = data["alias_prefix"]
  38. alias_suffix = data["alias_suffix"]
  39. # make sure alias_prefix is not empty
  40. alias_prefix = alias_prefix.strip()
  41. alias_prefix = convert_to_id(alias_prefix)
  42. if not alias_prefix: # should be checked on frontend
  43. LOG.d("user %s submits an empty alias with the prefix %s", user, alias_prefix)
  44. return jsonify(error="alias prefix cannot be empty"), 400
  45. # make sure alias_suffix is either .random_letters@simplelogin.co or @my-domain.com
  46. alias_suffix = alias_suffix.strip()
  47. if alias_suffix.startswith("@"):
  48. custom_domain = alias_suffix[1:]
  49. if custom_domain not in user_custom_domains:
  50. LOG.d("user %s submits a wrong custom domain %s ", user, custom_domain)
  51. return jsonify(error="error"), 400
  52. else:
  53. if not alias_suffix.startswith("."):
  54. LOG.d("user %s submits a wrong alias suffix %s", user, alias_suffix)
  55. return jsonify(error="error"), 400
  56. if not alias_suffix.endswith(EMAIL_DOMAIN):
  57. LOG.d("user %s submits a wrong alias suffix %s", user, alias_suffix)
  58. return jsonify(error="error"), 400
  59. random_letters = alias_suffix[1 : alias_suffix.find("@")]
  60. if len(random_letters) < 5:
  61. LOG.d("user %s submits a wrong alias suffix %s", user, alias_suffix)
  62. return jsonify(error="error"), 400
  63. full_alias = alias_prefix + alias_suffix
  64. if GenEmail.get_by(email=full_alias):
  65. LOG.d("full alias already used %s", full_alias)
  66. return jsonify(error=f"alias {full_alias} already exists"), 409
  67. gen_email = GenEmail.create(user_id=user.id, email=full_alias)
  68. db.session.commit()
  69. if hostname:
  70. AliasUsedOn.create(gen_email_id=gen_email.id, hostname=hostname)
  71. db.session.commit()
  72. return jsonify(alias=full_alias), 201