@almyty/auth 1.5.9 → 1.5.10

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.
@@ -58,3 +58,17 @@ export interface BrowserLoginOptions {
58
58
  openBrowser?: boolean;
59
59
  }
60
60
  export declare function browserLogin(options?: BrowserLoginOptions): Promise<BrowserLoginResult>;
61
+ /**
62
+ * Ask the API whether the key from a browser login is good for the
63
+ * organization the browser handed back with it, before saving the two
64
+ * side by side. The runner sends that saved organization on every call,
65
+ * and a key scoped to a different one (or to none) is refused with
66
+ * "API key is not scoped to the requested organization" -- which used to
67
+ * surface only later, at `runner start`, long after the login reported
68
+ * success.
69
+ *
70
+ * `mismatch` only on that exact refusal. An unreachable API or any other
71
+ * answer is `unknown`: the login is not failed over a network blip, and
72
+ * the other CLIs, which send no organization header, never hit this.
73
+ */
74
+ export declare function confirmKeyOrganization(apiUrl: string, token: string, organizationId: string): Promise<'ok' | 'mismatch' | 'unknown'>;
@@ -379,3 +379,35 @@ export function browserLogin(options = {}) {
379
379
  });
380
380
  });
381
381
  }
382
+ /**
383
+ * Ask the API whether the key from a browser login is good for the
384
+ * organization the browser handed back with it, before saving the two
385
+ * side by side. The runner sends that saved organization on every call,
386
+ * and a key scoped to a different one (or to none) is refused with
387
+ * "API key is not scoped to the requested organization" -- which used to
388
+ * surface only later, at `runner start`, long after the login reported
389
+ * success.
390
+ *
391
+ * `mismatch` only on that exact refusal. An unreachable API or any other
392
+ * answer is `unknown`: the login is not failed over a network blip, and
393
+ * the other CLIs, which send no organization header, never hit this.
394
+ */
395
+ export async function confirmKeyOrganization(apiUrl, token, organizationId) {
396
+ let res;
397
+ try {
398
+ res = await fetch(`${apiUrl.replace(/\/$/, '')}/auth/profile`, {
399
+ headers: { Authorization: `Bearer ${token}`, 'X-Organization-Id': organizationId },
400
+ });
401
+ }
402
+ catch {
403
+ return 'unknown';
404
+ }
405
+ if (res.ok)
406
+ return 'ok';
407
+ if (res.status === 401) {
408
+ const text = await res.text().catch(() => '');
409
+ if (/not scoped to the requested organization|not valid for that organization/i.test(text))
410
+ return 'mismatch';
411
+ }
412
+ return 'unknown';
413
+ }
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@
7
7
  * npx @almyty/auth logout
8
8
  * npx @almyty/auth whoami [--json] [--verify]
9
9
  */
10
- import { browserLogin } from './browser-login.js';
10
+ import { browserLogin, confirmKeyOrganization } from './browser-login.js';
11
11
  import { CREDENTIALS_FILE, credentialSummary, expiryState, loadCredentials, saveCredentials, clearCredentials, } from './credentials.js';
12
12
  import { CONFIG_FILE, resolveApiUrl, resolveFrontendUrl, saveConfig, } from './config.js';
13
13
  import { parseArgs, flagString } from './args.js';
@@ -102,6 +102,16 @@ async function cmdLogin(args) {
102
102
  frontendUrl,
103
103
  openBrowser: args.flags['no-browser'] !== true,
104
104
  });
105
+ // The saved organization and the key's organization have to agree,
106
+ // or `runner start` is refused later with a 401. Check now and fail
107
+ // the login with the fix, instead of saving a pair that cannot work.
108
+ if (result.organizationId) {
109
+ const check = await confirmKeyOrganization(apiUrl, result.token, result.organizationId);
110
+ if (check === 'mismatch') {
111
+ throw new Error('the key this login created is not scoped to the organization you picked. ' +
112
+ 'Run login again; if it repeats, the web app is older than this CLI.');
113
+ }
114
+ }
105
115
  saveCredentials({
106
116
  url: apiUrl,
107
117
  token: result.token,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@almyty/auth",
3
- "version": "1.5.9",
3
+ "version": "1.5.10",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },