@fluid-app/fluid-cli 0.1.422 → 0.1.424

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.
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { A as primeIdentityProfile, C as updateConfig, E as getConfigDir, F as resolveIdentityUrl, G as sendMfa, H as fetchUserCompanies, J as validateToken, K as signup, M as sessionFacts, N as signInWithBrowser, O as describeIdentityError, P as signOutIdentity, R as isIdentityProfile, S as readConfig, T as isStrictConfig, U as getFluidApiBase, V as confirmMfa, W as redeemIdentityLink, _ as getAuthToken, b as resolveCredentialSource, c as inputsModeActive, ct as filterCatalog, dt as visibleSubcommands, ft as walkCommands, g as getActiveProfile, i as INPUTS_MODE_ENV, k as identityToken, l as registerInputsHook, lt as rootCommand, q as switchCompany, s as exitOnInputsError, u as createInputsReader, ut as visibleOptions, w as writeConfig, z as FLUID_API_ERROR } from "../hook-Cpk6jCiV.mjs";
2
+ import { A as identityToken, B as isIdentityProfile, C as readConfig, D as getConfigDir, E as isStrictConfig, F as signInWithBrowser, G as getFluidApiBase, I as signOutIdentity, J as signup, K as redeemIdentityLink, L as resolveIdentityUrl, M as primeIdentityProfile, P as sessionFacts, T as writeConfig, U as confirmMfa, V as FLUID_API_ERROR, W as fetchUserCompanies, X as validateToken, Y as switchCompany, _ as getAuthToken, c as inputsModeActive, dt as rootCommand, ft as visibleOptions, g as getActiveProfile, i as INPUTS_MODE_ENV, k as describeIdentityError, l as registerInputsHook, mt as walkCommands, pt as visibleSubcommands, q as sendMfa, s as exitOnInputsError, u as createInputsReader, ut as filterCatalog, w as updateConfig, x as resolveCredentialSource } from "../hook-CPmRkza4.mjs";
3
3
  import { createRequire } from "node:module";
4
4
  import { existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
5
5
  import { fileURLToPath, pathToFileURL } from "node:url";
@@ -2872,7 +2872,18 @@ async function heldSession(auth) {
2872
2872
  sessions.set(key, held);
2873
2873
  return held;
2874
2874
  }
2875
- async function exchangeInto(auth, held, org) {
2875
+ /** The roles a token is narrowed to, in one stable order; none for no narrowing. */
2876
+ function narrowedTo(roles) {
2877
+ return [...new Set(roles ?? [])].sort();
2878
+ }
2879
+ /**
2880
+ * Where an exchanged token is held. A narrowed token is held apart from the
2881
+ * company's un-narrowed one, so asking for either never hands out the other.
2882
+ */
2883
+ function exchangeKey(org, roles) {
2884
+ return roles.length === 0 ? org : `${org} ${roles.join(" ")}`;
2885
+ }
2886
+ async function exchangeInto(auth, held, org, roles) {
2876
2887
  const config = {
2877
2888
  identityUrl: auth.identityUrl,
2878
2889
  clientId: IDENTITY_CLIENT_ID,
@@ -2881,7 +2892,7 @@ async function exchangeInto(auth, held, org) {
2881
2892
  };
2882
2893
  const target = {
2883
2894
  org,
2884
- roles: null
2895
+ roles: roles.length > 0 ? roles : null
2885
2896
  };
2886
2897
  const subject = await held.session.getAccessToken();
2887
2898
  let exchanged;
@@ -2891,30 +2902,75 @@ async function exchangeInto(auth, held, org) {
2891
2902
  if (!(error instanceof SessionError) || error.code !== "session_expired") throw error;
2892
2903
  exchanged = await exchangeNativeToken(config, await held.session.getAccessToken({ replacing: subject }), target);
2893
2904
  }
2894
- held.exchanged.set(org, exchanged);
2905
+ held.exchanged.set(exchangeKey(org, roles), exchanged);
2895
2906
  return exchanged;
2896
2907
  }
2897
2908
  /**
2909
+ * The identity service would not narrow a session to roles the person does not
2910
+ * hold in the company, for example `rep` for an admin with no rep standing.
2911
+ * Matched by `code`, not `instanceof`, because a plugin may load its own copy
2912
+ * of this package.
2913
+ */
2914
+ var RoleNotGrantedError = class extends Error {
2915
+ code = "role_not_granted";
2916
+ constructor(roles, org, companyName) {
2917
+ super(`The identity service did not grant ${roles.join(" and ")} at ${companyName ?? org}.`);
2918
+ this.roles = roles;
2919
+ this.org = org;
2920
+ this.companyName = companyName;
2921
+ this.name = "RoleNotGrantedError";
2922
+ }
2923
+ };
2924
+ /** Whether an error is a {@link RoleNotGrantedError}, from any copy of it. */
2925
+ function isRoleNotGrantedError(error) {
2926
+ return error instanceof Error && error.code === "role_not_granted";
2927
+ }
2928
+ /**
2898
2929
  * A current access token for an identity profile and the facts behind it,
2899
2930
  * restoring the session from the credential store without a prompt and
2900
2931
  * refreshing or exchanging as needed. `org` defaults to the company the
2901
2932
  * profile moved to; null asks for the session's own company. Rejects with
2902
2933
  * `not_signed_in` when no session is stored or the stored one has ended.
2934
+ *
2935
+ * `roles` narrows the token to those roles in that company (RFC 8693): the
2936
+ * identity service grants it only when the person holds them there now, and
2937
+ * the narrowed token is held apart from the un-narrowed one. A session whose
2938
+ * own token carries no role beyond `roles` is already narrow enough and keeps
2939
+ * its token. Rejects with {@link RoleNotGrantedError} when the service refuses.
2940
+ * Without `roles` the token is the company's as before.
2903
2941
  */
2904
- async function identityToken(auth, org = auth.org ?? null) {
2942
+ async function identityToken(auth, org = auth.org ?? null, roles) {
2905
2943
  const held = await heldSession(auth);
2906
2944
  const facts = await held.session.restore();
2907
2945
  if (!facts) throw new SessionError("not_signed_in", "No identity session is stored.");
2908
2946
  const accessToken = await held.session.getAccessToken();
2909
2947
  const chainFacts = held.session.facts() ?? facts;
2910
- if (!org || org === chainFacts.companyId) return {
2948
+ const narrowing = narrowedTo(roles);
2949
+ if (narrowing.length === 0) {
2950
+ if (!org || org === chainFacts.companyId) return {
2951
+ accessToken,
2952
+ expiresAt: chainFacts.expiresAt,
2953
+ facts: chainFacts
2954
+ };
2955
+ const cached = held.exchanged.get(exchangeKey(org, narrowing));
2956
+ if (cached && deps.now() < cached.expiresAt - EXPIRY_MARGIN_MS) return cached;
2957
+ return exchangeInto(auth, held, org, narrowing);
2958
+ }
2959
+ const target = org ?? chainFacts.companyId;
2960
+ if (!target) throw new SessionError("access_denied", "A platform session names no company to narrow in.");
2961
+ if (target === chainFacts.companyId && chainFacts.roles.length > 0 && chainFacts.roles.every((role) => narrowing.includes(role))) return {
2911
2962
  accessToken,
2912
2963
  expiresAt: chainFacts.expiresAt,
2913
2964
  facts: chainFacts
2914
2965
  };
2915
- const cached = held.exchanged.get(org);
2966
+ const cached = held.exchanged.get(exchangeKey(target, narrowing));
2916
2967
  if (cached && deps.now() < cached.expiresAt - EXPIRY_MARGIN_MS) return cached;
2917
- return exchangeInto(auth, held, org);
2968
+ try {
2969
+ return await exchangeInto(auth, held, target, narrowing);
2970
+ } catch (error) {
2971
+ if (error instanceof SessionError && error.code === "access_denied") throw new RoleNotGrantedError(narrowing, target);
2972
+ throw error;
2973
+ }
2918
2974
  }
2919
2975
  /** The company the session itself signed in to, with no exchange. */
2920
2976
  async function sessionFacts(auth) {
@@ -2922,10 +2978,19 @@ async function sessionFacts(auth) {
2922
2978
  }
2923
2979
  /**
2924
2980
  * An access token for a profile, recorded for {@link peekIdentityToken}.
2981
+ * `roles` narrows it as {@link identityToken} does, and a narrowed token is
2982
+ * not recorded there. A refusal to narrow rejects with a
2983
+ * {@link RoleNotGrantedError} that names the profile's company.
2925
2984
  */
2926
- async function profileAccessToken(profile) {
2927
- const token = await identityToken(profile.auth);
2928
- latest.set(profile.name, {
2985
+ async function profileAccessToken(profile, roles) {
2986
+ let token;
2987
+ try {
2988
+ token = await identityToken(profile.auth, profile.auth.org ?? null, roles);
2989
+ } catch (error) {
2990
+ if (isRoleNotGrantedError(error)) throw new RoleNotGrantedError(error.roles, error.org, profile.companyName);
2991
+ throw error;
2992
+ }
2993
+ if (narrowedTo(roles).length === 0) latest.set(profile.name, {
2929
2994
  token: token.accessToken,
2930
2995
  expiresAt: token.expiresAt
2931
2996
  });
@@ -3329,6 +3394,27 @@ async function getAuthTokenAsync(profileName) {
3329
3394
  return source.profile.token ?? null;
3330
3395
  }
3331
3396
  /**
3397
+ * Like {@link getAuthTokenAsync}, for a token that acts only as `roles`. An
3398
+ * admin who is also a rep holds an `admin` token by default, which an API that
3399
+ * wants a rep refuses; this asks the identity service to narrow the session to
3400
+ * `roles` in the company (RFC 8693), and holds that token apart from the
3401
+ * default one, which every other reader keeps getting.
3402
+ *
3403
+ * Only an identity-mode profile can be narrowed. A literal token
3404
+ * (`FLUID_TOKEN`) and a legacy profile come back exactly as
3405
+ * {@link getAuthTokenAsync} returns them, because their token already is what
3406
+ * it is. Resolves with null when there is no credential. Rejects with an
3407
+ * error {@link isRoleNotGrantedError} recognises when the person does not
3408
+ * hold `roles` in the company, and when an identity session has ended.
3409
+ */
3410
+ async function getRoleAuthTokenAsync(roles, profileName) {
3411
+ const source = resolveCredentialSource(profileName);
3412
+ if (!source) return null;
3413
+ if (source.kind === "token") return source.token;
3414
+ if (isIdentityProfile(source.profile)) return profileAccessToken(source.profile, roles);
3415
+ return source.profile.token ?? null;
3416
+ }
3417
+ /**
3332
3418
  * Get the active profile, or null if not logged in
3333
3419
  */
3334
3420
  function getActiveProfile() {
@@ -3798,6 +3884,6 @@ function exitOnInputsError(error, io = processIo) {
3798
3884
  io.exit(64);
3799
3885
  }
3800
3886
  //#endregion
3801
- export { isNodeError as $, primeIdentityProfile as A, SIGNUP_ERROR as B, updateConfig as C, getConfigFilePath as D, getConfigDir as E, resolveIdentityUrl as F, sendMfa as G, fetchUserCompanies as H, createFetchClient as I, validateToken as J, signup as K, createDefaultConfig as L, sessionFacts as M, signInWithBrowser as N, describeIdentityError as O, signOutIdentity as P, isFailure as Q, isIdentityProfile as R, readConfig as S, isStrictConfig as T, getFluidApiBase as U, confirmMfa as V, redeemIdentityLink as W, getErrorMessage$1 as X, failure as Y, isError as Z, getAuthToken as _, INPUTS_UNDECLARED_CODE as a, tryCatchAsync as at, resolveCredentialSource as b, inputsModeActive as c, filterCatalog as ct, setActiveSnapshot as d, visibleSubcommands as dt, isSuccess as et, inInternalRead as f, walkCommands as ft, getActiveProfile as g, declareInputs as h, INPUTS_MODE_ENV as i, tryCatch as it, profileAccessToken as j, identityToken as k, registerInputsHook as l, rootCommand as lt, withInternalReadsAsync as m, INPUTS_MISMATCH_CODE as n, mapResult as nt, INPUTS_UNDECLARED_EXIT as o, unwrap as ot, withInternalReads as p, describeCommand as pt, switchCompany as q, INPUTS_MISMATCH_EXIT as r, success as rt, exitOnInputsError as s, unwrapOr as st, INPUTS_EXPECT_ENV as t, mapError as tt, createInputsReader as u, visibleOptions as ut, getAuthTokenAsync as v, writeConfig as w, findProjectConfig as x, listProfileNames as y, FLUID_API_ERROR as z };
3887
+ export { isError as $, identityToken as A, isIdentityProfile as B, readConfig as C, getConfigDir as D, isStrictConfig as E, signInWithBrowser as F, getFluidApiBase as G, SIGNUP_ERROR as H, signOutIdentity as I, signup as J, redeemIdentityLink as K, resolveIdentityUrl as L, primeIdentityProfile as M, profileAccessToken as N, getConfigFilePath as O, sessionFacts as P, getErrorMessage$1 as Q, createFetchClient as R, findProjectConfig as S, writeConfig as T, confirmMfa as U, FLUID_API_ERROR as V, fetchUserCompanies as W, validateToken as X, switchCompany as Y, failure as Z, getAuthToken as _, INPUTS_UNDECLARED_CODE as a, success as at, listProfileNames as b, inputsModeActive as c, unwrap as ct, setActiveSnapshot as d, rootCommand as dt, isFailure as et, inInternalRead as f, visibleOptions as ft, getActiveProfile as g, declareInputs as h, describeCommand as ht, INPUTS_MODE_ENV as i, mapResult as it, isRoleNotGrantedError as j, describeIdentityError as k, registerInputsHook as l, unwrapOr as lt, withInternalReadsAsync as m, walkCommands as mt, INPUTS_MISMATCH_CODE as n, isSuccess as nt, INPUTS_UNDECLARED_EXIT as o, tryCatch as ot, withInternalReads as p, visibleSubcommands as pt, sendMfa as q, INPUTS_MISMATCH_EXIT as r, mapError as rt, exitOnInputsError as s, tryCatchAsync as st, INPUTS_EXPECT_ENV as t, isNodeError as tt, createInputsReader as u, filterCatalog as ut, getAuthTokenAsync as v, updateConfig as w, resolveCredentialSource as x, getRoleAuthTokenAsync as y, createDefaultConfig as z };
3802
3888
 
3803
- //# sourceMappingURL=hook-Cpk6jCiV.mjs.map
3889
+ //# sourceMappingURL=hook-CPmRkza4.mjs.map