Introduce application tokens
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -4,19 +4,16 @@ defmodule Mobilizon.Web.GraphQLSocket do
|
||||
use Absinthe.Phoenix.Socket,
|
||||
schema: Mobilizon.GraphQL.Schema
|
||||
|
||||
alias Mobilizon.Applications.Application, as: AuthApplication
|
||||
alias Mobilizon.Applications.ApplicationToken
|
||||
alias Mobilizon.Users.User
|
||||
|
||||
@spec connect(map, Phoenix.Socket.t()) :: {:ok, Phoenix.Socket.t()} | :error
|
||||
def connect(%{"token" => token}, socket) do
|
||||
with {:ok, authed_socket} <-
|
||||
Guardian.Phoenix.Socket.authenticate(socket, Mobilizon.Web.Auth.Guardian, token),
|
||||
%User{} = user <- Guardian.Phoenix.Socket.current_resource(authed_socket) do
|
||||
authed_socket =
|
||||
Absinthe.Phoenix.Socket.put_options(socket,
|
||||
context: %{
|
||||
current_user: user
|
||||
}
|
||||
)
|
||||
resource <- Guardian.Phoenix.Socket.current_resource(authed_socket) do
|
||||
set_context(authed_socket, resource)
|
||||
|
||||
{:ok, authed_socket}
|
||||
else
|
||||
@@ -29,4 +26,27 @@ defmodule Mobilizon.Web.GraphQLSocket do
|
||||
|
||||
@spec id(any) :: nil
|
||||
def id(_socket), do: nil
|
||||
|
||||
@spec set_context(Phoenix.Socket.t(), User.t() | ApplicationToken.t()) :: Phoenix.Socket.t()
|
||||
defp set_context(socket, %User{} = user) do
|
||||
Absinthe.Phoenix.Socket.put_options(socket,
|
||||
context: %{
|
||||
current_user: user
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
defp set_context(
|
||||
socket,
|
||||
%ApplicationToken{user: %User{} = user, application: %AuthApplication{} = app} =
|
||||
app_token
|
||||
) do
|
||||
Absinthe.Phoenix.Socket.put_options(socket,
|
||||
context: %{
|
||||
current_auth_app_token: app_token,
|
||||
current_auth_app: app,
|
||||
current_user: user
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user