From 6c5e315df43dc6937984ecab2fbea6a5b6cad684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Tue, 28 May 2024 19:04:44 -0300 Subject: [PATCH] Add docs related to auth when using Kino.Proxy --- lib/kino/proxy.ex | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/kino/proxy.ex b/lib/kino/proxy.ex index 60cf4db4..e78e2ec6 100644 --- a/lib/kino/proxy.ex +++ b/lib/kino/proxy.ex @@ -20,7 +20,7 @@ defmodule Kino.Proxy do Plug.Conn.send_resp(conn, 200, "hello") end - > #### Plug {: .info} + > #### Plug dependency {: .info} > > In order to use this feature, you need to add `:plug` as a dependency. @@ -29,12 +29,10 @@ defmodule Kino.Proxy do Using the proxy feature, we can use Livebook apps to build APIs. For example, we could provide a data export endpoint: - data = <<...>> - token = "auth-token" Kino.Proxy.listen(fn %{path_info: ["export", "data"]} = conn -> - ["Bearer " <> ^token] = Plug.Conn.get_req_header(conn, "authorization") + data = "some data" conn |> Plug.Conn.put_resp_header("content-type", "application/csv") @@ -46,8 +44,28 @@ defmodule Kino.Proxy do |> Plug.Conn.send_resp(200, "use /export/data to get extract the report data") end) - Once deployed as an app, the user would be able to export the data + Once deployed as an app, the API client would be able to export the data by sending a request to `/apps/:slug/proxy/export/data`. + + > #### Authentication {: .warning} + > + > The paths exposed by `Kino.Proxy` don't use the authentication mechanisms + > defined in your Livebook instance. + > + > If you need to authenticate requests, you should + > implement your own authentication mechanism. Here's a simple example. + > + > ```elixir + > Kino.Proxy.listen(fn conn -> + > import Plug.BasicAuth + > + > case Plug.BasicAuth.basic_auth(conn, username: "username", password: "password") do + > %{status: 401} = conn -> Plug.Conn.send_resp(conn) + > + > conn -> Plug.Conn.send_resp(conn, 200, "hello") + > end + > end) + > ``` """ @doc """