Merge remote-tracking branch 'upstream/develop' into earmark
[akkoma] / lib / pleroma / web / mastodon_api / controllers / app_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AppController do
6 @moduledoc """
7 Controller for supporting app-related actions.
8 If authentication is an option, app tokens (user-unbound) must be supported.
9 """
10
11 use Pleroma.Web, :controller
12
13 alias Pleroma.Repo
14 alias Pleroma.Web.OAuth.App
15 alias Pleroma.Web.OAuth.Scopes
16 alias Pleroma.Web.OAuth.Token
17 alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
18 alias Pleroma.Web.Plugs.OAuthScopesPlug
19
20 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
21
22 plug(
23 :skip_plug,
24 [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug]
25 when action in [:create, :verify_credentials]
26 )
27
28 plug(Pleroma.Web.ApiSpec.CastAndValidate)
29
30 @local_mastodon_name "Mastodon-Local"
31
32 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
33
34 @doc "POST /api/v1/apps"
35 def create(%{body_params: params} = conn, _params) do
36 scopes = Scopes.fetch_scopes(params, ["read"])
37
38 app_attrs =
39 params
40 |> Map.take([:client_name, :redirect_uris, :website])
41 |> Map.put(:scopes, scopes)
42
43 with cs <- App.register_changeset(%App{}, app_attrs),
44 false <- cs.changes[:client_name] == @local_mastodon_name,
45 {:ok, app} <- Repo.insert(cs) do
46 render(conn, "show.json", app: app)
47 end
48 end
49
50 @doc """
51 GET /api/v1/apps/verify_credentials
52 Gets compact non-secret representation of the app. Supports app tokens and user tokens.
53 """
54 def verify_credentials(%{assigns: %{token: %Token{} = token}} = conn, _) do
55 with %{app: %App{} = app} <- Repo.preload(token, :app) do
56 render(conn, "compact_non_secret.json", app: app)
57 end
58 end
59 end