1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AppController do
6 use Pleroma.Web, :controller
8 alias Pleroma.Plugs.OAuthScopesPlug
10 alias Pleroma.Web.OAuth.App
11 alias Pleroma.Web.OAuth.Scopes
12 alias Pleroma.Web.OAuth.Token
14 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
16 plug(OAuthScopesPlug, %{scopes: ["read"]} when action == :verify_credentials)
17 plug(OpenApiSpex.Plug.CastAndValidate)
19 @local_mastodon_name "Mastodon-Local"
21 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
23 @doc "POST /api/v1/apps"
24 def create(%{body_params: params} = conn, _params) do
25 scopes = Scopes.fetch_scopes(params, ["read"])
29 |> Map.take([:client_name, :redirect_uris, :website])
30 |> Map.put(:scopes, scopes)
32 with cs <- App.register_changeset(%App{}, app_attrs),
33 false <- cs.changes[:client_name] == @local_mastodon_name,
34 {:ok, app} <- Repo.insert(cs) do
35 render(conn, "show.json", app: app)
39 @doc "GET /api/v1/apps/verify_credentials"
40 def verify_credentials(%{assigns: %{user: _user, token: token}} = conn, _) do
41 with %Token{app: %App{} = app} <- Repo.preload(token, :app) do
42 render(conn, "short.json", app: app)