mirror of
https://github.com/simple-login/app.git
synced 2024-11-16 00:48:32 +01:00
add /alias/custom/new and /alias/random/new
This commit is contained in:
parent
ab4f5bf329
commit
8250ab2f22
3 changed files with 116 additions and 1 deletions
|
@ -1 +1 @@
|
|||
from .views import index, alias_options
|
||||
from .views import index, alias_options, new_custom_alias, new_random_alias
|
||||
|
|
79
app/api/views/new_custom_alias.py
Normal file
79
app/api/views/new_custom_alias.py
Normal file
|
@ -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
|
36
app/api/views/new_random_alias.py
Normal file
36
app/api/views/new_random_alias.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue