logo

pleroma

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

app_controller.ex (1862B)


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