| 1 |
|
defmodule WalletAuth.Commands.RevokeDevice do |
| 2 |
|
@moduledoc """ |
| 3 |
|
Device trust revocation command. |
| 4 |
|
|
| 5 |
|
Per checklist Track D: device revoke with actor traceability. |
| 6 |
|
""" |
| 7 |
|
|
| 8 |
|
alias WalletAuth.Device.DeviceStore |
| 9 |
|
alias WalletAuth.Events.DeviceRevoked |
| 10 |
|
alias WalletObservability.AuditEvent |
| 11 |
|
|
| 12 |
|
@doc """ |
| 13 |
|
Revokes a trusted device. |
| 14 |
|
|
| 15 |
|
Returns `:ok` or `{:error, :not_found}`. |
| 16 |
|
""" |
| 17 |
|
@spec execute(device_id :: String.t(), revoked_by :: String.t(), keyword()) :: |
| 18 |
|
:ok | {:error, :not_found} |
| 19 |
:-( |
def execute(device_id, revoked_by, opts \\ []) do |
| 20 |
:-( |
correlation_id = Keyword.get(opts, :correlation_id, WalletSharedKernel.Correlation.new_correlation_id()) |
| 21 |
|
|
| 22 |
:-( |
case DeviceStore.revoke(device_id, revoked_by) do |
| 23 |
|
{:ok, revoked_device} -> |
| 24 |
:-( |
audit = AuditEvent.build(:auth, "device_revoked", "device", device_id, :success, |
| 25 |
|
actor_id: revoked_by, |
| 26 |
|
correlation_id: correlation_id |
| 27 |
|
) |
| 28 |
:-( |
:telemetry.execute([:wallet_auth, :audit], %{}, audit) |
| 29 |
|
|
| 30 |
:-( |
event = DeviceRevoked.build(device_id, revoked_device.sub, revoked_by, |
| 31 |
|
correlation_id: correlation_id |
| 32 |
|
) |
| 33 |
:-( |
emit_event(event) |
| 34 |
|
:ok |
| 35 |
|
|
| 36 |
:-( |
{:error, :not_found} -> |
| 37 |
|
{:error, :not_found} |
| 38 |
|
end |
| 39 |
|
end |
| 40 |
|
|
| 41 |
:-( |
defp emit_event(event) do |
| 42 |
:-( |
pubsub = Application.get_env(:wallet_auth, :pubsub, WalletWeb.PubSub) |
| 43 |
:-( |
apply(Phoenix.PubSub, :broadcast, [pubsub, "wallet_auth:events", {:domain_event, event}]) |
| 44 |
|
rescue |
| 45 |
:-( |
_ -> :ok |
| 46 |
|
end |
| 47 |
|
end |