livebook/lib/livebook_web/endpoint.ex
Jonatan Kłosko e9766ed7a5
Introduce token auth and add basic cli configuration (#148)
* Add token authentication

* Restructure CLI

* Allow port configuration

* Further refactoring

* Make sure livebook node starts with unique name

* Improve startup error handling

* Further refactoring

* Add authentication tests

* Add authentication view for entering the token

* Fix auth tests

* Always use random Livebook name for distribution

* Don't enable ANSI on Windows

* Define CLI Task behaviour and move generic logic to the main module

* Generalize convertion from cli arguments to configuration

* Randomly generate secret key base

* Update test/livebook_web/plugs/auth_plug_test.exs

Co-authored-by: José Valim <jose.valim@dashbit.co>

* Override app config in persistent manner

* Update lib/litebook_cli.ex

Co-authored-by: José Valim <jose.valim@dashbit.co>

* Move auth error to ErrorView

* Unify node name configuration and allow it via CLI

* Set all applications configs at once

* Move token generation to application.ex to work outside CLI

* Clean up overriding configuration

* Store auth token in separate cookies

* Update lib/livebook_cli/server.ex

Co-authored-by: José Valim <jose.valim@dashbit.co>

* Update lib/livebook_web/endpoint.ex

Co-authored-by: José Valim <jose.valim@dashbit.co>

* Update lib/livebook_web/plugs/auth_plug.ex

Co-authored-by: José Valim <jose.valim@dashbit.co>

Co-authored-by: José Valim <jose.valim@dashbit.co>
2021-04-08 11:41:52 +02:00

71 lines
2.3 KiB
Elixir

defmodule LivebookWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :livebook
# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
# Set :encryption_salt if you would also like to encrypt it.
@session_options [
store: :cookie,
key: "_livebook_key",
signing_salt: "deadbook"
]
socket "/live", Phoenix.LiveView.Socket,
# Don't check the origin as we don't know how the web app is gonna be accessed.
# It runs locally, but may be exposed via IP or domain name.
# The WebSocket connection is already protected from CSWSH by using CSRF token.
websocket: [check_origin: false, connect_info: [:user_agent, session: @session_options]]
# We use Escript for distributing Livebook, so we don't
# have access to the files in priv/static at runtime in the prod environment.
# To overcome this we load contents of those files at compilation time,
# so that they become a part of the executable and can be served from memory.
defmodule AssetsMemoryProvider do
use LivebookWeb.MemoryProvider,
from: :livebook,
gzip: true
end
defmodule AssetsFileSystemProvider do
use LivebookWeb.FileSystemProvider,
from: {:livebook, "priv/static_dev"}
end
# Serve static failes at "/"
if code_reloading? do
# In development we use assets from priv/static_dev (rebuilt dynamically on every change).
# Note that this directory doesn't contain predefined files (e.g. images),
# so we also use `AssetsMemoryProvider` to serve those from priv/static.
plug LivebookWeb.StaticPlug,
at: "/",
file_provider: AssetsFileSystemProvider,
gzip: false
end
plug LivebookWeb.StaticPlug,
at: "/",
file_provider: AssetsMemoryProvider,
gzip: true
# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader
end
plug Plug.RequestId
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
plug Plug.MethodOverride
plug Plug.Head
plug Plug.Session, @session_options
plug LivebookWeb.Router
end