From bcc8b1e2964f2763e9331d6260daa73b6651d0ea Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 5 Nov 2026 16:16:38 +0200 Subject: [PATCH] Mark currently accessing token in oauth token management API --- changelog.d/current_token.add | 1 + lib/pleroma/web/o_auth/token_controller.ex | 4 ++-- lib/pleroma/web/o_auth/token_view.ex | 5 +++-- .../pleroma/web/o_auth/token_controller_test.exs | 16 +++++++++++++++- 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 changelog.d/current_token.add diff --git a/changelog.d/current_token.add b/changelog.d/current_token.add new file mode 100644 index 000000000..f8004ef7b --- /dev/null +++ b/changelog.d/current_token.add @@ -0,0 +1 @@ +OAuth token list API now includes property to signify which token is being used to access the API diff --git a/lib/pleroma/web/o_auth/token_controller.ex b/lib/pleroma/web/o_auth/token_controller.ex index 296b60510..5c92307da 100644 --- a/lib/pleroma/web/o_auth/token_controller.ex +++ b/lib/pleroma/web/o_auth/token_controller.ex @@ -26,11 +26,11 @@ defmodule Pleroma.Web.OAuth.TokenController do end end - def oauth_tokens(%{assigns: %{user: user}} = conn, _params) do + def oauth_tokens(%{assigns: %{user: user, token: current_token}} = conn, _params) do with oauth_tokens <- Token.get_user_tokens(user) do conn |> put_view(TokenView) - |> render("index.json", %{tokens: oauth_tokens}) + |> render("index.json", %{tokens: oauth_tokens, token: current_token}) end end diff --git a/lib/pleroma/web/o_auth/token_view.ex b/lib/pleroma/web/o_auth/token_view.ex index f9894399a..053ad748a 100644 --- a/lib/pleroma/web/o_auth/token_view.ex +++ b/lib/pleroma/web/o_auth/token_view.ex @@ -11,12 +11,13 @@ defmodule Pleroma.Web.OAuth.TokenView do |> Enum.filter(&Enum.any?/1) end - def render("show.json", %{token: token_entry}) do + def render("show.json", %{token: token_entry, current_token: current_token}) do %{ id: token_entry.id, valid_until: token_entry.valid_until, app_name: token_entry.app.client_name, - scopes: token_entry.scopes + scopes: token_entry.scopes, + is_current_token: token_entry.id == current_token.id } end end diff --git a/test/pleroma/web/o_auth/token_controller_test.exs b/test/pleroma/web/o_auth/token_controller_test.exs index 08acf27bf..c408c7c32 100644 --- a/test/pleroma/web/o_auth/token_controller_test.exs +++ b/test/pleroma/web/o_auth/token_controller_test.exs @@ -62,7 +62,21 @@ defmodule Pleroma.Web.OAuth.TokenControllerTest do |> hd() |> Map.keys() - assert Enum.sort(keys) == Enum.sort(["id", "app_name", "valid_until", "scopes"]) + assert Enum.sort(keys) == Enum.sort(["id", "app_name", "valid_until", "scopes", "is_current_token"]) + end + + test "marks which token is being used for the request" do + %{conn: conn, user: user, token: current} = oauth_access(["read:security"]) + other = insert(:oauth_token, user: user) + + tokens = + conn + |> get("/api/oauth_tokens") + |> json_response(200) + |> Map.new(&{&1["id"], &1["is_current_token"]}) + + assert tokens[current.id] == true + assert tokens[other.id] == false end test "is allowed with the read scope" do