2022-06-13 12:41:47 +02:00
|
|
|
import base64
|
|
|
|
import hmac
|
|
|
|
import secrets
|
2021-03-06 18:07:51 +01:00
|
|
|
|
2022-06-13 12:41:47 +02:00
|
|
|
import arrow
|
2021-02-17 12:56:28 +01:00
|
|
|
from flask import render_template, redirect, url_for, flash, request
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
2022-06-13 12:41:47 +02:00
|
|
|
from app import config
|
2021-02-17 12:56:28 +01:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2022-06-10 15:50:44 +02:00
|
|
|
from app.dashboard.views.enter_sudo import sudo_required
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2021-03-06 17:24:26 +01:00
|
|
|
from app.email_utils import send_email, render
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.extensions import limiter
|
2021-02-17 12:56:28 +01:00
|
|
|
from app.log import LOG
|
|
|
|
from app.models import (
|
|
|
|
Alias,
|
|
|
|
Contact,
|
|
|
|
AliasUsedOn,
|
|
|
|
AliasMailbox,
|
|
|
|
User,
|
|
|
|
ClientUser,
|
|
|
|
)
|
|
|
|
from app.models import Mailbox
|
|
|
|
|
|
|
|
|
|
|
|
def transfer(alias, new_user, new_mailboxes: [Mailbox]):
|
|
|
|
# cannot transfer alias which is used for receiving newsletter
|
|
|
|
if User.get_by(newsletter_alias_id=alias.id):
|
|
|
|
raise Exception("Cannot transfer alias that's used to receive newsletter")
|
|
|
|
|
|
|
|
# update user_id
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.query(Contact).filter(Contact.alias_id == alias.id).update(
|
2021-02-17 12:56:28 +01:00
|
|
|
{"user_id": new_user.id}
|
|
|
|
)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.query(AliasUsedOn).filter(AliasUsedOn.alias_id == alias.id).update(
|
2021-02-17 12:56:28 +01:00
|
|
|
{"user_id": new_user.id}
|
|
|
|
)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.query(ClientUser).filter(ClientUser.alias_id == alias.id).update(
|
2021-02-17 12:56:28 +01:00
|
|
|
{"user_id": new_user.id}
|
|
|
|
)
|
|
|
|
|
|
|
|
# remove existing mailboxes from the alias
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.query(AliasMailbox).filter(AliasMailbox.alias_id == alias.id).delete()
|
2021-02-17 12:56:28 +01:00
|
|
|
|
|
|
|
# set mailboxes
|
|
|
|
alias.mailbox_id = new_mailboxes.pop().id
|
|
|
|
for mb in new_mailboxes:
|
|
|
|
AliasMailbox.create(alias_id=alias.id, mailbox_id=mb.id)
|
|
|
|
|
|
|
|
# alias has never been transferred before
|
|
|
|
if not alias.original_owner_id:
|
|
|
|
alias.original_owner_id = alias.user_id
|
|
|
|
|
2021-03-06 17:24:26 +01:00
|
|
|
# inform previous owner
|
|
|
|
old_user = alias.user
|
|
|
|
send_email(
|
|
|
|
old_user.email,
|
|
|
|
f"Alias {alias.email} has been received",
|
|
|
|
render(
|
|
|
|
"transactional/alias-transferred.txt",
|
|
|
|
alias=alias,
|
|
|
|
),
|
|
|
|
render(
|
|
|
|
"transactional/alias-transferred.html",
|
|
|
|
alias=alias,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2021-02-17 12:56:28 +01:00
|
|
|
# now the alias belongs to the new user
|
|
|
|
alias.user_id = new_user.id
|
|
|
|
|
2021-02-17 13:08:02 +01:00
|
|
|
# set some fields back to default
|
|
|
|
alias.disable_pgp = False
|
|
|
|
alias.pinned = False
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-02-17 12:56:28 +01:00
|
|
|
|
|
|
|
|
2022-06-13 12:41:47 +02:00
|
|
|
def hmac_alias_transfer_token(transfer_token: str) -> str:
|
|
|
|
alias_hmac = hmac.new(
|
|
|
|
config.ALIAS_TRANSFER_TOKEN_SECRET.encode("utf-8"),
|
|
|
|
transfer_token.encode("utf-8"),
|
|
|
|
"sha3_224",
|
|
|
|
)
|
|
|
|
return base64.urlsafe_b64encode(alias_hmac.digest()).decode("utf-8").rstrip("=")
|
|
|
|
|
|
|
|
|
2021-02-17 12:56:28 +01:00
|
|
|
@dashboard_bp.route("/alias_transfer/send/<int:alias_id>/", methods=["GET", "POST"])
|
|
|
|
@login_required
|
2022-06-10 15:50:44 +02:00
|
|
|
@sudo_required
|
2021-02-17 12:56:28 +01:00
|
|
|
def alias_transfer_send_route(alias_id):
|
|
|
|
alias = Alias.get(alias_id)
|
|
|
|
if not alias or alias.user_id != current_user.id:
|
|
|
|
flash("You cannot see this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
if current_user.newsletter_alias_id == alias.id:
|
|
|
|
flash(
|
|
|
|
"This alias is currently used for receiving the newsletter and cannot be transferred",
|
|
|
|
"error",
|
|
|
|
)
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2022-06-13 12:41:47 +02:00
|
|
|
alias_transfer_url = None
|
2021-02-17 12:56:28 +01:00
|
|
|
|
2021-03-06 18:07:51 +01:00
|
|
|
if request.method == "POST":
|
2022-06-13 12:41:47 +02:00
|
|
|
# generate a new transfer_token
|
2021-03-06 18:07:51 +01:00
|
|
|
if request.form.get("form-name") == "create":
|
2022-06-13 12:41:47 +02:00
|
|
|
transfer_token = f"{alias.id}.{secrets.token_urlsafe(32)}"
|
|
|
|
alias.transfer_token = hmac_alias_transfer_token(transfer_token)
|
|
|
|
alias.transfer_token_expiration = arrow.utcnow().shift(hours=24)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-03-06 18:07:51 +01:00
|
|
|
alias_transfer_url = (
|
2022-06-13 12:41:47 +02:00
|
|
|
config.URL
|
2021-03-06 18:10:41 +01:00
|
|
|
+ "/dashboard/alias_transfer/receive"
|
2022-06-13 12:41:47 +02:00
|
|
|
+ f"?token={transfer_token}"
|
2021-03-06 18:07:51 +01:00
|
|
|
)
|
2022-06-13 12:41:47 +02:00
|
|
|
flash("Share alias URL created", "success")
|
2021-03-06 18:07:51 +01:00
|
|
|
# request.form.get("form-name") == "remove"
|
|
|
|
else:
|
|
|
|
alias.transfer_token = None
|
2022-06-13 12:41:47 +02:00
|
|
|
alias.transfer_token_expiration = None
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-03-06 18:07:51 +01:00
|
|
|
alias_transfer_url = None
|
|
|
|
flash("Share URL deleted", "success")
|
2021-02-17 12:56:28 +01:00
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"dashboard/alias_transfer_send.html",
|
|
|
|
alias=alias,
|
|
|
|
alias_transfer_url=alias_transfer_url,
|
2022-06-13 12:41:47 +02:00
|
|
|
link_active=alias.transfer_token_expiration is not None
|
|
|
|
and alias.transfer_token_expiration > arrow.utcnow(),
|
2021-02-17 12:56:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/alias_transfer/receive", methods=["GET", "POST"])
|
2021-03-06 18:07:51 +01:00
|
|
|
@limiter.limit("5/minute")
|
2021-02-17 12:56:28 +01:00
|
|
|
@login_required
|
|
|
|
def alias_transfer_receive_route():
|
|
|
|
"""
|
|
|
|
URL has ?alias_id=signed_alias_id
|
|
|
|
"""
|
2021-03-06 18:07:51 +01:00
|
|
|
token = request.args.get("token")
|
2022-06-13 12:41:47 +02:00
|
|
|
if not token:
|
|
|
|
flash("Invalid transfer token", "error")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
hashed_token = hmac_alias_transfer_token(token)
|
|
|
|
# TODO: Don't allow unhashed tokens once all the tokens have been migrated to the new format
|
|
|
|
alias = Alias.get_by(transfer_token=token) or Alias.get_by(
|
|
|
|
transfer_token=hashed_token
|
|
|
|
)
|
2021-02-17 12:56:28 +01:00
|
|
|
|
2021-03-06 18:07:51 +01:00
|
|
|
if not alias:
|
2021-02-17 12:56:28 +01:00
|
|
|
flash("Invalid link", "error")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2022-06-13 12:41:47 +02:00
|
|
|
# TODO: Don't allow none once all the tokens have been migrated to the new format
|
|
|
|
if (
|
|
|
|
alias.transfer_token_expiration is not None
|
|
|
|
and alias.transfer_token_expiration < arrow.utcnow()
|
|
|
|
):
|
|
|
|
flash("Expired link, please request a new one", "error")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2021-02-17 12:56:28 +01:00
|
|
|
# alias already belongs to this user
|
|
|
|
if alias.user_id == current_user.id:
|
|
|
|
flash("You already own this alias", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2021-07-03 17:11:54 +02:00
|
|
|
# check if user has not exceeded the alias quota
|
|
|
|
if not current_user.can_create_new_alias():
|
|
|
|
LOG.d("%s can't receive new alias", current_user)
|
|
|
|
flash(
|
|
|
|
"You have reached free plan limit, please upgrade to create new aliases",
|
|
|
|
"warning",
|
|
|
|
)
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2021-02-17 12:56:28 +01:00
|
|
|
mailboxes = current_user.mailboxes()
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
mailbox_ids = request.form.getlist("mailbox_ids")
|
|
|
|
# check if mailbox is not tempered with
|
|
|
|
mailboxes = []
|
|
|
|
for mailbox_id in mailbox_ids:
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if (
|
|
|
|
not mailbox
|
|
|
|
or mailbox.user_id != current_user.id
|
|
|
|
or not mailbox.verified
|
|
|
|
):
|
|
|
|
flash("Something went wrong, please retry", "warning")
|
|
|
|
return redirect(request.url)
|
|
|
|
mailboxes.append(mailbox)
|
|
|
|
|
|
|
|
if not mailboxes:
|
|
|
|
flash("You must select at least 1 mailbox", "warning")
|
|
|
|
return redirect(request.url)
|
|
|
|
|
|
|
|
LOG.d(
|
2022-06-13 12:41:47 +02:00
|
|
|
"transfer alias %s from %s to %s with %s with token %s",
|
2022-03-23 18:33:33 +01:00
|
|
|
alias,
|
|
|
|
alias.user,
|
|
|
|
current_user,
|
|
|
|
mailboxes,
|
2022-06-13 12:41:47 +02:00
|
|
|
token,
|
2021-02-17 12:56:28 +01:00
|
|
|
)
|
|
|
|
transfer(alias, current_user, mailboxes)
|
|
|
|
flash(f"You are now owner of {alias.email}", "success")
|
|
|
|
return redirect(url_for("dashboard.index", highlight_alias_id=alias.id))
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"dashboard/alias_transfer_receive.html",
|
|
|
|
alias=alias,
|
|
|
|
mailboxes=mailboxes,
|
|
|
|
)
|