KartaWeb.UserAuth (Karta v1.0.0)

Signing in and out of the admin interface, and who is signed in.

The session cookie holds a session token (Karta.Accounts.create_session/2); the user, their roles and the session's end come from the database on every request and LiveView mount, so that signing out, a disabled user and an expired session take effect at once.

  • fetch_current_scope/2 (plug, browser pipeline) assigns :current_scope (Karta.Accounts.Scope, or nil).
  • require_authenticated/2 (plug) sends anyone not signed in to /login, remembering the page (a GET) to come back to.
  • redirect_if_authenticated/2 (plug) sends someone signed in from the sign-in pages to /.
  • on_mount(:require_authenticated, ...) does the same for LiveViews. A connected LiveView checks its session again every minute and when it expires, and leaves for /login once it has ended. Before every event, and before a live page's refresh (KartaWeb.LiveUpdates), it reads the session and the user's roles again, so that the event is judged, and the page read, by the scope as it is now (KartaWeb.Authorize).
  • log_in/3 starts a session: the old session (and anything an attacker may have planted in it) is dropped and a new cookie issued, so a session id is never carried across sign-in. log_out/1 ends the session and disconnects its LiveViews.

Summary

Functions

Disconnects the LiveViews of every session of user: they mount again, under the scope as it is now (after a change of the user's memberships).

Assigns :current_scope from the session token, or nil.

The topic of a session's LiveViews, from the SHA-256 of its token (it names the hash, not the token). Broadcasting "disconnect" on it closes them.

path when it is a path on this host (/..., not //host or /\host, no control characters), else nil: a return address never leads elsewhere.

Signs user in (by method, :oidc or :password): a new session cookie with a new token, then a redirect to the page remembered before sign-in (a path on this host only) or to /.

Ends the session, disconnects its LiveViews and goes to /login.

Sends signed-in users away from the sign-in pages.

Lets only signed-in users through; others go to /login.

Functions

disconnect_user(user)

@spec disconnect_user(Karta.Accounts.User.t()) :: :ok

Disconnects the LiveViews of every session of user: they mount again, under the scope as it is now (after a change of the user's memberships).

fetch_current_scope(conn, opts)

Assigns :current_scope from the session token, or nil.

live_socket_id(token_hash)

@spec live_socket_id(binary()) :: String.t()

The topic of a session's LiveViews, from the SHA-256 of its token (it names the hash, not the token). Broadcasting "disconnect" on it closes them.

local_path(path)

@spec local_path(term()) :: String.t() | nil

path when it is a path on this host (/..., not //host or /\host, no control characters), else nil: a return address never leads elsewhere.

log_in(conn, user, method)

Signs user in (by method, :oidc or :password): a new session cookie with a new token, then a redirect to the page remembered before sign-in (a path on this host only) or to /.

log_out(conn)

Ends the session, disconnects its LiveViews and goes to /login.

redirect_if_authenticated(conn, opts)

Sends signed-in users away from the sign-in pages.

require_authenticated(conn, opts)

Lets only signed-in users through; others go to /login.