diff --git a/canaille/oidc/endpoints/clients.py b/canaille/oidc/endpoints/clients.py index 34285a38..8f29ceca 100644 --- a/canaille/oidc/endpoints/clients.py +++ b/canaille/oidc/endpoints/clients.py @@ -2,6 +2,7 @@ import datetime from flask import Blueprint from flask import abort +from flask import current_app from flask import flash from flask import redirect from flask import request @@ -94,6 +95,9 @@ def edit(user, client): if request.form and request.form.get("action") == "delete": return client_delete(client) + if request.form and request.form.get("action") == "new-token": + return client_new_token(client) + return client_edit(client) @@ -154,3 +158,23 @@ def client_delete(client): ) Backend.instance.delete(client) return redirect(url_for("oidc.clients.index")) + + +def client_new_token(client): + flash( + _(f"A token have been created for the client {client.client_name}"), + "success", + ) + now = datetime.datetime.now(datetime.timezone.utc) + token = models.Token( + token_id=gen_salt(48), + type="access_token", + access_token=gen_salt(48), + issue_date=now, + lifetime=current_app.config["CANAILLE_OIDC"]["JWT"]["EXP"], + scope=client.scope, + client=client, + audience=client.audience, + ) + Backend.instance.save(token) + return redirect(url_for("oidc.tokens.view", token=token)) diff --git a/canaille/oidc/templates/client_edit.html b/canaille/oidc/templates/client_edit.html index 92f39b49..08dbcd4a 100644 --- a/canaille/oidc/templates/client_edit.html +++ b/canaille/oidc/templates/client_edit.html @@ -71,6 +71,9 @@
+ diff --git a/tests/oidc/test_client_admin.py b/tests/oidc/test_client_admin.py index 0ed02764..082c15e8 100644 --- a/tests/oidc/test_client_admin.py +++ b/tests/oidc/test_client_admin.py @@ -289,3 +289,22 @@ def test_client_edit_invalid_uri(testclient, client, logged_admin, trusted_clien "The client has not been edited. Please check your information.", ) in res.flashes res.mustcontain("This is not a valid URL") + + +def test_client_new_token(testclient, logged_admin, backend, client): + res = testclient.get("/admin/client/edit/" + client.client_id) + res = res.forms["clientaddform"].submit(name="action", value="new-token") + assert ( + "success", + "A token have been created for the client Some client", + ) in res.flashes + + token = backend.get(models.Token) + assert token.client == client + assert not token.subject + assert token.type == "access_token" + assert token.scope == client.scope + assert token.audience == client.audience + + res = res.follow() + assert res.template == "token_view.html"