079382b174a414f76155dd5a240d24b4ae665f4f
[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.Maps
14 alias Pleroma.Repo
15 alias Pleroma.User
16 alias Pleroma.Web.OAuth.App
17 alias Pleroma.Web.OAuth.Scopes
18 alias Pleroma.Web.OAuth.Token
19
20 require Logger
21
22 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
23
24 plug(:skip_auth when action in [:create, :verify_credentials])
25
26 plug(Pleroma.Web.ApiSpec.CastAndValidate)
27
28 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
29
30 @doc "POST /api/v1/apps"
31 def create(%{assigns: %{user: user}, body_params: params} = conn, _params) do
32 scopes = Scopes.fetch_scopes(params, ["read"])
33
34 user_id =
35 with %User{id: id} <- user do
36 id
37 else
38 _ -> nil
39 end
40
41 app_attrs =
42 params
43 |> Map.take([:client_name, :redirect_uris, :website])
44 |> Map.put(:scopes, scopes)
45 |> Maps.put_if_present(:user_id, user_id)
46
47 with cs <- App.register_changeset(%App{}, app_attrs),
48 {:ok, app} <- Repo.insert(cs) do
49 render(conn, "show.json", app: app)
50 end
51 end
52
53 @doc """
54 GET /api/v1/apps/verify_credentials
55 Gets compact non-secret representation of the app. Supports app tokens and user tokens.
56 """
57 def verify_credentials(%{assigns: %{token: %Token{} = token}} = conn, _) do
58 with %{app: %App{} = app} <- Repo.preload(token, :app) do
59 render(conn, "compact_non_secret.json", app: app)
60 end
61 end
62 end