2020-10-15 16:45:28 +02:00
|
|
|
from functools import wraps
|
2020-05-18 11:14:40 +02:00
|
|
|
from time import time
|
|
|
|
|
|
|
|
from flask import render_template, flash, redirect, url_for, session, request
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import PasswordField, validators
|
|
|
|
|
2022-07-26 12:38:18 +02:00
|
|
|
from app.config import CONNECT_WITH_PROTON
|
2020-05-18 11:14:40 +02:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2023-07-26 12:56:06 +02:00
|
|
|
from app.extensions import limiter
|
2020-05-18 11:14:40 +02:00
|
|
|
from app.log import LOG
|
2022-07-04 16:09:36 +02:00
|
|
|
from app.models import PartnerUser
|
2022-07-26 12:38:18 +02:00
|
|
|
from app.proton.utils import get_proton_partner
|
2022-03-29 18:03:18 +02:00
|
|
|
from app.utils import sanitize_next_url
|
2020-05-18 11:14:40 +02:00
|
|
|
|
2020-05-18 22:46:13 +02:00
|
|
|
_SUDO_GAP = 900
|
2020-05-18 11:14:40 +02:00
|
|
|
|
2020-05-18 22:54:05 +02:00
|
|
|
|
2020-05-18 11:14:40 +02:00
|
|
|
class LoginForm(FlaskForm):
|
|
|
|
password = PasswordField("Password", validators=[validators.DataRequired()])
|
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/enter_sudo", methods=["GET", "POST"])
|
2023-07-26 12:56:06 +02:00
|
|
|
@limiter.limit("3/minute")
|
2020-05-18 11:14:40 +02:00
|
|
|
@login_required
|
|
|
|
def enter_sudo():
|
|
|
|
password_check_form = LoginForm()
|
|
|
|
|
|
|
|
if password_check_form.validate_on_submit():
|
|
|
|
password = password_check_form.password.data
|
|
|
|
|
|
|
|
if current_user.check_password(password):
|
|
|
|
session["sudo_time"] = int(time())
|
|
|
|
|
|
|
|
# User comes to sudo page from another page
|
2022-03-29 18:03:18 +02:00
|
|
|
next_url = sanitize_next_url(request.args.get("next"))
|
2020-05-18 11:14:40 +02:00
|
|
|
if next_url:
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.d("redirect user to %s", next_url)
|
2020-05-18 11:14:40 +02:00
|
|
|
return redirect(next_url)
|
|
|
|
else:
|
2021-09-08 11:29:55 +02:00
|
|
|
LOG.d("redirect user to dashboard")
|
2020-05-18 11:14:40 +02:00
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
else:
|
|
|
|
flash("Incorrect password", "warning")
|
|
|
|
|
2022-07-26 12:38:18 +02:00
|
|
|
proton_enabled = CONNECT_WITH_PROTON
|
2022-07-04 16:09:36 +02:00
|
|
|
if proton_enabled:
|
|
|
|
# Only for users that have the account linked
|
|
|
|
partner_user = PartnerUser.get_by(user_id=current_user.id)
|
|
|
|
if not partner_user or partner_user.partner_id != get_proton_partner().id:
|
|
|
|
proton_enabled = False
|
|
|
|
|
2020-05-18 11:14:40 +02:00
|
|
|
return render_template(
|
2022-07-04 16:09:36 +02:00
|
|
|
"dashboard/enter_sudo.html",
|
|
|
|
password_check_form=password_check_form,
|
|
|
|
next=request.args.get("next"),
|
|
|
|
connect_with_proton=proton_enabled,
|
2020-05-18 11:14:40 +02:00
|
|
|
)
|
|
|
|
|
2020-05-18 11:15:52 +02:00
|
|
|
|
2020-05-18 11:14:40 +02:00
|
|
|
def sudo_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def wrap(*args, **kwargs):
|
2020-05-18 11:15:52 +02:00
|
|
|
if (
|
|
|
|
"sudo_time" not in session
|
2020-05-18 22:46:13 +02:00
|
|
|
or (time() - int(session["sudo_time"])) > _SUDO_GAP
|
2020-05-18 11:15:52 +02:00
|
|
|
):
|
|
|
|
return redirect(url_for("dashboard.enter_sudo", next=request.path))
|
2020-05-18 11:14:40 +02:00
|
|
|
return f(*args, **kwargs)
|
2020-05-18 11:15:52 +02:00
|
|
|
|
|
|
|
return wrap
|