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)
18 @local_mastodon_name "Mastodon-Local"
20 @doc "POST /api/v1/apps"
21 def create(conn, params) do
22 scopes = Scopes.fetch_scopes(params, ["read"])
26 |> Map.drop(["scope", "scopes"])
27 |> Map.put("scopes", scopes)
29 with cs <- App.register_changeset(%App{}, app_attrs),
30 false <- cs.changes[:client_name] == @local_mastodon_name,
31 {:ok, app} <- Repo.insert(cs) do
32 render(conn, "show.json", app: app)
36 @doc "GET /api/v1/apps/verify_credentials"
37 def verify_credentials(%{assigns: %{user: _user, token: token}} = conn, _) do
38 with %Token{app: %App{} = app} <- Repo.preload(token, :app) do
39 render(conn, "short.json", app: app)