Skip to content

Instantly share code, notes, and snippets.

@DeedleFake
Last active April 25, 2024 19:51
Show Gist options
  • Save DeedleFake/5349cbd59b3eaf2cf2132e4ae3dc9ea1 to your computer and use it in GitHub Desktop.
Save DeedleFake/5349cbd59b3eaf2cf2132e4ae3dc9ea1 to your computer and use it in GitHub Desktop.
Livebook auth module for forwarded auth headers behind a reverse proxy.

This is an extremely simply identity provider module for Livebook when running Livebook behind a reverse proxy with forwarded authentication, such as when using Tailscale. For more information, see https://github.com/tailscale/tailscale/tree/main/cmd/nginx-auth and https://caddyserver.com/docs/caddyfile/directives/forward_auth#tailscale.

To use this, just stick webauth.exs in /app/user/extensions or, if using a Docker container for Livebook, stick it somewhere else and mount it into the Docker container via -v /path/to/webauth.exs:/app/user/extensions/webauth.exs:ro. Then, just set LIVEBOOK_IDENTITY_PROVIDER=custom:Webauth when running the Livebook server.

defmodule Webauth do
@behaviour Livebook.ZTA
use GenServer
def start_link(opts) do
GenServer.start_link(__MODULE__, :no_state, Keyword.take(opts, [:name]))
end
@impl true
def init(:no_state) do
{:ok, :no_state}
end
@impl true
def authenticate(_server, conn, _opts \\ []) do
import Plug.Conn
user =
%{
id: get_req_header(conn, "x-webauth-user") |> only(),
name: get_req_header(conn, "x-webauth-name") |> only(),
email: get_req_header(conn, "x-webauth-user") |> only()
}
{conn, user}
end
defp only([val]), do: val
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment