GET /api/v1/apps endpoint

This commit is contained in:
Sean King 2021-08-25 21:01:04 -06:00
parent 84ec0fbeaa
commit 6519732045
No known key found for this signature in database
GPG Key ID: 510C52BACD6E7257
6 changed files with 73 additions and 0 deletions

View File

@ -13,6 +13,19 @@ defmodule Pleroma.Web.ApiSpec.AppOperation do
apply(__MODULE__, operation, []) apply(__MODULE__, operation, [])
end end
@spec index_operation() :: Operation.t()
def index_operation do
%Operation{
tags: ["Applications"],
summary: "List applications",
description: "List the OAuth applications for the current user",
operationId: "AppController.index",
responses: %{
200 => Operation.response("App", "application/json", index_response()),
}
}
end
@spec create_operation() :: Operation.t() @spec create_operation() :: Operation.t()
def create_operation do def create_operation do
%Operation{ %Operation{
@ -145,4 +158,30 @@ defmodule Pleroma.Web.ApiSpec.AppOperation do
} }
} }
end end
defp index_response do
%Schema{
title: "AppIndexResponse",
description: "Response schema for GET /api/v1/apps",
type: :object,
properties: [%{
id: %Schema{type: :string},
name: %Schema{type: :string},
client_id: %Schema{type: :string},
client_secret: %Schema{type: :string},
redirect_uri: %Schema{type: :string},
vapid_key: %Schema{type: :string},
website: %Schema{type: :string, nullable: true}
}],
example: [%{
"id" => "123",
"name" => "My App",
"client_id" => "TWhM-tNSuncnqN7DBJmoyeLnk6K3iJJ71KKXxgL1hPM",
"client_secret" => "ZEaFUFmF0umgBX1qKJDjaU99Q31lDkOU8NutzTOoliw",
"vapid_key" =>
"BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=",
"website" => "https://myapp.com/"
}]
}
end
end end

View File

@ -14,17 +14,27 @@ defmodule Pleroma.Web.MastodonAPI.AppController do
alias Pleroma.Web.OAuth.App alias Pleroma.Web.OAuth.App
alias Pleroma.Web.OAuth.Scopes alias Pleroma.Web.OAuth.Scopes
alias Pleroma.Web.OAuth.Token alias Pleroma.Web.OAuth.Token
alias Pleroma.Web.Plugs.OAuthScopesPlug
action_fallback(Pleroma.Web.MastodonAPI.FallbackController) action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
plug(:skip_auth when action in [:create, :verify_credentials]) plug(:skip_auth when action in [:create, :verify_credentials])
plug(:skip_plug, OAuthScopesPlug when action in [:index])
plug(Pleroma.Web.ApiSpec.CastAndValidate) plug(Pleroma.Web.ApiSpec.CastAndValidate)
@local_mastodon_name "Mastodon-Local" @local_mastodon_name "Mastodon-Local"
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
@doc "GET /api/v1/apps"
def index(%{assigns: %{user: user}} = conn, _params) do
with apps <- App.get_user_apps(user) do
render(conn, "index.json", %{apps: apps})
end
end
@doc "POST /api/v1/apps" @doc "POST /api/v1/apps"
def create(%{body_params: params} = conn, _params) do def create(%{body_params: params} = conn, _params) do
scopes = Scopes.fetch_scopes(params, ["read"]) scopes = Scopes.fetch_scopes(params, ["read"])

View File

@ -15,6 +15,10 @@ defmodule Pleroma.Web.MastodonAPI.AppView do
} }
end end
def render("index.json", %{apps: apps}) do
render_many(apps, Pleroma.Web.MastodonAPI.AppView, "show.json")
end
def render("show.json", %{admin: true, app: %App{} = app} = assigns) do def render("show.json", %{admin: true, app: %App{} = app} = assigns) do
"show.json" "show.json"
|> render(Map.delete(assigns, :admin)) |> render(Map.delete(assigns, :admin))

View File

@ -7,6 +7,7 @@ defmodule Pleroma.Web.OAuth.App do
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query import Ecto.Query
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User
@type t :: %__MODULE__{} @type t :: %__MODULE__{}
@ -19,6 +20,8 @@ defmodule Pleroma.Web.OAuth.App do
field(:client_secret, :string) field(:client_secret, :string)
field(:trusted, :boolean, default: false) field(:trusted, :boolean, default: false)
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
has_many(:oauth_authorizations, Pleroma.Web.OAuth.Authorization, on_delete: :delete_all) has_many(:oauth_authorizations, Pleroma.Web.OAuth.Authorization, on_delete: :delete_all)
has_many(:oauth_tokens, Pleroma.Web.OAuth.Token, on_delete: :delete_all) has_many(:oauth_tokens, Pleroma.Web.OAuth.Token, on_delete: :delete_all)
@ -129,6 +132,12 @@ defmodule Pleroma.Web.OAuth.App do
{:ok, Repo.all(query), count} {:ok, Repo.all(query), count}
end end
@spec get_user_apps(User.t()) :: {:ok, [t()], non_neg_integer()}
def get_user_apps(%User{id: user_id}) do
from(a in __MODULE__, where: a.user_id == ^user_id)
|> Repo.all()
end
@spec destroy(pos_integer()) :: {:ok, t()} | {:error, Ecto.Changeset.t()} @spec destroy(pos_integer()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
def destroy(id) do def destroy(id) do
with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do

View File

@ -444,6 +444,8 @@ defmodule Pleroma.Web.Router do
scope "/api/v1", Pleroma.Web.MastodonAPI do scope "/api/v1", Pleroma.Web.MastodonAPI do
pipe_through(:authenticated_api) pipe_through(:authenticated_api)
get("/apps", AppController, :index)
get("/accounts/verify_credentials", AccountController, :verify_credentials) get("/accounts/verify_credentials", AccountController, :verify_credentials)
patch("/accounts/update_credentials", AccountController, :update_credentials) patch("/accounts/update_credentials", AccountController, :update_credentials)

View File

@ -0,0 +1,9 @@
defmodule Pleroma.Repo.Migrations.AddUserIdToApps do
use Ecto.Migration
def change do
alter table(:apps) do
add(:user_id, references(:users, type: :uuid, on_delete: :delete_all))
end
end
end