2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-05-06 11:51:03 -06:00
|
|
|
defmodule Pleroma.Web.OAuth.Token.Strategy.Revoke do
|
|
|
|
@moduledoc """
|
|
|
|
Functions for dealing with revocation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.Web.OAuth.App
|
|
|
|
alias Pleroma.Web.OAuth.Token
|
|
|
|
|
|
|
|
@doc "Finds and revokes access token for app and by token"
|
|
|
|
@spec revoke(App.t(), map()) :: {:ok, Token.t()} | {:error, :not_found | Ecto.Changeset.t()}
|
|
|
|
def revoke(%App{} = app, %{"token" => token} = _attrs) do
|
|
|
|
with {:ok, token} <- Token.get_by_token(app, token),
|
|
|
|
do: revoke(token)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "Revokes access token"
|
|
|
|
@spec revoke(Token.t()) :: {:ok, Token.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def revoke(%Token{} = token) do
|
2022-08-19 12:58:57 -06:00
|
|
|
with {:ok, token} <- Repo.delete(token) do
|
|
|
|
Task.Supervisor.start_child(
|
|
|
|
Pleroma.TaskSupervisor,
|
|
|
|
Pleroma.Web.Streamer,
|
|
|
|
:close_streams_by_oauth_token,
|
|
|
|
[token],
|
|
|
|
restart: :transient
|
|
|
|
)
|
|
|
|
|
|
|
|
{:ok, token}
|
|
|
|
else
|
|
|
|
result -> result
|
|
|
|
end
|
2019-05-06 11:51:03 -06:00
|
|
|
end
|
|
|
|
end
|