@senticor/hive 1.169.2 → 1.171.0

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.
Files changed (3) hide show
  1. package/README.md +3 -1
  2. package/hive.cjs +96 -16
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -50,7 +50,9 @@ hive login --api-url https://api.hive.example.internal
50
50
  hive login alice
51
51
 
52
52
  # Remote shell (SSH / Coder / container) where the browser is on another host:
53
- # prints the URL — authenticate anywhere, then paste the code / callback URL back.
53
+ # prints the URL — authenticate anywhere, then paste the code the page shows back.
54
+ # The CLI shows which account signed in and asks before it saves; --switch-account
55
+ # makes the sign-in page ask for the account instead of reusing the browser's session.
54
56
  # An imperfect paste gets up to three attempts; the printed link stays valid.
55
57
  hive login --no-browser
56
58
 
package/hive.cjs CHANGED
@@ -8927,13 +8927,22 @@ function openBrowser(url2) {
8927
8927
  function parsePastedCallback(input2, expectedState) {
8928
8928
  const trimmed2 = input2.trim();
8929
8929
  if (!trimmed2) throw new Error("No code entered.");
8930
+ if (trimmed2.includes("#") && !trimmed2.includes("://") && !trimmed2.includes("=")) {
8931
+ const at = trimmed2.lastIndexOf("#");
8932
+ const code = trimmed2.slice(0, at).trim();
8933
+ if (trimmed2.slice(at + 1).trim() !== expectedState) throw new Error("OAuth state mismatch");
8934
+ if (!code) throw new Error("No code entered.");
8935
+ return code;
8936
+ }
8930
8937
  if (trimmed2.includes("code=") || trimmed2.includes("error=") || trimmed2.includes("?")) {
8931
8938
  const u = trimmed2.includes("://") ? new import_node_url.URL(trimmed2) : new import_node_url.URL(`http://127.0.0.1/callback?${trimmed2.replace(/^[?]/, "")}`);
8932
- const err = u.searchParams.get("error");
8939
+ const fragment = u.hash.replace(/^#/, "");
8940
+ const params = /(^|&)(code|error)=/.test(fragment) ? new URLSearchParams(fragment) : u.searchParams;
8941
+ const err = params.get("error");
8933
8942
  if (err) throw new Error(`Authorization failed: ${err}`);
8934
- const st = u.searchParams.get("state");
8943
+ const st = params.get("state");
8935
8944
  if (st && st !== expectedState) throw new Error("OAuth state mismatch");
8936
- const code = u.searchParams.get("code");
8945
+ const code = params.get("code");
8937
8946
  if (!code) throw new Error("No 'code' found in the pasted URL.");
8938
8947
  return code;
8939
8948
  }
@@ -9213,6 +9222,9 @@ async function loginWithBrowser(issuerUrl, opts = {}) {
9213
9222
  const timeoutMs = opts.timeoutMs ?? 3e5;
9214
9223
  const tokenEndpoint = rewriteForInternalKeycloak(disco.token_endpoint, issuer);
9215
9224
  const maxAttempts = opts.noBrowser ? Math.max(1, opts.maxPasteAttempts ?? MAX_PASTE_ATTEMPTS) : 1;
9225
+ const hosted = opts.noBrowser && opts.callbackUrl ? opts.callbackUrl : void 0;
9226
+ const askForAccount = Boolean(opts.switchAccount || opts.loginHint);
9227
+ const previous = getStoredCredentials();
9216
9228
  const inbox = createInbox();
9217
9229
  let prompter;
9218
9230
  let timer;
@@ -9241,7 +9253,7 @@ async function loginWithBrowser(issuerUrl, opts = {}) {
9241
9253
  inbox.push({ kind: "loopback", code: gotCode });
9242
9254
  });
9243
9255
  try {
9244
- const redirectUri = await new Promise((resolve3, reject) => {
9256
+ const redirectUri = hosted ?? await new Promise((resolve3, reject) => {
9245
9257
  const onListenError = (e) => reject(e);
9246
9258
  server.once("error", onListenError);
9247
9259
  server.listen(0, "127.0.0.1", () => {
@@ -9258,14 +9270,24 @@ async function loginWithBrowser(issuerUrl, opts = {}) {
9258
9270
  authUrl.searchParams.set("state", state);
9259
9271
  authUrl.searchParams.set("code_challenge", challenge);
9260
9272
  authUrl.searchParams.set("code_challenge_method", "S256");
9273
+ if (hosted) authUrl.searchParams.set("response_mode", "fragment");
9261
9274
  if (opts.loginHint) authUrl.searchParams.set("login_hint", opts.loginHint);
9275
+ if (askForAccount) authUrl.searchParams.set("prompt", "login");
9262
9276
  const href = authUrl.toString();
9263
9277
  if (opts.noBrowser) {
9264
9278
  console.log("\nOpen this link in a browser on any device and sign in:");
9265
9279
  console.log(` ${href}
9266
9280
  `);
9281
+ if (!askForAccount) {
9282
+ const other = new import_node_url.URL(href);
9283
+ other.searchParams.set("prompt", "login");
9284
+ if (previous?.user && previous.issuer === issuer) other.searchParams.set("login_hint", previous.user);
9285
+ console.log("To sign in with a different account than the browser's current one, open this link instead:");
9286
+ console.log(` ${other.toString()}
9287
+ `);
9288
+ }
9267
9289
  console.log(
9268
- "You are then redirected to a 127.0.0.1 page, which may not load here.\nCopy the address from the address bar (or just the code=\u2026 value) and paste it below.\n"
9290
+ hosted ? "The page then shows a code. Copy it and paste it below.\n" : "You are then redirected to a 127.0.0.1 page, which may not load here.\nCopy the address from the address bar (or just the code=\u2026 value) and paste it below.\n"
9269
9291
  );
9270
9292
  } else {
9271
9293
  console.log("\nOpening the browser to sign in (or copy the link):");
@@ -9353,12 +9375,21 @@ async function loginWithBrowser(issuerUrl, opts = {}) {
9353
9375
  const accessToken = typeof res.data.access_token === "string" ? res.data.access_token : "";
9354
9376
  if (res.ok && accessToken) {
9355
9377
  const claims2 = parseJwt(accessToken);
9378
+ const identity = loginIdentity(issuer, claims2, res.data.id_token, previous);
9379
+ identity.user ??= opts.loginHint;
9380
+ printIdentity(identity);
9381
+ const confirm = opts.confirmAccount ?? (ev.kind === "paste" && prompter && prompter.canReask() ? (id) => askToContinue(prompter, id) : void 0);
9382
+ if (confirm && !await confirm(identity)) {
9383
+ throw new Error(
9384
+ `Not saved: you did not continue as ${identity.user ?? "this account"}, so the stored login is unchanged. Run 'hive login --no-browser --switch-account' to sign in with another account.`
9385
+ );
9386
+ }
9356
9387
  saveCredentials({
9357
9388
  issuer,
9358
9389
  access_token: accessToken,
9359
9390
  refresh_token: res.data.refresh_token,
9360
9391
  expires_at: Date.now() + (res.data.expires_in || 3600) * 1e3,
9361
- user: claims2 ? claims2.preferred_username || claims2.sub : opts.loginHint,
9392
+ user: identity.user,
9362
9393
  clientId
9363
9394
  });
9364
9395
  return true;
@@ -9377,6 +9408,26 @@ async function loginWithBrowser(issuerUrl, opts = {}) {
9377
9408
  prompter?.close();
9378
9409
  }
9379
9410
  }
9411
+ var claimString = (claims2, key) => typeof claims2?.[key] === "string" && claims2[key] ? claims2[key] : void 0;
9412
+ function loginIdentity(issuer, accessClaims, idToken, previous) {
9413
+ const idClaims = typeof idToken === "string" ? parseJwt(idToken) : null;
9414
+ const pick2 = (key) => claimString(idClaims, key) ?? claimString(accessClaims, key);
9415
+ const user = pick2("preferred_username") ?? pick2("sub");
9416
+ const previousUser = previous?.issuer === issuer && previous.user && previous.user !== user ? previous.user : void 0;
9417
+ return { user, email: pick2("email"), name: pick2("name"), issuer, previousUser };
9418
+ }
9419
+ function printIdentity(id) {
9420
+ console.log(`
9421
+ Signed in to ${id.issuer} as:`);
9422
+ console.log(` Account: ${id.user ?? "(unknown)"}`);
9423
+ if (id.email) console.log(` Email: ${id.email}`);
9424
+ if (id.name) console.log(` Name: ${id.name}`);
9425
+ if (id.previousUser) console.log(` \x1B[33mThis replaces the stored login of ${id.previousUser}.\x1B[0m`);
9426
+ }
9427
+ async function askToContinue(prompter, id) {
9428
+ const answer = await prompter.ask(`Continue as ${id.user ?? "this account"}? [Y/n] `);
9429
+ return answer.kind === "paste" && !/^\s*no?\s*$/i.test(answer.text);
9430
+ }
9380
9431
  var REFRESH_SKEW_MS = 6e4;
9381
9432
  async function refreshToken(creds) {
9382
9433
  if (!creds.refresh_token) return { ok: false, failure: { reason: "no_refresh_token", expiresAt: creds.expires_at } };
@@ -10015,6 +10066,15 @@ async function resolveSetting(opts) {
10015
10066
  }
10016
10067
  return { value: opts.def, source: "default" };
10017
10068
  }
10069
+ function httpUrlOrUndefined(value) {
10070
+ if (typeof value !== "string" || !value.trim()) return void 0;
10071
+ try {
10072
+ const url2 = new URL(value.trim());
10073
+ return url2.protocol === "https:" || url2.protocol === "http:" ? url2.href : void 0;
10074
+ } catch {
10075
+ return void 0;
10076
+ }
10077
+ }
10018
10078
  async function fetchAuthConfig(apiUrl) {
10019
10079
  try {
10020
10080
  const base = apiUrl.replace(/\/+$/, "");
@@ -10026,7 +10086,8 @@ async function fetchAuthConfig(apiUrl) {
10026
10086
  return {
10027
10087
  issuerUrl,
10028
10088
  clientId: typeof body.clientId === "string" ? body.clientId : void 0,
10029
- audience: typeof body.audience === "string" ? body.audience : void 0
10089
+ audience: typeof body.audience === "string" ? body.audience : void 0,
10090
+ cliCallbackUrl: httpUrlOrUndefined(body.cliCallbackUrl)
10030
10091
  };
10031
10092
  } catch {
10032
10093
  return null;
@@ -10575,22 +10636,22 @@ var artifactName = injectedString(
10575
10636
  true ? "hive-cli" : void 0
10576
10637
  ) || String(process.env.HIVE_CLI_ARTIFACT_NAME || "hive-cli").trim() || "hive-cli";
10577
10638
  var packageVersion = injectedString(
10578
- true ? "1.169.2" : void 0
10639
+ true ? "1.171.0" : void 0
10579
10640
  ) || String(process.env.HIVE_CLI_PACKAGE_VERSION || "").trim() || readPackageVersion();
10580
10641
  var releaseVersion = injectedString(
10581
- true ? "1.169.2" : void 0
10642
+ true ? "1.171.0" : void 0
10582
10643
  ) || String(process.env.HIVE_CLI_RELEASE_VERSION || "").trim() || packageVersion || "dev";
10583
10644
  var releaseChannel = injectedString(
10584
10645
  true ? "final" : void 0
10585
10646
  ) || String(process.env.HIVE_CLI_RELEASE_CHANNEL || "").trim() || (releaseVersion.includes("-rc.") ? "rc" : releaseVersion === "dev" ? "dev" : "final");
10586
10647
  var buildCommit = injectedString(
10587
- true ? "0645adffe6c498f75dd50368b54f396f2f5cb42b" : void 0
10648
+ true ? "ea64bb88950be1b622689e1a1a0cc7968410d84c" : void 0
10588
10649
  ) || String(process.env.HIVE_CLI_BUILD_COMMIT || "").trim() || "unknown";
10589
10650
  var buildTimestamp = injectedString(
10590
- true ? "2026-11-19T14:28:43Z" : void 0
10651
+ true ? "2026-11-20T13:59:34Z" : void 0
10591
10652
  ) || String(process.env.HIVE_CLI_BUILD_TIMESTAMP || "").trim() || "unknown";
10592
10653
  var ociRef = injectedString(
10593
- true ? "registry.onstackit.cloud/senticor/hive-cli:1.169.2" : void 0
10654
+ true ? "registry.onstackit.cloud/senticor/hive-cli:1.171.0" : void 0
10594
10655
  ) || String(process.env.HIVE_CLI_OCI_REF || "").trim() || `registry.onstackit.cloud/senticor/${artifactName}:${releaseVersion}`;
10595
10656
  var RELEASE_INFO = Object.freeze({
10596
10657
  packageVersion,
@@ -11501,7 +11562,13 @@ var loginArgs = {
11501
11562
  // `--no-browser`: don't rely on the loopback redirect reaching the CLI (the
11502
11563
  // browser and CLI are on different hosts — Coder/SSH/containers). Prints the
11503
11564
  // URL and reads the code/callback-URL from stdin instead. (citty negation.)
11504
- browser: { type: "boolean", default: true, description: "Open a browser + capture the loopback redirect (use --no-browser to paste the code)" }
11565
+ browser: { type: "boolean", default: true, description: "Open a browser + capture the loopback redirect (use --no-browser to paste the code)" },
11566
+ // #259: an existing Keycloak session in the browser signs in silently; this makes
11567
+ // Keycloak ask for the account. Naming a user (-u) does the same.
11568
+ switchAccount: { type: "boolean", default: false, description: "Make the sign-in page ask for the account instead of reusing the browser's session" },
11569
+ // #259: with --no-browser, keep the 127.0.0.1 redirect instead of the deployment's
11570
+ // hosted code page (scripts that complete the login through the loopback port).
11571
+ loopback: { type: "boolean", default: false, description: "With --no-browser: redirect to 127.0.0.1 instead of the page that shows the code" }
11505
11572
  };
11506
11573
  registerValueFlags(loginArgs);
11507
11574
  var login = defineCommand({
@@ -11519,10 +11586,12 @@ var login = defineCommand({
11519
11586
  let issuer;
11520
11587
  let clientId = process.env.HIVE_OIDC_CLIENT_ID?.trim() || void 0;
11521
11588
  const explicit = typeof args.issuerUrl === "string" && args.issuerUrl.trim() || process.env.HIVE_OIDC_ISSUER_URL?.trim();
11589
+ const noBrowser = args.browser === false;
11590
+ let fromApi;
11522
11591
  if (explicit) {
11523
11592
  issuer = { value: explicit, source: args.issuerUrl ? "flag" : "env" };
11524
11593
  } else {
11525
- const fromApi = await fetchAuthConfig(api.value);
11594
+ fromApi = await fetchAuthConfig(api.value);
11526
11595
  if (fromApi?.issuerUrl) {
11527
11596
  issuer = { value: fromApi.issuerUrl, source: "api" };
11528
11597
  if (!clientId && fromApi.clientId) clientId = fromApi.clientId;
@@ -11563,10 +11632,21 @@ var login = defineCommand({
11563
11632
  );
11564
11633
  }
11565
11634
  console.log("");
11635
+ let callbackUrl;
11636
+ if (noBrowser && !args.loopback) {
11637
+ if (fromApi === void 0) fromApi = await fetchAuthConfig(api.value);
11638
+ const sameIssuer = (a, b) => Boolean(a && b && a.replace(/\/+$/, "") === b.replace(/\/+$/, ""));
11639
+ if (fromApi?.cliCallbackUrl && sameIssuer(fromApi.issuerUrl, issuer.value)) callbackUrl = fromApi.cliCallbackUrl;
11640
+ }
11566
11641
  const loginHint = args.username || args.user || void 0;
11567
- const noBrowser = args.browser === false;
11568
11642
  try {
11569
- await loginWithBrowser(issuer.value, { loginHint, noBrowser, clientId });
11643
+ await loginWithBrowser(issuer.value, {
11644
+ loginHint,
11645
+ noBrowser,
11646
+ clientId,
11647
+ callbackUrl,
11648
+ switchAccount: Boolean(args.switchAccount)
11649
+ });
11570
11650
  const creds = getStoredCredentials();
11571
11651
  console.log(`\x1B[32mSigned in successfully${creds?.user ? ` as ${creds.user}` : ""}.\x1B[0m`);
11572
11652
  if (tenant.source !== "default") await warnIfTenantIsNotAMembership(api.value, tenant);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@senticor/hive",
3
- "version": "1.169.2",
3
+ "version": "1.171.0",
4
4
  "description": "Cognitive Hive OS — unified CLI",
5
5
  "type": "commonjs",
6
6
  "bin": {