2023-02-14 20:55:46 +00:00
|
|
|
import datetime
|
|
|
|
import uuid
|
|
|
|
|
2023-04-09 09:37:04 +00:00
|
|
|
from canaille.app import models
|
2023-04-09 13:52:55 +00:00
|
|
|
from canaille.app.flask import user_needed
|
2023-09-01 08:46:56 +00:00
|
|
|
from canaille.app.i18n import gettext as _
|
2023-08-16 15:14:11 +00:00
|
|
|
from canaille.app.themes import render_template
|
2021-12-20 22:57:27 +00:00
|
|
|
from flask import Blueprint
|
|
|
|
from flask import flash
|
|
|
|
from flask import redirect
|
|
|
|
from flask import url_for
|
2020-09-17 10:01:21 +00:00
|
|
|
|
2022-12-28 00:46:05 +00:00
|
|
|
from .utils import SCOPE_DETAILS
|
|
|
|
|
2020-09-17 10:01:21 +00:00
|
|
|
|
2022-01-11 18:49:06 +00:00
|
|
|
bp = Blueprint("consents", __name__, url_prefix="/consent")
|
2020-09-17 10:01:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/")
|
|
|
|
@user_needed()
|
|
|
|
def consents(user):
|
2023-04-09 09:37:04 +00:00
|
|
|
consents = models.Consent.query(subject=user)
|
2023-03-08 22:53:53 +00:00
|
|
|
clients = {t.client for t in consents}
|
2023-03-15 16:38:32 +00:00
|
|
|
|
|
|
|
nb_consents = len(consents)
|
|
|
|
nb_preconsents = sum(
|
2023-04-09 09:37:04 +00:00
|
|
|
1
|
|
|
|
for client in models.Client.query()
|
|
|
|
if client.preconsent and client not in clients
|
2023-03-15 16:38:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-14 13:28:20 +00:00
|
|
|
"consent_list.html",
|
2023-03-15 16:38:32 +00:00
|
|
|
consents=consents,
|
|
|
|
menuitem="consents",
|
|
|
|
scope_details=SCOPE_DETAILS,
|
|
|
|
ignored_scopes=["openid"],
|
|
|
|
nb_consents=nb_consents,
|
|
|
|
nb_preconsents=nb_preconsents,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/pre-consents")
|
|
|
|
@user_needed()
|
|
|
|
def pre_consents(user):
|
2023-04-09 09:37:04 +00:00
|
|
|
consents = models.Consent.query(subject=user)
|
2023-03-15 16:38:32 +00:00
|
|
|
clients = {t.client for t in consents}
|
2023-02-14 20:55:46 +00:00
|
|
|
preconsented = [
|
|
|
|
client
|
2023-04-09 09:37:04 +00:00
|
|
|
for client in models.Client.query()
|
2023-03-08 22:53:53 +00:00
|
|
|
if client.preconsent and client not in clients
|
2023-02-14 20:55:46 +00:00
|
|
|
]
|
|
|
|
|
2023-03-15 16:38:32 +00:00
|
|
|
nb_consents = len(consents)
|
|
|
|
nb_preconsents = len(preconsented)
|
|
|
|
|
2020-10-21 10:14:35 +00:00
|
|
|
return render_template(
|
2023-08-14 13:28:20 +00:00
|
|
|
"preconsent_list.html",
|
2022-01-11 18:49:06 +00:00
|
|
|
menuitem="consents",
|
2022-12-28 00:46:05 +00:00
|
|
|
scope_details=SCOPE_DETAILS,
|
|
|
|
ignored_scopes=["openid"],
|
2023-02-14 20:55:46 +00:00
|
|
|
preconsented=preconsented,
|
2023-03-15 16:38:32 +00:00
|
|
|
nb_consents=nb_consents,
|
|
|
|
nb_preconsents=nb_preconsents,
|
2020-10-21 10:14:35 +00:00
|
|
|
)
|
2020-09-17 10:01:21 +00:00
|
|
|
|
|
|
|
|
2023-06-29 10:15:12 +00:00
|
|
|
@bp.route("/revoke/<consent(required=False):consent>")
|
2020-09-17 10:01:21 +00:00
|
|
|
@user_needed()
|
2023-06-29 10:15:12 +00:00
|
|
|
def revoke(user, consent):
|
2023-03-08 22:53:53 +00:00
|
|
|
if not consent or consent.subject != user:
|
2023-02-14 17:43:43 +00:00
|
|
|
flash(_("Could not revoke this access"), "error")
|
|
|
|
|
|
|
|
elif consent.revokation_date:
|
|
|
|
flash(_("The access is already revoked"), "error")
|
2020-09-17 10:01:21 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
consent.revoke()
|
2023-02-14 17:43:43 +00:00
|
|
|
flash(_("The access has been revoked"), "success")
|
|
|
|
|
|
|
|
return redirect(url_for("oidc.consents.consents"))
|
|
|
|
|
|
|
|
|
2023-06-29 10:15:12 +00:00
|
|
|
@bp.route("/restore/<consent(required=False):consent>")
|
2023-02-14 17:43:43 +00:00
|
|
|
@user_needed()
|
2023-06-29 10:15:12 +00:00
|
|
|
def restore(user, consent):
|
2023-03-08 22:53:53 +00:00
|
|
|
if not consent or consent.subject != user:
|
2023-02-14 17:43:43 +00:00
|
|
|
flash(_("Could not restore this access"), "error")
|
|
|
|
|
|
|
|
elif not consent.revokation_date:
|
|
|
|
flash(_("The access is not revoked"), "error")
|
|
|
|
|
|
|
|
else:
|
|
|
|
consent.restore()
|
2023-02-14 20:55:46 +00:00
|
|
|
if not consent.issue_date:
|
2023-03-17 23:38:56 +00:00
|
|
|
consent.issue_date = datetime.datetime.now(datetime.timezone.utc)
|
2023-02-14 20:55:46 +00:00
|
|
|
consent.save()
|
2023-02-14 17:43:43 +00:00
|
|
|
flash(_("The access has been restored"), "success")
|
2020-09-17 10:01:21 +00:00
|
|
|
|
2022-01-11 18:49:06 +00:00
|
|
|
return redirect(url_for("oidc.consents.consents"))
|
2023-02-14 20:55:46 +00:00
|
|
|
|
|
|
|
|
2023-06-29 10:15:12 +00:00
|
|
|
@bp.route("/revoke-preconsent/<client(required=False):client>")
|
2023-02-14 20:55:46 +00:00
|
|
|
@user_needed()
|
2023-06-29 10:15:12 +00:00
|
|
|
def revoke_preconsent(user, client):
|
2023-02-14 20:55:46 +00:00
|
|
|
if not client or not client.preconsent:
|
|
|
|
flash(_("Could not revoke this access"), "error")
|
2023-02-14 21:06:03 +00:00
|
|
|
return redirect(url_for("oidc.consents.consents"))
|
2023-02-14 20:55:46 +00:00
|
|
|
|
2023-04-09 09:37:04 +00:00
|
|
|
consent = models.Consent.get(client=client, subject=user)
|
2023-02-14 21:06:03 +00:00
|
|
|
if consent:
|
2023-06-29 10:15:12 +00:00
|
|
|
return redirect(url_for("oidc.consents.revoke", consent=consent))
|
2023-02-14 20:55:46 +00:00
|
|
|
|
2023-04-09 09:37:04 +00:00
|
|
|
consent = models.Consent(
|
2023-05-17 06:54:13 +00:00
|
|
|
consent_id=str(uuid.uuid4()),
|
2023-03-08 22:53:53 +00:00
|
|
|
client=client,
|
|
|
|
subject=user,
|
2023-02-14 21:06:03 +00:00
|
|
|
scope=client.scope,
|
|
|
|
)
|
|
|
|
consent.revoke()
|
|
|
|
consent.save()
|
|
|
|
flash(_("The access has been revoked"), "success")
|
2023-02-14 20:55:46 +00:00
|
|
|
|
|
|
|
return redirect(url_for("oidc.consents.consents"))
|