cover/Elixir.WalletAuth.Commands.RegisterDevice.html

1 defmodule WalletAuth.Commands.RegisterDevice do
2 @moduledoc """
3 Device trust registration command.
4
5 Per checklist Track D:
6 - Registers a device as trusted after OTP/MFA verification.
7 - Emits `DeviceRegistered` event with actor traceability.
8 """
9
10 alias WalletAuth.Device.{Device, DeviceStore}
11 alias WalletAuth.Events.DeviceRegistered
12 alias WalletObservability.AuditEvent
13
14 @doc """
15 Registers a trusted device for the given user.
16
17 Returns `{:ok, device}`.
18 """
19 @spec execute(user_id :: String.t(), keyword()) :: {:ok, Device.t()}
20
:-(
def execute(user_id, opts \\ []) do
21
:-(
correlation_id = Keyword.get(opts, :correlation_id, WalletSharedKernel.Correlation.new_correlation_id())
22
23
:-(
device = Device.new(user_id, opts)
24
:-(
:ok = DeviceStore.register(device)
25
26
:-(
audit = AuditEvent.build(:auth, "device_registered", "device", device.device_id, :success,
27 actor_id: user_id,
28 correlation_id: correlation_id,
29
:-(
metadata: %{user_agent: device.user_agent, ip_address: device.ip_address}
30 )
31
:-(
:telemetry.execute([:wallet_auth, :audit], %{}, audit)
32
33
:-(
event = DeviceRegistered.build(device.device_id, user_id,
34 correlation_id: correlation_id,
35
:-(
user_agent: device.user_agent,
36
:-(
ip_address: device.ip_address
37 )
38
:-(
emit_event(event)
39
40 {:ok, device}
41 end
42
43
:-(
defp emit_event(event) do
44
:-(
pubsub = Application.get_env(:wallet_auth, :pubsub, WalletWeb.PubSub)
45
:-(
apply(Phoenix.PubSub, :broadcast, [pubsub, "wallet_auth:events", {:domain_event, event}])
46 rescue
47
:-(
_ -> :ok
48 end
49 end
Line Hits Source