@indigoai-us/hq-cloud 6.18.65 → 6.18.66
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/dist/cognito-auth.d.ts +3 -1
- package/dist/cognito-auth.d.ts.map +1 -1
- package/dist/cognito-auth.js +186 -32
- package/dist/cognito-auth.js.map +1 -1
- package/dist/outposts/cli-billing.d.ts +11 -0
- package/dist/outposts/cli-billing.d.ts.map +1 -1
- package/dist/outposts/cli-billing.js +18 -1
- package/dist/outposts/cli-billing.js.map +1 -1
- package/dist/outposts/cli.d.ts +1 -1
- package/dist/outposts/cli.d.ts.map +1 -1
- package/dist/outposts/cli.js +1 -1
- package/dist/outposts/cli.js.map +1 -1
- package/dist/outposts/client.d.ts +38 -0
- package/dist/outposts/client.d.ts.map +1 -1
- package/dist/outposts/client.js +90 -1
- package/dist/outposts/client.js.map +1 -1
- package/dist/outposts/command.d.ts +9 -6
- package/dist/outposts/command.d.ts.map +1 -1
- package/dist/outposts/command.js +171 -12
- package/dist/outposts/command.js.map +1 -1
- package/dist/outposts/company.d.ts +103 -0
- package/dist/outposts/company.d.ts.map +1 -0
- package/dist/outposts/company.js +266 -0
- package/dist/outposts/company.js.map +1 -0
- package/dist/outposts/errors.d.ts +7 -1
- package/dist/outposts/errors.d.ts.map +1 -1
- package/dist/outposts/errors.js +8 -1
- package/dist/outposts/errors.js.map +1 -1
- package/package.json +1 -1
package/dist/cognito-auth.d.ts
CHANGED
|
@@ -45,6 +45,8 @@ export interface CognitoTokens {
|
|
|
45
45
|
/** Epoch milliseconds when the access token expires. Writers MUST emit a number. Readers accept ISO 8601 strings for backward compatibility with pre-migration token files. */
|
|
46
46
|
expiresAt: string | number;
|
|
47
47
|
tokenType: "Bearer";
|
|
48
|
+
/** Local credential-generation binding, never sent to a service. */
|
|
49
|
+
machineCredentialKey?: string;
|
|
48
50
|
}
|
|
49
51
|
/** Returned when an interactive login is needed but stdin/browser is unavailable. */
|
|
50
52
|
export declare class CognitoAuthError extends Error {
|
|
@@ -245,7 +247,7 @@ export interface MachineCreds {
|
|
|
245
247
|
/** Cognito region for the mint endpoint; falls back to config. */
|
|
246
248
|
region?: string;
|
|
247
249
|
/** Expected machine kind carried in the ID token's custom:entityType claim. */
|
|
248
|
-
entityType?: "agent" | "outpost";
|
|
250
|
+
entityType?: "agent" | "outpost" | "company-outpost";
|
|
249
251
|
/** Exact expected machine uid carried in custom:entityUid. */
|
|
250
252
|
entityUid?: string;
|
|
251
253
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cognito-auth.d.ts","sourceRoot":"","sources":["../src/cognito-auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAaH,MAAM,WAAW,iBAAiB;IAChC,4DAA4D;IAC5D,MAAM,EAAE,MAAM,CAAC;IACf,oEAAoE;IACpE,cAAc,EAAE,MAAM,CAAC;IACvB,yDAAyD;IACzD,QAAQ,EAAE,MAAM,CAAC;IACjB,gDAAgD;IAChD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,gEAAgE;IAChE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,+KAA+K;IAC/K,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IAC3B,SAAS,EAAE,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"cognito-auth.d.ts","sourceRoot":"","sources":["../src/cognito-auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAaH,MAAM,WAAW,iBAAiB;IAChC,4DAA4D;IAC5D,MAAM,EAAE,MAAM,CAAC;IACf,o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,CAAC,CAmHxB;AA+DD;;;;;;;;;;;;;GAaG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,iBAAiB,EACzB,mBAAmB,EAAE,MAAM,EAC3B,kBAAkB,CAAC,EAAE,MAAM,GAC1B,OAAO,CAAC,aAAa,CAAC,CA8JxB;AAED;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CACvC,MAAM,EAAE,iBAAiB,EACzB,OAAO,GAAE;IAAE,WAAW,CAAC,EAAE,OAAO,CAAA;CAAO,GACtC,OAAO,CAAC,MAAM,CAAC,CA2EjB;AAeD;;;;;GAKG;AACH,eAAO,MAAM,SAAS;;4BAEE,IAAI;CAG3B,CAAC"}
|
package/dist/cognito-auth.js
CHANGED
|
@@ -297,7 +297,7 @@ function isNotWritableError(err) {
|
|
|
297
297
|
* failing the call outright. It is deliberately NOT a substitute for the shared
|
|
298
298
|
* on-disk cache: a fresh subprocess starts empty and re-mints.
|
|
299
299
|
*/
|
|
300
|
-
|
|
300
|
+
const inMemoryCachedTokens = new Map();
|
|
301
301
|
/**
|
|
302
302
|
* True when `tokens` were minted for a machine identity (agent or outpost),
|
|
303
303
|
* judged by the ID token's `custom:entityType` claim. People never carry that
|
|
@@ -306,13 +306,20 @@ let inMemoryCachedTokens = null;
|
|
|
306
306
|
*/
|
|
307
307
|
export function tokensCarryMachineIdentity(tokens) {
|
|
308
308
|
const idClaims = decodeJwtClaims(tokens.idToken);
|
|
309
|
+
const accessClaims = decodeJwtClaims(tokens.accessToken);
|
|
309
310
|
const entityType = idClaims?.["custom:entityType"];
|
|
310
|
-
return typeof entityType === "string" && entityType.trim().length > 0
|
|
311
|
+
return (typeof entityType === "string" && entityType.trim().length > 0) ||
|
|
312
|
+
(idClaims !== null && hasCompanyMarkers(idClaims)) ||
|
|
313
|
+
(accessClaims !== null && hasCompanyMarkers(accessClaims));
|
|
311
314
|
}
|
|
312
315
|
function loadCachedTokensFrom(kind) {
|
|
313
316
|
const file = cacheFileFor(kind);
|
|
317
|
+
const memory = () => {
|
|
318
|
+
const tokens = inMemoryCachedTokens.get(file);
|
|
319
|
+
return tokens && !tokenIsInvalidated(tokens.accessToken, kind) ? tokens : null;
|
|
320
|
+
};
|
|
314
321
|
if (!fs.existsSync(file))
|
|
315
|
-
return
|
|
322
|
+
return memory();
|
|
316
323
|
try {
|
|
317
324
|
const raw = fs.readFileSync(file, "utf-8");
|
|
318
325
|
const tokens = JSON.parse(raw);
|
|
@@ -322,7 +329,7 @@ function loadCachedTokensFrom(kind) {
|
|
|
322
329
|
return tokens;
|
|
323
330
|
}
|
|
324
331
|
catch {
|
|
325
|
-
return
|
|
332
|
+
return memory();
|
|
326
333
|
}
|
|
327
334
|
}
|
|
328
335
|
/**
|
|
@@ -334,6 +341,8 @@ function loadCachedTokensFrom(kind) {
|
|
|
334
341
|
* the human path re-authenticates.
|
|
335
342
|
*/
|
|
336
343
|
export function loadCachedTokens() {
|
|
344
|
+
if (companyMachineRequired())
|
|
345
|
+
return null;
|
|
337
346
|
const tokens = loadCachedTokensFrom("person");
|
|
338
347
|
if (tokens && tokensCarryMachineIdentity(tokens))
|
|
339
348
|
return null;
|
|
@@ -341,7 +350,21 @@ export function loadCachedTokens() {
|
|
|
341
350
|
}
|
|
342
351
|
/** The MACHINE identity's cached session, or null. */
|
|
343
352
|
export function loadCachedMachineTokens() {
|
|
344
|
-
|
|
353
|
+
const tokens = loadCachedTokensFrom("machine");
|
|
354
|
+
if (!companyMachineRequired() || !tokens)
|
|
355
|
+
return tokens;
|
|
356
|
+
const creds = loadMachineCreds();
|
|
357
|
+
if (!creds || creds.entityType !== "company-outpost" || !creds.clientId || !creds.region)
|
|
358
|
+
return null;
|
|
359
|
+
// Company credentials carry their own client and region, so no caller config
|
|
360
|
+
// is needed to validate the same generation key used by the mint path.
|
|
361
|
+
const config = {
|
|
362
|
+
clientId: creds.clientId, region: creds.region, userPoolDomain: "",
|
|
363
|
+
};
|
|
364
|
+
if (tokens.machineCredentialKey !== machineIdentityKey(config, creds) ||
|
|
365
|
+
!machineTokenPairMatchesIdentity(tokens, creds, creds.clientId))
|
|
366
|
+
return null;
|
|
367
|
+
return tokens;
|
|
345
368
|
}
|
|
346
369
|
/**
|
|
347
370
|
* Persist a session to the cache that belongs to the principal it names.
|
|
@@ -355,12 +378,12 @@ export function loadCachedMachineTokens() {
|
|
|
355
378
|
export function saveCachedTokens(tokens) {
|
|
356
379
|
// Remember in-process FIRST: whatever happens to the disk write below, this
|
|
357
380
|
// process must not lose a session it already successfully minted.
|
|
358
|
-
inMemoryCachedTokens = tokens;
|
|
359
381
|
const kind = tokensCarryMachineIdentity(tokens)
|
|
360
382
|
? "machine"
|
|
361
383
|
: "person";
|
|
362
384
|
const stateDir = stateDirFor(kind);
|
|
363
385
|
const cacheFile = cacheFileFor(kind);
|
|
386
|
+
inMemoryCachedTokens.set(cacheFile, tokens);
|
|
364
387
|
try {
|
|
365
388
|
if (!fs.existsSync(stateDir)) {
|
|
366
389
|
fs.mkdirSync(stateDir, { recursive: true, mode: 0o700 });
|
|
@@ -381,6 +404,9 @@ export function saveCachedTokens(tokens) {
|
|
|
381
404
|
}
|
|
382
405
|
}
|
|
383
406
|
function clearCachedTokensOf(kind) {
|
|
407
|
+
inMemoryCachedTokens.delete(cacheFileFor(kind));
|
|
408
|
+
if (kind === "machine")
|
|
409
|
+
inProcessMachineTokens = null;
|
|
384
410
|
const file = cacheFileFor(kind);
|
|
385
411
|
const dir = stateDirFor(kind);
|
|
386
412
|
if (fs.existsSync(file))
|
|
@@ -449,8 +475,7 @@ function refreshLockWaitLogMs() {
|
|
|
449
475
|
const n = Number(raw);
|
|
450
476
|
return Number.isFinite(n) && n >= 0 ? n : DEFAULT_REFRESH_LOCK_WAIT_LOG_MS;
|
|
451
477
|
}
|
|
452
|
-
function readLockOwner(kind) {
|
|
453
|
-
const lockFile = refreshLockFile(kind);
|
|
478
|
+
function readLockOwner(kind, lockFile = refreshLockFile(kind)) {
|
|
454
479
|
let raw;
|
|
455
480
|
try {
|
|
456
481
|
raw = fs.readFileSync(lockFile, "utf-8");
|
|
@@ -486,8 +511,8 @@ function readLockOwner(kind) {
|
|
|
486
511
|
return { pid, startedAtMs };
|
|
487
512
|
}
|
|
488
513
|
/** The pid recorded in the lockfile, or null if absent/empty/malformed. */
|
|
489
|
-
function readLockOwnerPid(kind) {
|
|
490
|
-
const owner = readLockOwner(kind);
|
|
514
|
+
function readLockOwnerPid(kind, lockFile = refreshLockFile(kind)) {
|
|
515
|
+
const owner = readLockOwner(kind, lockFile);
|
|
491
516
|
return owner?.pid ?? null;
|
|
492
517
|
}
|
|
493
518
|
/**
|
|
@@ -638,7 +663,7 @@ async function withRefreshLock(kind, fn) {
|
|
|
638
663
|
catch (err) {
|
|
639
664
|
if (err.code !== "EEXIST")
|
|
640
665
|
throw err;
|
|
641
|
-
const owner = readLockOwner(kind);
|
|
666
|
+
const owner = readLockOwner(kind, lockFile);
|
|
642
667
|
const age = owner === null ? Infinity : Date.now() - owner.startedAtMs;
|
|
643
668
|
const deadHolder = owner !== null && owner.pid !== process.pid && !processAlive(owner.pid);
|
|
644
669
|
const staleByTtl = owner !== null && age > maxHoldMs;
|
|
@@ -680,7 +705,7 @@ async function withRefreshLock(kind, fn) {
|
|
|
680
705
|
}
|
|
681
706
|
finally {
|
|
682
707
|
// Release ONLY our own lock — never clobber a successor's.
|
|
683
|
-
if (held && readLockOwnerPid(kind) === process.pid) {
|
|
708
|
+
if (held && readLockOwnerPid(kind, lockFile) === process.pid) {
|
|
684
709
|
try {
|
|
685
710
|
fs.unlinkSync(lockFile);
|
|
686
711
|
}
|
|
@@ -754,11 +779,48 @@ function decodeJwtClaims(token) {
|
|
|
754
779
|
return null;
|
|
755
780
|
}
|
|
756
781
|
}
|
|
782
|
+
const owns = (bag, key) => Object.prototype.hasOwnProperty.call(bag, key);
|
|
783
|
+
function hasCompanyMarkers(claims) {
|
|
784
|
+
return claims["custom:entityType"] === "company-outpost" ||
|
|
785
|
+
owns(claims, "custom:ownerCompanyUid") || owns(claims, "custom:companyOutpostId");
|
|
786
|
+
}
|
|
787
|
+
function validCompanyCredentials(raw) {
|
|
788
|
+
return typeof raw.entityUid === "string" && /^out_[0-9A-HJKMNP-TV-Z]{26}$/.test(raw.entityUid) &&
|
|
789
|
+
typeof raw.username === "string" && raw.username.trim().length > 0 &&
|
|
790
|
+
typeof raw.secret === "string" && raw.secret.trim().length > 0 &&
|
|
791
|
+
typeof raw.clientId === "string" && raw.clientId.trim().length > 0 &&
|
|
792
|
+
typeof raw.region === "string" && /^[a-z]{2}-[a-z]+-[0-9]+$/.test(raw.region) &&
|
|
793
|
+
(raw.runtime === undefined || raw.runtime === "hosted") &&
|
|
794
|
+
!owns(raw, "delegatedSub") && !owns(raw, "delegatedEmail") &&
|
|
795
|
+
!owns(raw, "custom:delegatedSub") && !owns(raw, "custom:delegatedEmail");
|
|
796
|
+
}
|
|
797
|
+
function companyClaimsMatch(claims, creds, clientId) {
|
|
798
|
+
const company = claims["custom:ownerCompanyUid"];
|
|
799
|
+
return claims["custom:entityType"] === "company-outpost" &&
|
|
800
|
+
claims["custom:entityUid"] === creds.entityUid &&
|
|
801
|
+
claims["custom:authClientId"] === clientId &&
|
|
802
|
+
typeof company === "string" && /^cmp_[A-Za-z0-9]+$/.test(company) &&
|
|
803
|
+
claims["custom:companyOutpostId"] === `outpost_${company}` &&
|
|
804
|
+
(!process.env.HQ_COMPANY_UID || company === process.env.HQ_COMPANY_UID) &&
|
|
805
|
+
(!process.env.HQ_COMPANY_OUTPOST_ID || claims["custom:companyOutpostId"] === process.env.HQ_COMPANY_OUTPOST_ID) &&
|
|
806
|
+
!owns(claims, "custom:delegatedSub") && !owns(claims, "custom:delegatedEmail") &&
|
|
807
|
+
typeof claims.sub === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(claims.sub) &&
|
|
808
|
+
typeof claims.exp === "number" && claims.exp * 1000 > Date.now();
|
|
809
|
+
}
|
|
757
810
|
function machineTokenPairMatchesIdentity(tokens, creds, expectedClientId) {
|
|
758
811
|
const accessClaims = decodeJwtClaims(tokens.accessToken);
|
|
759
812
|
const idClaims = decodeJwtClaims(tokens.idToken);
|
|
760
813
|
if (!accessClaims || !idClaims)
|
|
761
814
|
return false;
|
|
815
|
+
if (creds.entityType === "company-outpost") {
|
|
816
|
+
if (!creds.entityUid || !/^out_[0-9A-HJKMNP-TV-Z]{26}$/.test(creds.entityUid) ||
|
|
817
|
+
!companyClaimsMatch(idClaims, creds, expectedClientId) ||
|
|
818
|
+
!companyClaimsMatch(accessClaims, creds, expectedClientId) ||
|
|
819
|
+
idClaims["custom:ownerCompanyUid"] !== accessClaims["custom:ownerCompanyUid"])
|
|
820
|
+
return false;
|
|
821
|
+
}
|
|
822
|
+
else if (hasCompanyMarkers(idClaims) || hasCompanyMarkers(accessClaims))
|
|
823
|
+
return false;
|
|
762
824
|
const stringClaim = (claims, key) => {
|
|
763
825
|
const value = claims[key];
|
|
764
826
|
return typeof value === "string" && value.length > 0 ? value : null;
|
|
@@ -774,9 +836,10 @@ function machineTokenPairMatchesIdentity(tokens, creds, expectedClientId) {
|
|
|
774
836
|
const entityUid = stringClaim(idClaims, "custom:entityUid");
|
|
775
837
|
const delegatedSub = stringClaim(idClaims, "custom:delegatedSub");
|
|
776
838
|
const delegatedEmail = stringClaim(idClaims, "custom:delegatedEmail");
|
|
777
|
-
const hasValidMachineClaimShape =
|
|
778
|
-
|
|
779
|
-
|
|
839
|
+
const hasValidMachineClaimShape = entityType === "company-outpost" ||
|
|
840
|
+
(entityType === "agent" &&
|
|
841
|
+
entityUid !== null &&
|
|
842
|
+
entityUid.startsWith("agt_")) ||
|
|
780
843
|
(entityType === "outpost" &&
|
|
781
844
|
entityUid !== null &&
|
|
782
845
|
entityUid.startsWith("out_") &&
|
|
@@ -837,6 +900,12 @@ export function loadMachineCreds() {
|
|
|
837
900
|
if (!fs.existsSync(file))
|
|
838
901
|
return null;
|
|
839
902
|
const raw = JSON.parse(fs.readFileSync(file, "utf-8"));
|
|
903
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
904
|
+
return null;
|
|
905
|
+
if (companyMachineRequired() && raw.entityType !== "company-outpost")
|
|
906
|
+
return null;
|
|
907
|
+
if (raw.entityType === "company-outpost" && !validCompanyCredentials(raw))
|
|
908
|
+
return null;
|
|
840
909
|
// `runtime` is a closed set: anything else is a malformed file, not a
|
|
841
910
|
// silent downgrade to hosted (a typo must never bypass host-key signing).
|
|
842
911
|
if (raw.runtime !== undefined &&
|
|
@@ -864,7 +933,7 @@ export function loadMachineCreds() {
|
|
|
864
933
|
typeof raw.entityUid === "string" &&
|
|
865
934
|
raw.entityUid.startsWith("agt_") &&
|
|
866
935
|
raw.entityUid.length > "agt_".length) ||
|
|
867
|
-
(raw.entityType === "outpost" &&
|
|
936
|
+
((raw.entityType === "outpost" || raw.entityType === "company-outpost") &&
|
|
868
937
|
typeof raw.entityUid === "string" &&
|
|
869
938
|
raw.entityUid.startsWith("out_") &&
|
|
870
939
|
raw.entityUid.length > "out_".length);
|
|
@@ -878,7 +947,7 @@ export function loadMachineCreds() {
|
|
|
878
947
|
secret: raw.secret,
|
|
879
948
|
clientId: typeof raw.clientId === "string" ? raw.clientId : undefined,
|
|
880
949
|
region: typeof raw.region === "string" ? raw.region : undefined,
|
|
881
|
-
...(raw.entityType === "agent" || raw.entityType === "outpost"
|
|
950
|
+
...(raw.entityType === "agent" || raw.entityType === "outpost" || raw.entityType === "company-outpost"
|
|
882
951
|
? { entityType: raw.entityType }
|
|
883
952
|
: {}),
|
|
884
953
|
...(typeof raw.entityUid === "string"
|
|
@@ -955,7 +1024,18 @@ export function isMachineIdentity() {
|
|
|
955
1024
|
*/
|
|
956
1025
|
export function machineIdentityRequired() {
|
|
957
1026
|
const raw = process.env.HQ_REQUIRE_MACHINE_IDENTITY?.trim().toLowerCase();
|
|
958
|
-
return raw === "1" || raw === "true" || raw === "yes"
|
|
1027
|
+
return raw === "1" || raw === "true" || raw === "yes" ||
|
|
1028
|
+
companyMachineRequired();
|
|
1029
|
+
}
|
|
1030
|
+
/** Company bootstrap contract; the older switch continues to accept agents. */
|
|
1031
|
+
function companyMachineRequired() {
|
|
1032
|
+
const required = process.env.HQ_MACHINE_AUTH_REQUIRED?.trim().toLowerCase();
|
|
1033
|
+
return required === "1" || required === "true" || required === "yes";
|
|
1034
|
+
}
|
|
1035
|
+
function assertHumanAuthenticationAllowed() {
|
|
1036
|
+
if (companyMachineRequired()) {
|
|
1037
|
+
throw new CognitoAuthError("Human authentication is unavailable in required company machine mode");
|
|
1038
|
+
}
|
|
959
1039
|
}
|
|
960
1040
|
/**
|
|
961
1041
|
* Throw a typed, diagnosable error when machine identity is REQUIRED but the
|
|
@@ -971,7 +1051,7 @@ export function assertMachineIdentityWhenRequired() {
|
|
|
971
1051
|
const source = explicit !== undefined && explicit.length > 0
|
|
972
1052
|
? "HQ_MACHINE_CREDS_FILE"
|
|
973
1053
|
: `os.homedir()=${os.homedir()}`;
|
|
974
|
-
throw new CognitoAuthError(`HQ_REQUIRE_MACHINE_IDENTITY is set but no usable machine credentials were ` +
|
|
1054
|
+
throw new CognitoAuthError(`HQ_REQUIRE_MACHINE_IDENTITY or HQ_MACHINE_AUTH_REQUIRED is set but no usable machine credentials were ` +
|
|
975
1055
|
`found at ${machineCredsFilePath()} (path from ${source}). Refusing to ` +
|
|
976
1056
|
`fall back to an interactive human session. Set HQ_MACHINE_CREDS_FILE to ` +
|
|
977
1057
|
`the credentials file, or run with the HOME that owns it.`);
|
|
@@ -1012,11 +1092,19 @@ function backoffDelayMs(attempt, opts) {
|
|
|
1012
1092
|
* full jitter (transient, per-user rate limit); every other non-OK response
|
|
1013
1093
|
* (bad creds, disabled flow) is a hard error surfaced immediately.
|
|
1014
1094
|
*/
|
|
1015
|
-
async function mintMachineTokensUnlocked(config, creds, retry) {
|
|
1095
|
+
async function mintMachineTokensUnlocked(config, creds, retry, assertCurrent = () => { }) {
|
|
1096
|
+
assertCurrent();
|
|
1016
1097
|
const machineCreds = creds ?? loadMachineCreds();
|
|
1017
1098
|
if (!machineCreds) {
|
|
1018
1099
|
throw new CognitoAuthError(`No machine credentials found at ${machineCredsFilePath()}`);
|
|
1019
1100
|
}
|
|
1101
|
+
if (companyMachineRequired() && machineCreds.entityType !== "company-outpost") {
|
|
1102
|
+
throw new CognitoAuthError("Company machine authentication requires company-outpost credentials");
|
|
1103
|
+
}
|
|
1104
|
+
if (machineCreds.entityType === "company-outpost" &&
|
|
1105
|
+
!validCompanyCredentials({ ...machineCreds })) {
|
|
1106
|
+
throw new CognitoAuthError("Invalid company machine credentials");
|
|
1107
|
+
}
|
|
1020
1108
|
// Prefer the creds file's own clientId/region: provisioning pairs the
|
|
1021
1109
|
// machine user with an app client that has USER_PASSWORD_AUTH enabled,
|
|
1022
1110
|
// which the CLI's default (browser) client may not.
|
|
@@ -1026,7 +1114,11 @@ async function mintMachineTokensUnlocked(config, creds, retry) {
|
|
|
1026
1114
|
// External agents never talk to Cognito directly: hq-pro performs the
|
|
1027
1115
|
// USER_PASSWORD_AUTH on their behalf once they prove the host key.
|
|
1028
1116
|
if (isExternalMachineCreds(machineCreds)) {
|
|
1029
|
-
|
|
1117
|
+
const tokens = await mintExternalMachineTokens(machineCreds, clientId, retryOpts);
|
|
1118
|
+
assertCurrent();
|
|
1119
|
+
tokens.machineCredentialKey = machineIdentityKey(config, machineCreds);
|
|
1120
|
+
saveCachedTokens(tokens);
|
|
1121
|
+
return tokens;
|
|
1030
1122
|
}
|
|
1031
1123
|
let lastError = null;
|
|
1032
1124
|
for (let attempt = 0; attempt <= retryOpts.maxRetries; attempt++) {
|
|
@@ -1074,6 +1166,8 @@ async function mintMachineTokensUnlocked(config, creds, retry) {
|
|
|
1074
1166
|
if (!machineTokenPairMatchesIdentity(tokens, machineCreds, clientId)) {
|
|
1075
1167
|
throw new CognitoAuthError("Machine token mint returned tokens that do not match the requested machine identity");
|
|
1076
1168
|
}
|
|
1169
|
+
assertCurrent();
|
|
1170
|
+
tokens.machineCredentialKey = machineIdentityKey(config, machineCreds);
|
|
1077
1171
|
saveCachedTokens(tokens);
|
|
1078
1172
|
return tokens;
|
|
1079
1173
|
}
|
|
@@ -1185,7 +1279,6 @@ async function mintExternalMachineTokens(creds, clientId, retryOpts) {
|
|
|
1185
1279
|
if (!machineTokenPairMatchesIdentity(tokens, creds, clientId)) {
|
|
1186
1280
|
throw new CognitoAuthError("External machine token mint returned tokens that do not match the requested machine identity");
|
|
1187
1281
|
}
|
|
1188
|
-
saveCachedTokens(tokens);
|
|
1189
1282
|
return tokens;
|
|
1190
1283
|
}
|
|
1191
1284
|
throw lastError ?? new CognitoAuthError("External machine token mint failed: throttled");
|
|
@@ -1196,7 +1289,21 @@ async function mintExternalMachineTokens(creds, clientId, retryOpts) {
|
|
|
1196
1289
|
* race another process's cache write.
|
|
1197
1290
|
*/
|
|
1198
1291
|
export async function mintMachineTokens(config, creds, retry) {
|
|
1199
|
-
|
|
1292
|
+
const selected = creds ?? loadMachineCreds();
|
|
1293
|
+
if (!selected)
|
|
1294
|
+
throw new CognitoAuthError("No usable machine credentials found");
|
|
1295
|
+
const identityKey = machineIdentityKey(config, selected);
|
|
1296
|
+
const companyContext = companyMachineRequired() || selected.entityType === "company-outpost";
|
|
1297
|
+
const fileBound = !creds || loadMachineCreds() !== null || machineIdentityRequired();
|
|
1298
|
+
const assertCurrent = () => {
|
|
1299
|
+
if (!companyContext && !companyMachineRequired())
|
|
1300
|
+
return;
|
|
1301
|
+
const current = fileBound ? loadMachineCreds() : selected;
|
|
1302
|
+
if (!current || machineIdentityKey(config, current) !== identityKey) {
|
|
1303
|
+
throw new CognitoAuthError("Machine credentials changed during authentication; retry with the current identity");
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
return withRefreshLock("machine", () => mintMachineTokensUnlocked(config, selected, retry, assertCurrent));
|
|
1200
1307
|
}
|
|
1201
1308
|
let inProcessMachineTokens = null;
|
|
1202
1309
|
const inFlightMachineMints = new Map();
|
|
@@ -1204,12 +1311,17 @@ export const DEFAULT_MACHINE_TOKEN_MIN_VALIDITY_SECONDS = 360;
|
|
|
1204
1311
|
// Machine sessions last one hour; a larger requirement would re-mint on every call.
|
|
1205
1312
|
export const MAX_MACHINE_TOKEN_MIN_VALIDITY_SECONDS = 1800;
|
|
1206
1313
|
function machineIdentityKey(config, creds) {
|
|
1207
|
-
//
|
|
1208
|
-
//
|
|
1209
|
-
//
|
|
1314
|
+
// Hash only credential fields that change the effective mint, plus the
|
|
1315
|
+
// company-env bindings that scope a company-outpost token. Env-dependent
|
|
1316
|
+
// process state (HQ_REQUIRE_MACHINE_IDENTITY, HQ_MACHINE_AUTH_REQUIRED,
|
|
1317
|
+
// credential/cache paths) is intentionally excluded so sibling processes
|
|
1318
|
+
// sharing the same creds file compute the same key and share the cache.
|
|
1210
1319
|
return crypto
|
|
1211
1320
|
.createHash("sha256")
|
|
1212
1321
|
.update(JSON.stringify({
|
|
1322
|
+
companyUid: process.env.HQ_COMPANY_UID ?? null,
|
|
1323
|
+
outpostId: process.env.HQ_COMPANY_OUTPOST_ID ?? null,
|
|
1324
|
+
attempt: process.env.HQ_COMPANY_OUTPOST_ATTEMPT ?? null,
|
|
1213
1325
|
username: creds.username,
|
|
1214
1326
|
secret: creds.secret,
|
|
1215
1327
|
clientId: creds.clientId ?? config.clientId,
|
|
@@ -1241,9 +1353,32 @@ export async function getValidMachineTokens(config, options = {}) {
|
|
|
1241
1353
|
}
|
|
1242
1354
|
const expectedClientId = machineCreds.clientId ?? config.clientId;
|
|
1243
1355
|
const identityKey = machineIdentityKey(config, machineCreds);
|
|
1356
|
+
const companyContext = companyMachineRequired() || machineCreds.entityType === "company-outpost";
|
|
1357
|
+
const assertCurrent = () => {
|
|
1358
|
+
if (!companyContext && !companyMachineRequired())
|
|
1359
|
+
return;
|
|
1360
|
+
const current = loadMachineCreds();
|
|
1361
|
+
if (!current || machineIdentityKey(config, current) !== identityKey) {
|
|
1362
|
+
throw new CognitoAuthError("Machine credentials changed during authentication; retry with the current identity");
|
|
1363
|
+
}
|
|
1364
|
+
};
|
|
1365
|
+
// Only company-outpost identities (or company-required mode) enforce exact
|
|
1366
|
+
// machineCredentialKey equality. Agent/outpost identities fall back to the
|
|
1367
|
+
// pre-branch behavior: a claims match on the cached token is sufficient,
|
|
1368
|
+
// regardless of which process minted it. This lets agent-box siblings
|
|
1369
|
+
// (mesh daemon, agent-inbox, bot, mcp op) share one cache even when they
|
|
1370
|
+
// disagree on HQ_REQUIRE_MACHINE_IDENTITY or HQ_MACHINE_CREDS_FILE.
|
|
1371
|
+
const requiresKeyEquality = machineCreds.entityType === "company-outpost" || companyMachineRequired();
|
|
1372
|
+
const cacheMatches = (tokens) => {
|
|
1373
|
+
if (requiresKeyEquality && tokens.machineCredentialKey !== identityKey) {
|
|
1374
|
+
return false;
|
|
1375
|
+
}
|
|
1376
|
+
return machineTokenPairMatchesIdentity(tokens, machineCreds, expectedClientId);
|
|
1377
|
+
};
|
|
1244
1378
|
const resolutionKey = `${identityKey}\0${minValiditySeconds}`;
|
|
1245
1379
|
// Hot path: a live in-memory token serves every request after the first.
|
|
1246
1380
|
if (inProcessMachineTokens?.identityKey === identityKey &&
|
|
1381
|
+
!tokenIsInvalidated(inProcessMachineTokens.tokens.accessToken, "machine") &&
|
|
1247
1382
|
!isExpiring(inProcessMachineTokens.tokens, minValiditySeconds) &&
|
|
1248
1383
|
machineTokenPairMatchesIdentity(inProcessMachineTokens.tokens, machineCreds, expectedClientId)) {
|
|
1249
1384
|
return inProcessMachineTokens.tokens;
|
|
@@ -1252,10 +1387,14 @@ export async function getValidMachineTokens(config, options = {}) {
|
|
|
1252
1387
|
// The assignment below completes before the first `await` yields, so sibling
|
|
1253
1388
|
// callers in the same tick see the in-flight promise and reuse it.
|
|
1254
1389
|
const existingResolution = inFlightMachineMints.get(resolutionKey);
|
|
1255
|
-
if (existingResolution)
|
|
1256
|
-
|
|
1390
|
+
if (existingResolution) {
|
|
1391
|
+
const tokens = await existingResolution;
|
|
1392
|
+
assertCurrent();
|
|
1393
|
+
return tokens;
|
|
1394
|
+
}
|
|
1257
1395
|
const resolution = (async () => {
|
|
1258
1396
|
const resolveUnderLock = async () => {
|
|
1397
|
+
assertCurrent();
|
|
1259
1398
|
// Re-read only after acquiring the process-shared lock. A different hq
|
|
1260
1399
|
// process may have minted and saved this machine's session while this
|
|
1261
1400
|
// caller was waiting.
|
|
@@ -1265,7 +1404,7 @@ export async function getValidMachineTokens(config, options = {}) {
|
|
|
1265
1404
|
// clientId wins over config), and require the cached ID token to prove
|
|
1266
1405
|
// the exact machine identity. Opaque/missing/human-shaped claims are
|
|
1267
1406
|
// treated as stale.
|
|
1268
|
-
if (
|
|
1407
|
+
if (cacheMatches(cached)) {
|
|
1269
1408
|
if (cached.refreshToken === "")
|
|
1270
1409
|
return cached;
|
|
1271
1410
|
const normalized = { ...cached, refreshToken: "" };
|
|
@@ -1275,7 +1414,7 @@ export async function getValidMachineTokens(config, options = {}) {
|
|
|
1275
1414
|
}
|
|
1276
1415
|
// mintMachineTokens persists before returning, so the complete shared
|
|
1277
1416
|
// cache read -> USER_PASSWORD_AUTH mint -> save sequence is serialized.
|
|
1278
|
-
return mintMachineTokensUnlocked(config, machineCreds);
|
|
1417
|
+
return mintMachineTokensUnlocked(config, machineCreds, undefined, assertCurrent);
|
|
1279
1418
|
};
|
|
1280
1419
|
try {
|
|
1281
1420
|
return await withRefreshLock("machine", resolveUnderLock);
|
|
@@ -1287,7 +1426,7 @@ export async function getValidMachineTokens(config, options = {}) {
|
|
|
1287
1426
|
// session, reuse it; otherwise surface a retryable auth failure.
|
|
1288
1427
|
const cached = loadCachedMachineTokens();
|
|
1289
1428
|
if (cached && !isExpiring(cached, minValiditySeconds)) {
|
|
1290
|
-
if (
|
|
1429
|
+
if (cacheMatches(cached)) {
|
|
1291
1430
|
// The successful path always leaves a machine cache with no refresh
|
|
1292
1431
|
// token. If this is legacy state, only normalize it while holding the
|
|
1293
1432
|
// lock; after a timeout we must not race the live holder's write.
|
|
@@ -1306,6 +1445,7 @@ export async function getValidMachineTokens(config, options = {}) {
|
|
|
1306
1445
|
inFlightMachineMints.set(resolutionKey, resolution);
|
|
1307
1446
|
try {
|
|
1308
1447
|
const tokens = await resolution;
|
|
1448
|
+
assertCurrent();
|
|
1309
1449
|
inProcessMachineTokens = { identityKey, tokens };
|
|
1310
1450
|
return tokens;
|
|
1311
1451
|
}
|
|
@@ -1351,6 +1491,7 @@ function redirectUri(port) {
|
|
|
1351
1491
|
* Times out after 5 minutes if the user doesn't complete the flow.
|
|
1352
1492
|
*/
|
|
1353
1493
|
export async function browserLogin(config) {
|
|
1494
|
+
assertHumanAuthenticationAllowed();
|
|
1354
1495
|
const port = config.port ?? 3000;
|
|
1355
1496
|
const scopes = (config.scopes ?? ["openid", "email", "profile"]).join(" ");
|
|
1356
1497
|
const { verifier, challenge } = generatePkce();
|
|
@@ -1372,7 +1513,9 @@ export async function browserLogin(config) {
|
|
|
1372
1513
|
authUrl.searchParams.set("prompt", config.prompt);
|
|
1373
1514
|
}
|
|
1374
1515
|
const code = await waitForAuthCode(port, state);
|
|
1516
|
+
assertHumanAuthenticationAllowed();
|
|
1375
1517
|
const tokens = await exchangeCodeForTokens(config, code, verifier, port);
|
|
1518
|
+
assertHumanAuthenticationAllowed();
|
|
1376
1519
|
saveCachedTokens(tokens);
|
|
1377
1520
|
return tokens;
|
|
1378
1521
|
// -- inner: spin up loopback server and open browser ---------------------
|
|
@@ -1428,6 +1571,11 @@ export async function browserLogin(config) {
|
|
|
1428
1571
|
reject(err);
|
|
1429
1572
|
});
|
|
1430
1573
|
server.listen(port, "127.0.0.1", () => {
|
|
1574
|
+
if (companyMachineRequired()) {
|
|
1575
|
+
cleanup();
|
|
1576
|
+
reject(new CognitoAuthError("Human authentication is unavailable in required company machine mode"));
|
|
1577
|
+
return;
|
|
1578
|
+
}
|
|
1431
1579
|
console.log(`\n Opening browser for HQ sign-in...`);
|
|
1432
1580
|
console.log(` If your browser doesn't open, visit:\n ${authUrl.toString()}\n`);
|
|
1433
1581
|
open(authUrl.toString()).catch(() => {
|
|
@@ -1494,7 +1642,9 @@ function cachedIsForClient(cached, config) {
|
|
|
1494
1642
|
* session (AWS). Durable never-log-out is per-box machine identity (ADR-0009).
|
|
1495
1643
|
*/
|
|
1496
1644
|
export async function refreshTokens(config, currentRefreshToken, currentAccessToken) {
|
|
1645
|
+
assertHumanAuthenticationAllowed();
|
|
1497
1646
|
const doRefresh = async () => {
|
|
1647
|
+
assertHumanAuthenticationAllowed();
|
|
1498
1648
|
const cached = loadCachedTokens();
|
|
1499
1649
|
// Reuse a token another refresher just produced — but ONLY when it belongs
|
|
1500
1650
|
// to THIS app client (a direct caller may target a different pool than the
|
|
@@ -1527,6 +1677,7 @@ export async function refreshTokens(config, currentRefreshToken, currentAccessTo
|
|
|
1527
1677
|
});
|
|
1528
1678
|
let lastFailure;
|
|
1529
1679
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
1680
|
+
assertHumanAuthenticationAllowed();
|
|
1530
1681
|
let res;
|
|
1531
1682
|
try {
|
|
1532
1683
|
res = await fetch(`${authBaseUrl(config)}/oauth2/token`, {
|
|
@@ -1576,6 +1727,7 @@ export async function refreshTokens(config, currentRefreshToken, currentAccessTo
|
|
|
1576
1727
|
throw lastFailure;
|
|
1577
1728
|
}
|
|
1578
1729
|
const data = (await res.json());
|
|
1730
|
+
assertHumanAuthenticationAllowed();
|
|
1579
1731
|
const tokens = {
|
|
1580
1732
|
accessToken: data.access_token,
|
|
1581
1733
|
idToken: data.id_token,
|
|
@@ -1593,7 +1745,9 @@ export async function refreshTokens(config, currentRefreshToken, currentAccessTo
|
|
|
1593
1745
|
new CognitoRefreshError("Cognito refresh failed after retry", false));
|
|
1594
1746
|
};
|
|
1595
1747
|
try {
|
|
1596
|
-
|
|
1748
|
+
const tokens = await withRefreshLock("person", doRefresh);
|
|
1749
|
+
assertHumanAuthenticationAllowed();
|
|
1750
|
+
return tokens;
|
|
1597
1751
|
}
|
|
1598
1752
|
catch (err) {
|
|
1599
1753
|
if (!(err instanceof RefreshLockTimeoutError))
|