app_controller.ex 1.5 KB
Newer Older
1
# Pleroma: A lightweight social networking server
Haelwenn's avatar
Haelwenn committed
2
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3
4
5
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MastodonAPI.AppController do
6
7
8
9
10
  @moduledoc """
  Controller for supporting app-related actions.
  If authentication is an option, app tokens (user-unbound) must be supported.
  """

11
12
13
14
15
16
17
18
19
  use Pleroma.Web, :controller

  alias Pleroma.Repo
  alias Pleroma.Web.OAuth.App
  alias Pleroma.Web.OAuth.Scopes
  alias Pleroma.Web.OAuth.Token

  action_fallback(Pleroma.Web.MastodonAPI.FallbackController)

20
  plug(:skip_auth when action in [:create, :verify_credentials])
21

minibikini's avatar
minibikini committed
22
  plug(Pleroma.Web.ApiSpec.CastAndValidate)
23

minibikini's avatar
minibikini committed
24
25
  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation

26
  @doc "POST /api/v1/apps"
minibikini's avatar
minibikini committed
27
  def create(%{body_params: params} = conn, _params) do
28
29
30
31
    scopes = Scopes.fetch_scopes(params, ["read"])

    app_attrs =
      params
minibikini's avatar
minibikini committed
32
33
      |> Map.take([:client_name, :redirect_uris, :website])
      |> Map.put(:scopes, scopes)
34
35
36
37
38
39
40

    with cs <- App.register_changeset(%App{}, app_attrs),
         {:ok, app} <- Repo.insert(cs) do
      render(conn, "show.json", app: app)
    end
  end

41
42
43
44
45
46
47
  @doc """
  GET /api/v1/apps/verify_credentials
  Gets compact non-secret representation of the app. Supports app tokens and user tokens.
  """
  def verify_credentials(%{assigns: %{token: %Token{} = token}} = conn, _) do
    with %{app: %App{} = app} <- Repo.preload(token, :app) do
      render(conn, "compact_non_secret.json", app: app)
48
49
50
    end
  end
end