logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

app_controller.ex (1771B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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.Maps
  11. alias Pleroma.Repo
  12. alias Pleroma.User
  13. alias Pleroma.Web.OAuth.App
  14. alias Pleroma.Web.OAuth.Scopes
  15. alias Pleroma.Web.OAuth.Token
  16. action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
  17. plug(:skip_auth when action in [:create, :verify_credentials])
  18. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  19. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.AppOperation
  20. @doc "POST /api/v1/apps"
  21. def create(%{body_params: params} = conn, _params) do
  22. scopes = Scopes.fetch_scopes(params, ["read"])
  23. user_id = get_user_id(conn)
  24. app_attrs =
  25. params
  26. |> Map.take([:client_name, :redirect_uris, :website])
  27. |> Map.put(:scopes, scopes)
  28. |> Maps.put_if_present(:user_id, user_id)
  29. with cs <- App.register_changeset(%App{}, app_attrs),
  30. {:ok, app} <- Repo.insert(cs) do
  31. render(conn, "show.json", app: app)
  32. end
  33. end
  34. defp get_user_id(%{assigns: %{user: %User{id: user_id}}}), do: user_id
  35. defp get_user_id(_conn), do: nil
  36. @doc """
  37. GET /api/v1/apps/verify_credentials
  38. Gets compact non-secret representation of the app. Supports app tokens and user tokens.
  39. """
  40. def verify_credentials(%{assigns: %{token: %Token{} = token}} = conn, _) do
  41. with %{app: %App{} = app} <- Repo.preload(token, :app) do
  42. render(conn, "compact_non_secret.json", app: app)
  43. end
  44. end
  45. end