canaille-globuzma/canaille/oidc/consents.py

106 lines
2.8 KiB
Python
Raw Normal View History

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
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
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):
consents = Consent.filter(subject=user.dn)
client_dns = {t.client for t in consents}
2020-09-17 10:01:21 +00:00
clients = {dn: Client.get(dn) for dn in client_dns}
preconsented = [
client
for client in Client.filter()
if client.preconsent and client.dn not in clients
]
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,
clients=clients,
menuitem="consents",
scope_details=SCOPE_DETAILS,
ignored_scopes=["openid"],
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)
if not consent or consent.subject != user.dn:
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)
if not consent or consent.subject != user.dn:
flash(_("Could not restore this access"), "error")
elif not consent.revokation_date:
flash(_("The access is not revoked"), "error")
else:
consent.restore()
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"))
@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")
return redirect(url_for("oidc.consents.consents"))
consent = Consent.get(client=client.dn, subject=user.dn)
if consent:
return redirect(url_for("oidc.consents.revoke", consent_id=consent.cn[0]))
consent = Consent(
cn=str(uuid.uuid4()),
client=client.dn,
subject=user.dn,
scope=client.scope,
)
consent.revoke()
consent.save()
flash(_("The access has been revoked"), "success")
return redirect(url_for("oidc.consents.consents"))