Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.6KB

  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. defmodule Pleroma.Web.MastodonAPI.AppController do
  5. @moduledoc """
  6. Controller for supporting app-related actions.
  7. If authentication is an option, app tokens (user-unbound) must be supported.
  8. """
  9. use Pleroma.Web, :controller
  10. alias Pleroma.Repo
  11. alias Pleroma.Web.OAuth.App
  12. alias Pleroma.Web.OAuth.Scopes
  13. alias Pleroma.Web.OAuth.Token
  14. action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
  15. plug(:skip_auth when action in [:create, :verify_credentials])
  16. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  17. @local_mastodon_name "Mastodon-Local"
  18. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
  19. @doc "POST /api/v1/apps"
  20. def create(%{body_params: params} = conn, _params) do
  21. scopes = Scopes.fetch_scopes(params, ["read"])
  22. app_attrs =
  23. params
  24. |> Map.take([:client_name, :redirect_uris, :website])
  25. |> Map.put(:scopes, scopes)
  26. with cs <- App.register_changeset(%App{}, app_attrs),
  27. false <- cs.changes[:client_name] == @local_mastodon_name,
  28. {:ok, app} <- Repo.insert(cs) do
  29. render(conn, "show.json", app: app)
  30. end
  31. end
  32. @doc """
  33. GET /api/v1/apps/verify_credentials
  34. Gets compact non-secret representation of the app. Supports app tokens and user tokens.
  35. """
  36. def verify_credentials(%{assigns: %{token: %Token{} = token}} = conn, _) do
  37. with %{app: %App{} = app} <- Repo.preload(token, :app) do
  38. render(conn, "compact_non_secret.json", app: app)
  39. end
  40. end
  41. end