Add callback function to logout user

This commit is contained in:
Alexandre de Souza 2024-12-02 10:24:56 -03:00
parent c9603f4167
commit 344a37c5e8
No known key found for this signature in database
GPG key ID: E39228FFBA346545
7 changed files with 42 additions and 2 deletions

View file

@ -45,6 +45,11 @@ defmodule Livebook.ZTA do
"""
@callback authenticate(name(), Plug.Conn.t(), keyword()) :: {Plug.Conn.t(), metadata() | nil}
@doc """
Logouts against the given name.
"""
@callback logout(name(), Phoenix.LiveView.Socket.t()) :: :ok | :error
@doc false
def init do
:ets.new(__MODULE__, [:named_table, :public, :set, read_concurrency: true])

View file

@ -16,7 +16,7 @@ defmodule Livebook.ZTA.BasicAuth do
end
@impl true
def authenticate(name, conn, _options) do
def authenticate(name, conn, _opts) do
{username, password} = Livebook.ZTA.get(name)
conn = Plug.BasicAuth.basic_auth(conn, username: username, password: password)
@ -26,4 +26,9 @@ defmodule Livebook.ZTA.BasicAuth do
{conn, %{}}
end
end
@impl true
def logout(_name, _socket) do
:error
end
end

View file

@ -25,6 +25,11 @@ defmodule Livebook.ZTA.Cloudflare do
{conn, authenticate_user(token, identity, keys)}
end
@impl true
def logout(_name, _socket) do
:error
end
@impl true
def init(options) do
state = struct!(__MODULE__, options)

View file

@ -25,6 +25,11 @@ defmodule Livebook.ZTA.GoogleIAP do
{conn, authenticate_user(token, identity, keys)}
end
@impl true
def logout(_name, _socket) do
:error
end
@impl true
def init(options) do
state = struct!(__MODULE__, options)

View file

@ -35,6 +35,16 @@ defmodule Livebook.ZTA.LivebookTeams do
end
end
@impl true
def logout(name, %{assigns: %{current_user: %{payload: %{"access_token" => token}}}}) do
team = Livebook.ZTA.get(name)
case Teams.Requests.logout_identity_provider(team, token) do
{:ok, _no_content} -> :ok
_ -> :error
end
end
defp handle_request(conn, team, %{"teams_identity" => _, "code" => code}) do
with {:ok, access_token} <- retrieve_access_token(team, code),
{:ok, metadata} <- get_user_info(team, access_token) do

View file

@ -7,7 +7,12 @@ defmodule Livebook.ZTA.PassThrough do
end
@impl true
def authenticate(_, conn, _) do
def authenticate(_name, conn, _opts) do
{conn, %{}}
end
@impl true
def logout(_name, _socket) do
:error
end
end

View file

@ -29,6 +29,11 @@ defmodule Livebook.ZTA.Tailscale do
{conn, user}
end
@impl true
def logout(_name, _socket) do
:error
end
defp authenticate_ip(remote_ip, address) do
{url, options} =
if String.starts_with?(address, "http") do