2020-08-17 13:49:48 +00:00
|
|
|
import datetime
|
2021-12-20 22:57:27 +00:00
|
|
|
|
|
|
|
from canaille.flaskutils import permissions_needed
|
2023-03-09 16:41:26 +00:00
|
|
|
from canaille.flaskutils import render_htmx_template
|
2023-02-25 17:11:19 +00:00
|
|
|
from canaille.forms import TableForm
|
2022-12-15 17:00:45 +00:00
|
|
|
from canaille.oidc.forms import ClientAdd
|
2022-01-11 18:49:06 +00:00
|
|
|
from canaille.oidc.models import Client
|
2021-12-20 22:57:27 +00:00
|
|
|
from flask import abort
|
|
|
|
from flask import Blueprint
|
|
|
|
from flask import flash
|
|
|
|
from flask import redirect
|
|
|
|
from flask import request
|
|
|
|
from flask import url_for
|
2022-12-15 17:00:45 +00:00
|
|
|
from flask_babel import gettext as _
|
2021-10-28 13:24:34 +00:00
|
|
|
from flask_themer import render_template
|
2020-08-17 13:49:48 +00:00
|
|
|
from werkzeug.security import gen_salt
|
|
|
|
|
|
|
|
|
2022-01-11 18:49:06 +00:00
|
|
|
bp = Blueprint("clients", __name__, url_prefix="/admin/client")
|
2020-08-17 13:49:48 +00:00
|
|
|
|
|
|
|
|
2023-02-25 17:11:19 +00:00
|
|
|
@bp.route("/", methods=["GET", "POST"])
|
2021-12-02 17:23:14 +00:00
|
|
|
@permissions_needed("manage_oidc")
|
2020-10-29 10:09:31 +00:00
|
|
|
def index(user):
|
2023-02-25 17:11:19 +00:00
|
|
|
table_form = TableForm(Client, formdata=request.form)
|
|
|
|
if request.form and request.form.get("page") and not table_form.validate():
|
|
|
|
abort(404)
|
|
|
|
|
2023-03-09 16:41:26 +00:00
|
|
|
return render_htmx_template(
|
2023-02-25 17:11:19 +00:00
|
|
|
"oidc/admin/client_list.html", menuitem="admin", table_form=table_form
|
2022-01-11 18:49:06 +00:00
|
|
|
)
|
2020-08-17 13:49:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/add", methods=["GET", "POST"])
|
2021-12-02 17:23:14 +00:00
|
|
|
@permissions_needed("manage_oidc")
|
2020-10-29 10:09:31 +00:00
|
|
|
def add(user):
|
2020-08-17 13:49:48 +00:00
|
|
|
form = ClientAdd(request.form or None)
|
|
|
|
|
|
|
|
if not request.form:
|
2022-01-11 18:49:06 +00:00
|
|
|
return render_template(
|
|
|
|
"oidc/admin/client_add.html", form=form, menuitem="admin"
|
|
|
|
)
|
2020-08-17 13:49:48 +00:00
|
|
|
|
|
|
|
if not form.validate():
|
|
|
|
flash(
|
2021-10-13 09:52:02 +00:00
|
|
|
_("The client has not been added. Please check your information."),
|
|
|
|
"error",
|
2020-08-17 13:49:48 +00:00
|
|
|
)
|
2022-01-11 18:49:06 +00:00
|
|
|
return render_template(
|
|
|
|
"oidc/admin/client_add.html", form=form, menuitem="admin"
|
|
|
|
)
|
2020-08-17 13:49:48 +00:00
|
|
|
|
|
|
|
client_id = gen_salt(24)
|
2021-12-08 14:53:20 +00:00
|
|
|
client_id_issued_at = datetime.datetime.now()
|
2020-08-17 13:49:48 +00:00
|
|
|
client = Client(
|
2022-01-11 16:57:58 +00:00
|
|
|
client_id=client_id,
|
2022-10-17 15:49:52 +00:00
|
|
|
client_id_issued_at=client_id_issued_at,
|
|
|
|
client_name=form["client_name"].data,
|
|
|
|
contacts=[form["contacts"].data],
|
|
|
|
client_uri=form["client_uri"].data,
|
|
|
|
grant_types=form["grant_types"].data,
|
2022-01-11 16:57:58 +00:00
|
|
|
redirect_uris=[form["redirect_uris"].data],
|
2022-05-20 12:07:56 +00:00
|
|
|
post_logout_redirect_uris=[form["post_logout_redirect_uris"].data],
|
2022-10-17 15:49:52 +00:00
|
|
|
response_types=form["response_types"].data,
|
2022-01-11 16:57:58 +00:00
|
|
|
scope=form["scope"].data.split(" "),
|
|
|
|
token_endpoint_auth_method=form["token_endpoint_auth_method"].data,
|
|
|
|
logo_uri=form["logo_uri"].data,
|
|
|
|
tos_uri=form["tos_uri"].data,
|
|
|
|
policy_uri=form["policy_uri"].data,
|
|
|
|
software_id=form["software_id"].data,
|
|
|
|
software_version=form["software_version"].data,
|
|
|
|
jwk=form["jwk"].data,
|
2022-10-17 15:49:52 +00:00
|
|
|
jwks_uri=form["jwks_uri"].data,
|
2022-01-11 16:57:58 +00:00
|
|
|
preconsent=form["preconsent"].data,
|
2022-10-17 15:49:52 +00:00
|
|
|
client_secret=""
|
2022-01-11 16:57:58 +00:00
|
|
|
if form["token_endpoint_auth_method"].data == "none"
|
2020-08-17 13:49:48 +00:00
|
|
|
else gen_salt(48),
|
|
|
|
)
|
2023-03-08 22:53:53 +00:00
|
|
|
client.audience = [client]
|
2020-08-17 13:49:48 +00:00
|
|
|
client.save()
|
|
|
|
flash(
|
2021-10-13 09:52:02 +00:00
|
|
|
_("The client has been created."),
|
|
|
|
"success",
|
2020-08-17 13:49:48 +00:00
|
|
|
)
|
|
|
|
|
2022-01-11 18:49:06 +00:00
|
|
|
return redirect(url_for("oidc.clients.edit", client_id=client_id))
|
2020-08-17 13:49:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/edit/<client_id>", methods=["GET", "POST"])
|
2021-12-02 17:23:14 +00:00
|
|
|
@permissions_needed("manage_oidc")
|
2020-10-29 10:09:31 +00:00
|
|
|
def edit(user, client_id):
|
2020-11-23 16:32:40 +00:00
|
|
|
if request.method == "GET" or request.form.get("action") == "edit":
|
|
|
|
return client_edit(client_id)
|
|
|
|
|
|
|
|
if request.form.get("action") == "delete":
|
|
|
|
return client_delete(client_id)
|
|
|
|
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
|
|
|
|
def client_edit(client_id):
|
2022-04-04 15:49:50 +00:00
|
|
|
client = Client.get(client_id)
|
|
|
|
|
|
|
|
if not client:
|
|
|
|
abort(404)
|
|
|
|
|
2020-08-17 13:49:48 +00:00
|
|
|
data = dict(client)
|
2022-01-11 16:57:58 +00:00
|
|
|
data["scope"] = " ".join(data["scope"])
|
2022-12-13 18:14:25 +00:00
|
|
|
data["redirect_uris"] = data["redirect_uris"][0] if data["redirect_uris"] else ""
|
|
|
|
data["contacts"] = data["contacts"][0] if data["contacts"] else ""
|
2022-05-20 12:07:56 +00:00
|
|
|
data["post_logout_redirect_uris"] = (
|
|
|
|
data["post_logout_redirect_uris"][0]
|
|
|
|
if data["post_logout_redirect_uris"]
|
|
|
|
else ""
|
|
|
|
)
|
2022-01-11 16:57:58 +00:00
|
|
|
data["preconsent"] = client.preconsent
|
2020-08-17 13:49:48 +00:00
|
|
|
form = ClientAdd(request.form or None, data=data, client=client)
|
|
|
|
|
|
|
|
if not request.form:
|
2020-10-21 10:14:35 +00:00
|
|
|
return render_template(
|
2022-01-11 18:49:06 +00:00
|
|
|
"oidc/admin/client_edit.html", form=form, client=client, menuitem="admin"
|
2020-10-21 10:14:35 +00:00
|
|
|
)
|
2020-08-17 13:49:48 +00:00
|
|
|
|
|
|
|
if not form.validate():
|
|
|
|
flash(
|
2020-10-28 14:49:14 +00:00
|
|
|
_("The client has not been edited. Please check your information."),
|
2020-08-17 13:49:48 +00:00
|
|
|
"error",
|
|
|
|
)
|
2022-11-16 16:50:38 +00:00
|
|
|
return render_template(
|
|
|
|
"oidc/admin/client_edit.html", form=form, client=client, menuitem="admin"
|
2020-08-17 13:49:48 +00:00
|
|
|
)
|
|
|
|
|
2022-11-16 16:50:38 +00:00
|
|
|
client.update(
|
|
|
|
client_name=form["client_name"].data,
|
|
|
|
contacts=[form["contacts"].data],
|
|
|
|
client_uri=form["client_uri"].data,
|
|
|
|
grant_types=form["grant_types"].data,
|
|
|
|
redirect_uris=[form["redirect_uris"].data],
|
|
|
|
post_logout_redirect_uris=[form["post_logout_redirect_uris"].data],
|
|
|
|
response_types=form["response_types"].data,
|
|
|
|
scope=form["scope"].data.split(" "),
|
|
|
|
token_endpoint_auth_method=form["token_endpoint_auth_method"].data,
|
|
|
|
logo_uri=form["logo_uri"].data,
|
|
|
|
tos_uri=form["tos_uri"].data,
|
|
|
|
policy_uri=form["policy_uri"].data,
|
|
|
|
software_id=form["software_id"].data,
|
|
|
|
software_version=form["software_version"].data,
|
|
|
|
jwk=form["jwk"].data,
|
|
|
|
jwks_uri=form["jwks_uri"].data,
|
2023-03-08 22:53:53 +00:00
|
|
|
audience=[Client.get(dn=dn) for dn in form["audience"].data],
|
2022-11-16 16:50:38 +00:00
|
|
|
preconsent=form["preconsent"].data,
|
2020-10-21 10:14:35 +00:00
|
|
|
)
|
2022-11-16 16:50:38 +00:00
|
|
|
client.save()
|
|
|
|
flash(
|
|
|
|
_("The client has been edited."),
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
return redirect(url_for("oidc.clients.edit", client_id=client_id))
|
2020-11-23 16:32:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def client_delete(client_id):
|
2022-04-04 15:49:50 +00:00
|
|
|
client = Client.get(client_id)
|
|
|
|
|
|
|
|
if not client:
|
|
|
|
abort(404)
|
|
|
|
|
2020-11-23 16:32:40 +00:00
|
|
|
flash(
|
2021-10-13 09:52:02 +00:00
|
|
|
_("The client has been deleted."),
|
|
|
|
"success",
|
2020-11-23 16:32:40 +00:00
|
|
|
)
|
|
|
|
client.delete()
|
2022-01-11 18:49:06 +00:00
|
|
|
return redirect(url_for("oidc.clients.index"))
|