Performance: - schedule_live: preload page once instead of get_schedule!/1 per row - alert_live + alerts: DB-side status filter; Repo.aggregate counts replace length/Enum.count over 500-row fetches on every event - dashboard_live: drop duplicate get_device_status_counts; cap active alerts to 20 + use count_active_alerts/1 - maintenance.active_windows_for_device: 3 round-trips collapsed into one query (per-site-id branch keeps Postgres parameter types unambiguous) - sites.build_site_tree: O(N^2) -> O(N) via group_by(parent_site_id) - accounts.sole_owner_organizations: single group_by + having instead of per-org Repo.aggregate loop - agents + agent_live (org + admin): count_assigned_devices_batch/1 + count_agent_polling_targets/1 (no preloads) - alert_digest_worker: list_alerts_by_ids/1 batches digest fetch - gaiia: distinct: true at DB; limit 50 on bidirectional ilike Indexes: - maintenance_windows(organization_id, starts_at, ends_at) WHERE suppress_alerts = true - alerts(check_id) WHERE resolved_at IS NULL Antipatterns: - agents.delete_agent_token: PubSub.broadcast moved outside Repo.transaction so a rollback no longer leaves subscribers acting on a non-existent deletion - integrations_controller.to_atom_keys: replaced String.to_existing_atom on user-controlled JSON keys with explicit allowlist - 9 Task.start callsites converted to Task.Supervisor.start_child(Towerops.TaskSupervisor, ...) so background DB writes survive shutdown (test-mode discovery shims left as-is for sandbox semantics)
65 lines
1.8 KiB
Elixir
65 lines
1.8 KiB
Elixir
defmodule ToweropsWeb.Plugs.MobileAuth do
|
|
@moduledoc """
|
|
Plug for authenticating mobile app requests using bearer tokens.
|
|
|
|
Expects an Authorization header with format: "Bearer <token>"
|
|
|
|
On success, assigns :current_user and :current_mobile_session to the conn.
|
|
On failure, returns 401 Unauthorized.
|
|
"""
|
|
|
|
import Phoenix.Controller, only: [json: 2]
|
|
import Plug.Conn
|
|
|
|
alias Towerops.Accounts
|
|
alias Towerops.MobileSessions
|
|
|
|
def init(opts), do: opts
|
|
|
|
def call(conn, _opts) do
|
|
with {:ok, token} <- extract_token(conn),
|
|
{:ok, session} <- validate_session(token),
|
|
{:ok, user} <- get_user(session.user_id) do
|
|
# Touch the session to update last_used_at
|
|
_ =
|
|
Task.Supervisor.start_child(Towerops.TaskSupervisor, fn ->
|
|
MobileSessions.touch_session(session)
|
|
end)
|
|
|
|
conn
|
|
|> assign(:current_user, user)
|
|
|> assign(:current_mobile_session, session)
|
|
else
|
|
{:error, reason} ->
|
|
conn
|
|
|> put_status(:unauthorized)
|
|
|> json(%{error: error_message(reason)})
|
|
|> halt()
|
|
end
|
|
end
|
|
|
|
defp extract_token(conn) do
|
|
case get_req_header(conn, "authorization") do
|
|
["Bearer " <> token] -> {:ok, token}
|
|
_ -> {:error, :missing_token}
|
|
end
|
|
end
|
|
|
|
defp validate_session(token) do
|
|
case MobileSessions.get_session_by_token(token) do
|
|
nil -> {:error, :invalid_token}
|
|
session -> {:ok, session}
|
|
end
|
|
end
|
|
|
|
defp get_user(user_id) do
|
|
case Accounts.get_user(user_id) do
|
|
nil -> {:error, :user_not_found}
|
|
user -> {:ok, user}
|
|
end
|
|
end
|
|
|
|
defp error_message(:missing_token), do: "Authorization header is missing or invalid"
|
|
defp error_message(:invalid_token), do: "Invalid or expired authentication token"
|
|
defp error_message(:user_not_found), do: "User not found"
|
|
end
|