1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AppController do
7 Controller for supporting app-related actions.
8 If authentication is an option, app tokens (user-unbound) must be supported.
11 use Pleroma.Web, :controller
16 alias Pleroma.Web.OAuth.App
17 alias Pleroma.Web.OAuth.Scopes
18 alias Pleroma.Web.OAuth.Token
20 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
22 plug(:skip_auth when action in [:create, :verify_credentials])
24 plug(Pleroma.Web.ApiSpec.CastAndValidate)
26 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
28 @doc "POST /api/v1/apps"
29 def create(%{body_params: params} = conn, _params) do
30 scopes = Scopes.fetch_scopes(params, ["read"])
31 user_id = get_user_id(conn)
35 |> Map.take([:client_name, :redirect_uris, :website])
36 |> Map.put(:scopes, scopes)
37 |> Maps.put_if_present(:user_id, user_id)
39 with cs <- App.register_changeset(%App{}, app_attrs),
40 {:ok, app} <- Repo.insert(cs) do
41 render(conn, "show.json", app: app)
45 defp get_user_id(%{assigns: %{user: %User{id: user_id}}}), do: user_id
46 defp get_user_id(_conn), do: nil
49 GET /api/v1/apps/verify_credentials
50 Gets compact non-secret representation of the app. Supports app tokens and user tokens.
52 def verify_credentials(%{assigns: %{token: %Token{} = token}} = conn, _) do
53 with %{app: %App{} = app} <- Repo.preload(token, :app) do
54 render(conn, "compact_non_secret.json", app: app)