Mark currently accessing token in oauth token management API

This commit is contained in:
Henry Jameson 2026-11-05 16:16:38 +02:00
commit bcc8b1e296
4 changed files with 21 additions and 5 deletions

View file

@ -0,0 +1 @@
OAuth token list API now includes property to signify which token is being used to access the API

View file

@ -26,11 +26,11 @@ defmodule Pleroma.Web.OAuth.TokenController do
end end
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 with oauth_tokens <- Token.get_user_tokens(user) do
conn conn
|> put_view(TokenView) |> put_view(TokenView)
|> render("index.json", %{tokens: oauth_tokens}) |> render("index.json", %{tokens: oauth_tokens, token: current_token})
end end
end end

View file

@ -11,12 +11,13 @@ defmodule Pleroma.Web.OAuth.TokenView do
|> Enum.filter(&Enum.any?/1) |> Enum.filter(&Enum.any?/1)
end 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, id: token_entry.id,
valid_until: token_entry.valid_until, valid_until: token_entry.valid_until,
app_name: token_entry.app.client_name, 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
end end

View file

@ -62,7 +62,21 @@ defmodule Pleroma.Web.OAuth.TokenControllerTest do
|> hd() |> hd()
|> Map.keys() |> 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 end
test "is allowed with the read scope" do test "is allowed with the read scope" do