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 """