Allow authenticating via client-sent events
This commit is contained in:
parent
273cda63ad
commit
21395aa509
3 changed files with 118 additions and 0 deletions
|
|
@ -214,6 +214,42 @@ defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
|
|||
end
|
||||
end
|
||||
|
||||
defp handle_client_event(
|
||||
%{"type" => "pleroma.authenticate", "token" => access_token} = _params,
|
||||
state
|
||||
) do
|
||||
with {:auth, nil, nil} <- {:auth, state.user, state.oauth_token},
|
||||
{:ok, user, oauth_token} <- authenticate_request(access_token, nil) do
|
||||
{[
|
||||
{:text,
|
||||
StreamerView.render("pleroma_respond.json", %{
|
||||
type: "pleroma.authenticate",
|
||||
result: "success"
|
||||
})}
|
||||
], %{state | user: user, oauth_token: oauth_token}}
|
||||
else
|
||||
{:auth, _, _} ->
|
||||
{[
|
||||
{:text,
|
||||
StreamerView.render("pleroma_respond.json", %{
|
||||
type: "pleroma.authenticate",
|
||||
result: "error",
|
||||
error: :already_authenticated
|
||||
})}
|
||||
], state}
|
||||
|
||||
_ ->
|
||||
{[
|
||||
{:text,
|
||||
StreamerView.render("pleroma_respond.json", %{
|
||||
type: "pleroma.authenticate",
|
||||
result: "error",
|
||||
error: :unauthorized
|
||||
})}
|
||||
], state}
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_client_event(params, state) do
|
||||
Logger.error("#{__MODULE__} received unknown event: #{inspect(params)}")
|
||||
{[], state}
|
||||
|
|
|
|||
|
|
@ -152,5 +152,6 @@ defmodule Pleroma.Web.StreamerView do
|
|||
|
||||
defp maybe_error(%{error: :bad_topic}), do: %{error: "bad_topic"}
|
||||
defp maybe_error(%{error: :unauthorized}), do: %{error: "unauthorized"}
|
||||
defp maybe_error(%{error: :already_authenticated}), do: %{error: "already_authenticated"}
|
||||
defp maybe_error(_), do: %{}
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue