@codingfrees/uap-device-control 0.2.8 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -68,6 +68,24 @@ npx @codingfrees/uap-device-control@latest status
68
68
 
69
69
  `status` reports the local device identity, authentication state and remote presence when authenticated.
70
70
 
71
+ ## Account identity and switching
72
+
73
+ Use `whoami` to inspect the account name, subject and workspace **verified by the relay**, not inferred from stored token content:
74
+
75
+ ```powershell
76
+ npx @codingfrees/uap-device-control@latest whoami
77
+ ```
78
+
79
+ To move a Windows device from one account to another, stop the current `remote` process or installed service, then run:
80
+
81
+ ```powershell
82
+ npx @codingfrees/uap-device-control@latest switch-account
83
+ npx @codingfrees/uap-device-control@latest whoami
84
+ npx @codingfrees/uap-device-control@latest remote
85
+ ```
86
+
87
+ During the OAuth browser step, sign in to the **intended** account, not the existing browser session. Alternatively, generate a single-use pairing code for the intended user in the Device Control admin console and pass it to `switch-account --pairing-code <code>`. The device ID and browser pairing state are preserved. The prior refresh credential is revoked **after** a successful new login; if revocation cannot be confirmed, the CLI displays a warning. A failed new login leaves the old authorization intact. The relay and CLI must both support the identity endpoint to display verified identity; otherwise `whoami` explicitly reports that verification is unavailable instead of guessing.
88
+
71
89
  ## Persistent service
72
90
 
73
91
  After login:
@@ -299,6 +299,20 @@ async function accessToken(stateDirectory, fetcher = fetch, forceRefresh = false
299
299
  return { auth: next, token: next.accessToken };
300
300
  });
301
301
  }
302
+ async function revokePreviousRemoteAuthorization(previous, fetcher = fetch) {
303
+ try {
304
+ const response = await fetcher(new URL("/oauth/revoke", previous.origin), {
305
+ method: "POST",
306
+ headers: { "content-type": "application/x-www-form-urlencoded" },
307
+ body: new URLSearchParams({ client_id: previous.clientId, token: previous.refreshToken, resource: previous.origin + "/mcp" }),
308
+ redirect: "error",
309
+ signal: AbortSignal.timeout(1e4)
310
+ });
311
+ return response.ok;
312
+ } catch {
313
+ return false;
314
+ }
315
+ }
302
316
  async function logoutDeviceControl(stateDirectory, fetcher = fetch) {
303
317
  const auth = await loadRemoteAuth(stateDirectory);
304
318
  if (!auth) return false;
@@ -783,7 +797,7 @@ import WebSocket from "ws";
783
797
  // package.json
784
798
  var package_default = {
785
799
  name: "@codingfrees/uap-device-control",
786
- version: "0.2.8",
800
+ version: "0.2.9",
787
801
  description: "Cross-platform outbound UAP Device Control client",
788
802
  type: "module",
789
803
  bin: {
@@ -1103,7 +1117,7 @@ var DeviceAgentEngine = class {
1103
1117
  const process2 = this.#spawn(input, false, workspaceId);
1104
1118
  process2.capability = "terminal.exec";
1105
1119
  this.#oneShots.set(process2.id, process2);
1106
- const timeoutMs = bounded(Number(input.timeoutMs ?? 3e4), 100, 12e4, "command timeout");
1120
+ const timeoutMs = bounded(Number(input.timeoutMs ?? 3e4), 100, 3e5, "command timeout");
1107
1121
  try {
1108
1122
  const timedOut = await new Promise((resolve) => {
1109
1123
  const timer = setTimeout(() => {
@@ -2076,7 +2090,14 @@ async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, si
2076
2090
  const value = socketMessage(data);
2077
2091
  if (value.type === "connected") {
2078
2092
  connected = true;
2079
- log?.({ event: "remote.session", status: "connected", deviceId, agentVersion: UAP_DEVICE_CONTROL_AGENT_VERSION });
2093
+ log?.({
2094
+ event: "remote.session",
2095
+ status: "connected",
2096
+ deviceId,
2097
+ agentVersion: UAP_DEVICE_CONTROL_AGENT_VERSION,
2098
+ workspaceId: bounded2(value.workspaceId, "workspace id"),
2099
+ ...value.subject === void 0 ? {} : { subject: bounded2(value.subject, "account subject") }
2100
+ });
2080
2101
  return;
2081
2102
  }
2082
2103
  if (!connected) throw new TypeError("remote socket sent data before connection acknowledgement");
@@ -2247,6 +2268,14 @@ async function authorizedRequest(fetcher, origin, stateDirectory, pathname, body
2247
2268
  return send2(true);
2248
2269
  }
2249
2270
  }
2271
+ async function remoteIdentity(origin, stateDirectory, fetcher = fetch) {
2272
+ const value = await authorizedRequest(fetcher, origin, stateDirectory, "/device-control/remote/identity", {});
2273
+ const subject = bounded2(value.subject, "account subject");
2274
+ const workspaceId = bounded2(value.workspaceId, "workspace id");
2275
+ const accountName = typeof value.accountName === "string" && value.accountName.length <= 256 && value.accountName.trim() && !/[\x00-\x1f\x7f-\x9f]/.test(value.accountName) ? value.accountName : subject;
2276
+ const accountEmail = typeof value.accountEmail === "string" && value.accountEmail.length <= 320 && /^[^\s@\x00-\x1f\x7f-\x9f]+@[^\s@\x00-\x1f\x7f-\x9f]+$/.test(value.accountEmail) ? value.accountEmail : void 0;
2277
+ return { subject, workspaceId, accountName, ...accountEmail ? { accountEmail } : {} };
2278
+ }
2250
2279
  async function remoteStatus(origin, stateDirectory, deviceId, fetcher = fetch) {
2251
2280
  const value = await authorizedRequest(fetcher, origin, stateDirectory, "/device-control/remote/status", { deviceId });
2252
2281
  if (typeof value.online !== "boolean") throw new TypeError("remote status response is invalid");
@@ -2457,7 +2486,7 @@ function xml(value) {
2457
2486
  var DEFAULT_DEVICE_CONTROL_ORIGIN = "https://device-control.aemdpc.ro";
2458
2487
  function parseDeviceControlArguments(args, cwd = process.cwd()) {
2459
2488
  const command = !args.length || args.includes("--help") || args.includes("-h") || args[0] === "help" ? "help" : args[0];
2460
- if (!["help", "remote", "status", "login", "logout", "browser-reset", "install-service"].includes(command ?? "")) throw new TypeError("unknown command; run uap-device-control help");
2489
+ if (!["help", "remote", "status", "whoami", "login", "switch-account", "logout", "browser-reset", "install-service"].includes(command ?? "")) throw new TypeError("unknown command; run uap-device-control help");
2461
2490
  let origin = process.env.UAP_DEVICE_CONTROL_ORIGIN;
2462
2491
  let pairingCode;
2463
2492
  let stateDirectory = defaultDeviceControlStateDirectory();
@@ -2527,6 +2556,33 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
2527
2556
  }
2528
2557
  });
2529
2558
  output({ status: "authenticated", origin: auth.origin });
2559
+ await displayVerifiedIdentity(auth.origin, config.stateDirectory, output);
2560
+ return;
2561
+ }
2562
+ if (config.command === "switch-account") {
2563
+ const lease2 = await acquireRemoteProcessLock(config.stateDirectory);
2564
+ try {
2565
+ const prior = await loadRemoteAuth(config.stateDirectory);
2566
+ const next = await loginDeviceControl(resolvedOrigin, config.stateDirectory, {
2567
+ pairingCode: config.pairingCode,
2568
+ onAuthorizationUrl: (url) => {
2569
+ output({ event: "oauth.browser", status: "authorization-required", url });
2570
+ }
2571
+ });
2572
+ const revoked = prior ? await revokePreviousRemoteAuthorization(prior) : true;
2573
+ output({ status: "account-switched", origin: next.origin, oldAuthorizationRevoked: revoked });
2574
+ await displayVerifiedIdentity(next.origin, config.stateDirectory, output);
2575
+ } finally {
2576
+ await lease2.release();
2577
+ }
2578
+ return;
2579
+ }
2580
+ if (config.command === "whoami") {
2581
+ if (!existingAuth) {
2582
+ output({ status: "not-logged-in" });
2583
+ return;
2584
+ }
2585
+ await displayVerifiedIdentity(resolvedOrigin, config.stateDirectory, output);
2530
2586
  return;
2531
2587
  }
2532
2588
  if (config.command === "logout") {
@@ -2548,6 +2604,7 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
2548
2604
  const deviceAuthorized = !!auth?.scope.split(" ").includes("device-control:device");
2549
2605
  const remote = auth && deviceAuthorized ? await checkRemoteStatus(auth.origin, config.stateDirectory, deviceId) : null;
2550
2606
  output({ deviceId, origin: auth?.origin ?? resolvedOrigin, authenticated: !!auth, deviceAuthorized, remote, serviceMode: config.service, platform: process.platform });
2607
+ if (auth && deviceAuthorized) await displayVerifiedIdentity(auth.origin, config.stateDirectory, output);
2551
2608
  return;
2552
2609
  }
2553
2610
  if (config.command === "install-service") {
@@ -2583,12 +2640,23 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
2583
2640
  debug: config.debug,
2584
2641
  unrestricted: config.unrestricted,
2585
2642
  signal: controller.signal,
2586
- log: output
2643
+ log: (event) => {
2644
+ output(event);
2645
+ if (event.event === "remote.session" && event.status === "connected")
2646
+ void displayVerifiedIdentity(resolvedOrigin, config.stateDirectory, output);
2647
+ }
2587
2648
  });
2588
2649
  } finally {
2589
2650
  await lease.release();
2590
2651
  }
2591
2652
  }
2653
+ async function displayVerifiedIdentity(origin, stateDirectory, output) {
2654
+ try {
2655
+ output({ event: "remote.identity", status: "verified", ...await remoteIdentity(origin, stateDirectory) });
2656
+ } catch {
2657
+ output({ event: "remote.identity", status: "unavailable" });
2658
+ }
2659
+ }
2592
2660
  function formatCliLogEntry(event, json2 = false, now = /* @__PURE__ */ new Date()) {
2593
2661
  const timestamp = now.toISOString();
2594
2662
  if (json2) return JSON.stringify({ ...event, timestamp });
@@ -2610,6 +2678,10 @@ function formatCliEvent(event, json2 = false) {
2610
2678
  const summary2 = event.summary && typeof event.summary === "object" ? " \xB7 " + JSON.stringify(event.summary) : "";
2611
2679
  return "\u2705 " + tool + " \xB7 completed" + summary2;
2612
2680
  }
2681
+ if (event.event === "remote.identity") {
2682
+ if (status !== "verified") return "\u26A0\uFE0F Account identity could not be verified with the server; retry whoami";
2683
+ return "\u{1F510} Verified account: " + String(event.accountName) + (event.accountEmail ? " <" + String(event.accountEmail) + ">" : "") + "\n Subject: " + String(event.subject) + "\n Workspace: " + String(event.workspaceId);
2684
+ }
2613
2685
  if (event.event === "remote.mode" && event.status === "unrestricted") return "\u26A0\uFE0F Unrestricted mode active: all system executables and paths permitted";
2614
2686
  if (event.event === "remote.command") {
2615
2687
  const requestId = String(event.requestId ?? "").slice(0, 8);
@@ -2633,7 +2705,7 @@ function formatCliEvent(event, json2 = false) {
2633
2705
  }
2634
2706
  if (event.event === "remote.session") {
2635
2707
  if (status === "replaced") return "\u267B\uFE0F Previous UAP Remote instance stopped; starting the new session";
2636
- if (status === "connected") return "\u2705 Device connected \xB7 " + String(event.deviceId) + (event.agentVersion ? " \xB7 v" + String(event.agentVersion) : "");
2708
+ if (status === "connected") return "\u2705 Device connected \xB7 " + String(event.deviceId) + (event.agentVersion ? " \xB7 v" + String(event.agentVersion) : "") + (event.subject ? "\n Server subject: " + String(event.subject) : "") + (event.workspaceId ? "\n Workspace: " + String(event.workspaceId) : "");
2637
2709
  if (status === "disconnected") {
2638
2710
  const code = Number(event.closeCode);
2639
2711
  const cause = ["socket-closed", "socket-error", "client-abort", "protocol-error", "result-delivery"].includes(String(event.closeCause)) ? String(event.closeCause) : "";
@@ -2648,6 +2720,7 @@ function formatCliEvent(event, json2 = false) {
2648
2720
  return "Open this URL in a browser and explicitly approve this computer:\n" + String(event.url) + "\nIf using WSL2, open the link in Windows and keep this terminal running for the localhost callback.\nAlternatively, run login --pairing-code <code>.";
2649
2721
  if (status === "connecting") return "\u{1F310} Connecting to " + String(event.origin) + " \xB7 " + String(event.platform);
2650
2722
  if (status === "authenticated") return "\u2705 Authenticated \xB7 " + String(event.origin);
2723
+ if (status === "account-switched") return "\u2705 Account authorization replaced \xB7 " + String(event.origin) + (event.oldAuthorizationRevoked === false ? "\n\u26A0\uFE0F Prior authorization revocation could not be confirmed; revoke the old session in the account console" : " \xB7 previous authorization revoked");
2651
2724
  if (status === "logged-out") return "\u2705 Logged out";
2652
2725
  if (status === "browser-pairing-reset") return "\u2705 Browser pairing reset \xB7 run remote again to get a new pairing code";
2653
2726
  if (status === "not-logged-in") return "\u26A0\uFE0F Not logged in";
@@ -2692,6 +2765,8 @@ Usage:
2692
2765
  Commands:
2693
2766
  remote Connect this computer to Device Control (replace a verified existing Remote instance)
2694
2767
  status Show authentication, device and connection status
2768
+ whoami Show the server-verified account and workspace
2769
+ switch-account Stop Remote first; sign in as another account without deleting device identity
2695
2770
  login Sign in through browser OAuth/PKCE or an admin-issued pairing code
2696
2771
  logout Remove the saved authorization
2697
2772
  browser-reset Reset only the saved browser-extension pairing
@@ -2717,6 +2792,9 @@ Examples:
2717
2792
  npx @codingfrees/uap-device-control@latest
2718
2793
  npx @codingfrees/uap-device-control@latest login
2719
2794
  npx @codingfrees/uap-device-control@latest login --pairing-code <code>
2795
+ npx @codingfrees/uap-device-control@latest whoami
2796
+ npx @codingfrees/uap-device-control@latest switch-account
2797
+ npx @codingfrees/uap-device-control@latest switch-account --pairing-code <code>
2720
2798
  npx @codingfrees/uap-device-control@latest browser-reset
2721
2799
  npx @codingfrees/uap-device-control@latest remote
2722
2800
  npx @codingfrees/uap-device-control@latest remote --no-browser-control
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codingfrees/uap-device-control",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "Cross-platform outbound UAP Device Control client",
5
5
  "type": "module",
6
6
  "bin": {