logo

pleroma

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

revoke.ex (1115B)


  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.OAuth.Token.Strategy.Revoke do
  5. @moduledoc """
  6. Functions for dealing with revocation.
  7. """
  8. alias Pleroma.Repo
  9. alias Pleroma.Web.OAuth.App
  10. alias Pleroma.Web.OAuth.Token
  11. @doc "Finds and revokes access token for app and by token"
  12. @spec revoke(App.t(), map()) :: {:ok, Token.t()} | {:error, :not_found | Ecto.Changeset.t()}
  13. def revoke(%App{} = app, %{"token" => token} = _attrs) do
  14. with {:ok, token} <- Token.get_by_token(app, token),
  15. do: revoke(token)
  16. end
  17. @doc "Revokes access token"
  18. @spec revoke(Token.t()) :: {:ok, Token.t()} | {:error, Ecto.Changeset.t()}
  19. def revoke(%Token{} = token) do
  20. with {:ok, token} <- Repo.delete(token) do
  21. Task.Supervisor.start_child(
  22. Pleroma.TaskSupervisor,
  23. Pleroma.Web.Streamer,
  24. :close_streams_by_oauth_token,
  25. [token],
  26. restart: :transient
  27. )
  28. {:ok, token}
  29. else
  30. result -> result
  31. end
  32. end
  33. end