Owner bearer token and browser session cookie manager backed by a :protected ETS table and <data_dir>/auth.token (0600).

Provides constant-time token/cookie verification via Plug.Crypto.secure_compare/2 and strict Host / Origin checks against DNS rebinding and CSRF.

Summary

Functions

Validates the HTTP Host header against the listener's literal :bind_address (defaults to loopback literals when omitted). Pass allow_localhost: true to also accept localhost:<port>.

Validates the HTTP Origin as an HTTP URL using the same host policy as allowed_host?/3.

Returns a specification to start this module under a supervisor.

Verifies owner_token_candidate in constant time and, if valid, mints a fresh 32-byte browser session cookie stored in ETS.

Returns the current owner token from ETS.

Starts the auth manager.

Constant-time comparison of candidate against the stored owner token.

Constant-time comparison of cookie_candidate against active browser session cookies.

Functions

allowed_host?(host_header, port, opts \\ [])

@spec allowed_host?(term(), pos_integer(), keyword()) :: boolean()

Validates the HTTP Host header against the listener's literal :bind_address (defaults to loopback literals when omitted). Pass allow_localhost: true to also accept localhost:<port>.

allowed_origin?(origin_header, port, opts \\ [])

@spec allowed_origin?(term(), pos_integer(), keyword()) :: boolean()

Validates the HTTP Origin as an HTTP URL using the same host policy as allowed_host?/3.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

create_browser_session(owner_token_candidate, server \\ __MODULE__, table \\ :h_auth)

@spec create_browser_session(term(), GenServer.server(), atom()) ::
  {:ok, String.t()} | {:error, :unauthorized}

Verifies owner_token_candidate in constant time and, if valid, mints a fresh 32-byte browser session cookie stored in ETS.

owner_token(table \\ :h_auth)

@spec owner_token(atom()) :: String.t()

Returns the current owner token from ETS.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the auth manager.

Options:

  • :data_dir (required) - root storage directory holding auth.token
  • :name - GenServer registration name (default H.Auth, pass nil for unnamed)
  • :table - ETS table name (default :h_auth)

verify_bearer(candidate, table \\ :h_auth)

@spec verify_bearer(term(), atom()) :: boolean()

Constant-time comparison of candidate against the stored owner token.

verify_cookie(cookie_candidate, table \\ :h_auth)

@spec verify_cookie(term(), atom()) :: boolean()

Constant-time comparison of cookie_candidate against active browser session cookies.