API Reference ha_auth v#0.1.0

Copy Markdown View Source

Modules

Sign in to a Phoenix app through Home Assistant, three ways, all on one login page (HaAuth.Web.LoginLive)

Who may sign in: HA admins always, other HA users when their id is in config :ha_auth, :allowed_users. Checked at sign-in and again on every request and LiveView mount, so dropping someone locks them out at once.

Settings, from config :ha_auth, ... (usually in config/runtime.exs)

Websocket to HA, subscribed to call_service, for Kiosk Satellite sign-in: a persistent_notification.dismiss of an ha_auth_sign_in_<nonce> id goes to HaAuth.KioskSignIn with the HA user HA says made the call. Started only with kiosk: true and a token (admin, since other users' events are admin-only). Reconnects on drops.

Home Assistant's OAuth2 (IndieAuth flavour): no client registration, the app's base URL is the client_id and the redirect URI is on the same host. See https://developers.home-assistant.io/docs/auth_api/

One websocket command to HA: connect, authenticate with token, send command, return its result, close. For what HA only offers over the websocket (auth/current_user, config/auth/list).

HA's people (config/auth/list, admin-only and websocket-only, so it needs an admin :token). Used to name the user a kiosk signs in as. Swap in a stub with config :ha_auth, :users_source, Mod.

Sign-in without a button on Kiosk Satellite tablets.

on_mount for LiveViews: live_session :app, on_mount: {HaAuth.LiveAuth, :require_user} assigns :current_user or sends the browser to /login.

QR sign-in. The login page holds a code (in its session) and shows /pair/<code> as a QR code; a signed-in phone opens it and approves, which hands the phone's user to that page. Codes live 10 minutes, are approved once and taken once. Approval is broadcast on topic/1, so the waiting page moves on at once.

Plugs for routes

Routes for the login page and sign-in, added to a Phoenix router with

What HaAuth keeps in the Plug session, and reading it back.

The ways in (HA's OAuth, Kiosk Satellite, an approved QR code) and logout.

The login page, with all three ways in

The phone's side of the QR sign-in: /pair/<code> (behind sign-in) asks to approve the screen showing that code; the POST hands the phone's user to it.