2023-02-14 20:55:46 +00:00
|
|
|
import datetime
|
|
|
|
import uuid
|
|
|
|
|
2020-10-21 12:04:40 +00:00
|
|
|
from canaille.flaskutils import user_needed
|
2022-01-11 18:49:06 +00:00
|
|
|
from canaille.oidc.models import Client
|
|
|
|
from canaille.oidc.models import Consent
|
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
|
2023-02-14 20:55:46 +00:00
|
|
|
from flask_babel import gettext as _
|
2021-12-20 22:57:27 +00:00
|
|
|
from flask_themer import render_template
|
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-03-08 22:53:53 +00:00
|
|
|
consents = Consent.query(subject=user)
|
|
|
|
clients = {t.client for t in consents}
|
2023-02-14 20:55:46 +00:00
|
|
|
preconsented = [
|
|
|
|
client
|
2023-03-07 13:49:44 +00:00
|
|
|
for client in 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
|
|
|
]
|
|
|
|
|
2020-10-21 10:14:35 +00:00
|
|
|
return render_template(
|
2022-01-11 18:49:06 +00:00
|
|
|
"oidc/user/consent_list.html",
|
|
|
|
consents=consents,
|
|
|
|
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,
|
2020-10-21 10:14:35 +00:00
|
|
|
)
|
2020-09-17 10:01:21 +00:00
|
|
|
|
|
|
|
|
2023-02-14 17:43:43 +00:00
|
|
|
@bp.route("/revoke/<consent_id>")
|
2020-09-17 10:01:21 +00:00
|
|
|
@user_needed()
|
2023-02-14 17:43:43 +00:00
|
|
|
def revoke(user, consent_id):
|
2020-09-17 10:01:21 +00:00
|
|
|
consent = Consent.get(consent_id)
|
|
|
|
|
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"))
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/restore/<consent_id>")
|
|
|
|
@user_needed()
|
|
|
|
def restore(user, consent_id):
|
|
|
|
consent = Consent.get(consent_id)
|
|
|
|
|
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:
|
|
|
|
consent.issue_date = datetime.datetime.now()
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/revoke-preconsent/<client_id>")
|
|
|
|
@user_needed()
|
|
|
|
def revoke_preconsent(user, client_id):
|
|
|
|
client = Client.get(client_id)
|
|
|
|
|
|
|
|
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-03-08 22:53:53 +00:00
|
|
|
consent = Consent.get(client=client, subject=user)
|
2023-02-14 21:06:03 +00:00
|
|
|
if consent:
|
2023-03-09 23:38:16 +00:00
|
|
|
return redirect(
|
|
|
|
url_for("oidc.consents.revoke", consent_id=consent.consent_id[0])
|
|
|
|
)
|
2023-02-14 20:55:46 +00:00
|
|
|
|
2023-02-14 21:06:03 +00:00
|
|
|
consent = Consent(
|
|
|
|
cn=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"))
|