@sjawhar/pi-legion 19.26.0 → 19.27.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.
- package/dist/legion.js +930 -91
- package/package.json +1 -1
package/dist/legion.js
CHANGED
|
@@ -14065,6 +14065,9 @@ function fileBearer(bearer, url, section, env, commands = "run") {
|
|
|
14065
14065
|
return fileValue(bearer.token, bearer.field, bearer.path, env, commands);
|
|
14066
14066
|
}
|
|
14067
14067
|
|
|
14068
|
+
// ../broker-client/src/dpop.ts
|
|
14069
|
+
import { createPrivateKey, createPublicKey } from "crypto";
|
|
14070
|
+
|
|
14068
14071
|
// ../../node_modules/.bun/jose@6.2.9/node_modules/jose/dist/webapi/lib/buffer_utils.js
|
|
14069
14072
|
var encoder = new TextEncoder;
|
|
14070
14073
|
var decoder = new TextDecoder;
|
|
@@ -14911,6 +14914,112 @@ function keyAlgorithm(alg, source) {
|
|
|
14911
14914
|
return (typeof alg === "string" ? JWS[alg] ?? JWE[alg] : undefined) ?? unsupportedAlg(source);
|
|
14912
14915
|
}
|
|
14913
14916
|
|
|
14917
|
+
// ../../node_modules/.bun/jose@6.2.9/node_modules/jose/dist/webapi/lib/asn1.js
|
|
14918
|
+
var bytesEqual = (a, b) => {
|
|
14919
|
+
if (a.byteLength !== b.length)
|
|
14920
|
+
return false;
|
|
14921
|
+
for (let i = 0;i < a.byteLength; i++) {
|
|
14922
|
+
if (a[i] !== b[i])
|
|
14923
|
+
return false;
|
|
14924
|
+
}
|
|
14925
|
+
return true;
|
|
14926
|
+
};
|
|
14927
|
+
var createASN1State = (data) => ({ data, pos: 0 });
|
|
14928
|
+
var readByte = (state) => {
|
|
14929
|
+
const byte = state.data[state.pos++];
|
|
14930
|
+
if (byte === undefined) {
|
|
14931
|
+
throw new Error("Unexpected end of ASN.1 input");
|
|
14932
|
+
}
|
|
14933
|
+
return byte;
|
|
14934
|
+
};
|
|
14935
|
+
var parseLength = (state) => {
|
|
14936
|
+
const first = readByte(state);
|
|
14937
|
+
if (first & 128) {
|
|
14938
|
+
const lengthOfLen = first & 127;
|
|
14939
|
+
let length = 0;
|
|
14940
|
+
for (let i = 0;i < lengthOfLen; i++) {
|
|
14941
|
+
length = length << 8 | readByte(state);
|
|
14942
|
+
}
|
|
14943
|
+
return length;
|
|
14944
|
+
}
|
|
14945
|
+
return first;
|
|
14946
|
+
};
|
|
14947
|
+
var expectTag = (state, expectedTag, errorMessage) => {
|
|
14948
|
+
if (readByte(state) !== expectedTag) {
|
|
14949
|
+
throw new Error(errorMessage);
|
|
14950
|
+
}
|
|
14951
|
+
};
|
|
14952
|
+
var getSubarray = (state, length) => {
|
|
14953
|
+
if (length < 0 || state.pos + length > state.data.length) {
|
|
14954
|
+
throw new Error("Unexpected end of ASN.1 input");
|
|
14955
|
+
}
|
|
14956
|
+
const result = state.data.subarray(state.pos, state.pos + length);
|
|
14957
|
+
state.pos += length;
|
|
14958
|
+
return result;
|
|
14959
|
+
};
|
|
14960
|
+
var parseAlgorithmOID = (state) => {
|
|
14961
|
+
expectTag(state, 6, "Expected algorithm OID");
|
|
14962
|
+
const oidLen = parseLength(state);
|
|
14963
|
+
return getSubarray(state, oidLen);
|
|
14964
|
+
};
|
|
14965
|
+
function parseKeyHeader(state, keyFormat) {
|
|
14966
|
+
expectTag(state, 48, `Invalid ${keyFormat === "spki" ? "SPKI" : "PKCS#8"} structure`);
|
|
14967
|
+
parseLength(state);
|
|
14968
|
+
if (keyFormat === "pkcs8") {
|
|
14969
|
+
expectTag(state, 2, "Expected version field");
|
|
14970
|
+
const length = parseLength(state);
|
|
14971
|
+
state.pos += length;
|
|
14972
|
+
}
|
|
14973
|
+
expectTag(state, 48, "Expected algorithm identifier");
|
|
14974
|
+
parseLength(state);
|
|
14975
|
+
}
|
|
14976
|
+
var parseECAlgorithmIdentifier = (state) => {
|
|
14977
|
+
const algOid = parseAlgorithmOID(state);
|
|
14978
|
+
if (bytesEqual(algOid, [43, 101, 110])) {
|
|
14979
|
+
return "X25519";
|
|
14980
|
+
}
|
|
14981
|
+
if (!bytesEqual(algOid, [42, 134, 72, 206, 61, 2, 1])) {
|
|
14982
|
+
throw new Error("Unsupported key algorithm");
|
|
14983
|
+
}
|
|
14984
|
+
expectTag(state, 6, "Expected curve OID");
|
|
14985
|
+
const curveOidLen = parseLength(state);
|
|
14986
|
+
const curveOid = getSubarray(state, curveOidLen);
|
|
14987
|
+
if (bytesEqual(curveOid, [42, 134, 72, 206, 61, 3, 1, 7]))
|
|
14988
|
+
return "P-256";
|
|
14989
|
+
if (bytesEqual(curveOid, [43, 129, 4, 0, 34]))
|
|
14990
|
+
return "P-384";
|
|
14991
|
+
if (bytesEqual(curveOid, [43, 129, 4, 0, 35]))
|
|
14992
|
+
return "P-521";
|
|
14993
|
+
throw new Error("Unsupported named curve");
|
|
14994
|
+
};
|
|
14995
|
+
var genericImport = async (keyFormat, keyData, alg, options) => {
|
|
14996
|
+
const entry = keyAlgorithm(alg, algArgument);
|
|
14997
|
+
if (entry.secret) {
|
|
14998
|
+
unsupportedAlg(algArgument);
|
|
14999
|
+
}
|
|
15000
|
+
const isPublic = keyFormat === "spki";
|
|
15001
|
+
let algorithm;
|
|
15002
|
+
if (entry.resolve) {
|
|
15003
|
+
try {
|
|
15004
|
+
const state = createASN1State(keyData);
|
|
15005
|
+
parseKeyHeader(state, keyFormat);
|
|
15006
|
+
algorithm = entry.resolve({ crv: parseECAlgorithmIdentifier(state) });
|
|
15007
|
+
} catch {
|
|
15008
|
+
throw new JOSENotSupported("Invalid or unsupported key format");
|
|
15009
|
+
}
|
|
15010
|
+
} else {
|
|
15011
|
+
algorithm = entry.subtle;
|
|
15012
|
+
}
|
|
15013
|
+
return crypto.subtle.importKey(keyFormat, keyData, algorithm, options?.extractable ?? isPublic, entry.usages[isPublic ? 0 : 1]);
|
|
15014
|
+
};
|
|
15015
|
+
var processPEMData = (pem, pattern) => {
|
|
15016
|
+
return decodeBase64(pem.replace(pattern, ""));
|
|
15017
|
+
};
|
|
15018
|
+
var fromPKCS8 = (pem, alg, options) => {
|
|
15019
|
+
const keyData = processPEMData(pem, /(?:-----(?:BEGIN|END) PRIVATE KEY-----|\s)/g);
|
|
15020
|
+
return genericImport("pkcs8", keyData, alg, options);
|
|
15021
|
+
};
|
|
15022
|
+
|
|
14914
15023
|
// ../../node_modules/.bun/jose@6.2.9/node_modules/jose/dist/webapi/key/export.js
|
|
14915
15024
|
function omitUndefinedProperties(jwk) {
|
|
14916
15025
|
return Object.fromEntries(Object.entries(jwk).filter(([, value]) => value !== undefined));
|
|
@@ -14944,6 +15053,13 @@ async function keyToJWK(key) {
|
|
|
14944
15053
|
function exportJWK(key) {
|
|
14945
15054
|
return keyToJWK(key);
|
|
14946
15055
|
}
|
|
15056
|
+
// ../../node_modules/.bun/jose@6.2.9/node_modules/jose/dist/webapi/key/import.js
|
|
15057
|
+
async function importPKCS8(pkcs8, alg, options) {
|
|
15058
|
+
if (typeof pkcs8 !== "string" || pkcs8.indexOf("-----BEGIN PRIVATE KEY-----") !== 0) {
|
|
15059
|
+
throw new TypeError('"pkcs8" must be PKCS#8 formatted string');
|
|
15060
|
+
}
|
|
15061
|
+
return fromPKCS8(pkcs8, alg, options);
|
|
15062
|
+
}
|
|
14947
15063
|
// ../../node_modules/.bun/jose@6.2.9/node_modules/jose/dist/webapi/key/generate_key_pair.js
|
|
14948
15064
|
function getModulusLengthOption(options) {
|
|
14949
15065
|
const modulusLength = options?.modulusLength ?? 2048;
|
|
@@ -14987,6 +15103,11 @@ async function generateKeyPair(alg, options) {
|
|
|
14987
15103
|
...entry.usages[0]
|
|
14988
15104
|
]);
|
|
14989
15105
|
}
|
|
15106
|
+
// ../broker-client/src/error-text.ts
|
|
15107
|
+
function messageOf(error) {
|
|
15108
|
+
return error instanceof Error ? error.message : String(error);
|
|
15109
|
+
}
|
|
15110
|
+
|
|
14990
15111
|
// ../broker-client/src/dpop.ts
|
|
14991
15112
|
var ALGORITHM = "ES256";
|
|
14992
15113
|
function targetUri(url) {
|
|
@@ -14999,9 +15120,7 @@ async function accessTokenHash(accessToken) {
|
|
|
14999
15120
|
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(accessToken));
|
|
15000
15121
|
return Buffer.from(digest).toString("base64url");
|
|
15001
15122
|
}
|
|
15002
|
-
|
|
15003
|
-
const { privateKey, publicKey } = await generateKeyPair(ALGORITHM, { extractable: false });
|
|
15004
|
-
const publicJwk = await exportJWK(publicKey);
|
|
15123
|
+
function dpopKey(privateKey, publicJwk) {
|
|
15005
15124
|
return {
|
|
15006
15125
|
publicJwk,
|
|
15007
15126
|
async proof(request) {
|
|
@@ -15020,6 +15139,30 @@ async function newDpopKey() {
|
|
|
15020
15139
|
}
|
|
15021
15140
|
};
|
|
15022
15141
|
}
|
|
15142
|
+
async function newDpopKey() {
|
|
15143
|
+
const { privateKey, publicKey } = await generateKeyPair(ALGORITHM, { extractable: false });
|
|
15144
|
+
return dpopKey(privateKey, await exportJWK(publicKey));
|
|
15145
|
+
}
|
|
15146
|
+
async function importDpopKey(pkcs8Pem, name) {
|
|
15147
|
+
const unreadable = (error) => new Error(`read ${name}: ${messageOf(error)}`, { cause: error });
|
|
15148
|
+
let exported;
|
|
15149
|
+
try {
|
|
15150
|
+
exported = createPublicKey(createPrivateKey(pkcs8Pem)).export({ format: "jwk" });
|
|
15151
|
+
} catch (error) {
|
|
15152
|
+
throw unreadable(error);
|
|
15153
|
+
}
|
|
15154
|
+
const { kty, crv, x, y } = exported;
|
|
15155
|
+
if (kty !== "EC" || crv !== "P-256" || typeof x !== "string" || typeof y !== "string") {
|
|
15156
|
+
throw new Error(`${name} is not a P-256 key (${kty} ${crv ?? "with no curve"}), and a DPoP proof is signed ${ALGORITHM}`);
|
|
15157
|
+
}
|
|
15158
|
+
let privateKey;
|
|
15159
|
+
try {
|
|
15160
|
+
privateKey = await importPKCS8(pkcs8Pem, ALGORITHM, { extractable: false });
|
|
15161
|
+
} catch (error) {
|
|
15162
|
+
throw unreadable(error);
|
|
15163
|
+
}
|
|
15164
|
+
return dpopKey(privateKey, { kty, crv, x, y });
|
|
15165
|
+
}
|
|
15023
15166
|
|
|
15024
15167
|
// ../broker-client/src/metadata-identity.ts
|
|
15025
15168
|
import { Agent, request } from "http";
|
|
@@ -15033,6 +15176,7 @@ var FORM_LAUNCHER_CREDENTIAL = "launcher_credential";
|
|
|
15033
15176
|
var FORM_LAUNCHER_CREDENTIAL_DECLINED = "false";
|
|
15034
15177
|
var SUBJECT_TOKEN_TYPE_KUBERNETES = "urn:envoy:params:oauth:token-type:kubernetes-service-account";
|
|
15035
15178
|
var SUBJECT_TOKEN_TYPE_GOOGLE = "urn:envoy:params:oauth:token-type:google-identity";
|
|
15179
|
+
var SUBJECT_TOKEN_TYPE_PERSON = "urn:envoy:params:oauth:token-type:person-google-identity";
|
|
15036
15180
|
var ACTOR_TOKEN_TYPE_MACHINE = "urn:envoy:params:oauth:token-type:machine-google-identity";
|
|
15037
15181
|
var TOKEN_TYPE_DPOP = "DPoP";
|
|
15038
15182
|
|
|
@@ -15094,29 +15238,6 @@ function metadataIdentity(audience, env = process.env) {
|
|
|
15094
15238
|
};
|
|
15095
15239
|
}
|
|
15096
15240
|
|
|
15097
|
-
// ../broker-client/src/projected-token.ts
|
|
15098
|
-
import { readFile } from "fs/promises";
|
|
15099
|
-
function projectedToken(path) {
|
|
15100
|
-
return {
|
|
15101
|
-
async read(signal) {
|
|
15102
|
-
let contents;
|
|
15103
|
-
try {
|
|
15104
|
-
contents = await readFile(path, {
|
|
15105
|
-
encoding: "utf8",
|
|
15106
|
-
...signal === undefined ? {} : { signal }
|
|
15107
|
-
});
|
|
15108
|
-
} catch (error) {
|
|
15109
|
-
throw new Error(`read the projected service-account token ${path}: ${error.message}`);
|
|
15110
|
-
}
|
|
15111
|
-
const token = contents.trim();
|
|
15112
|
-
if (token === "")
|
|
15113
|
-
throw new Error(`the projected service-account token ${path} is empty`);
|
|
15114
|
-
return { token, tokenType: SUBJECT_TOKEN_TYPE_KUBERNETES };
|
|
15115
|
-
},
|
|
15116
|
-
describe: () => `the projected service-account token ${path}`
|
|
15117
|
-
};
|
|
15118
|
-
}
|
|
15119
|
-
|
|
15120
15241
|
// ../../node_modules/.bun/oauth4webapi@3.8.8/node_modules/oauth4webapi/build/index.js
|
|
15121
15242
|
var USER_AGENT;
|
|
15122
15243
|
if (typeof navigator === "undefined" || !navigator.userAgent?.startsWith?.("Mozilla/5.0 ")) {
|
|
@@ -15262,6 +15383,56 @@ function signal(url, value) {
|
|
|
15262
15383
|
}
|
|
15263
15384
|
return;
|
|
15264
15385
|
}
|
|
15386
|
+
function replaceDoubleSlash(pathname) {
|
|
15387
|
+
if (pathname.includes("//")) {
|
|
15388
|
+
return pathname.replace("//", "/");
|
|
15389
|
+
}
|
|
15390
|
+
return pathname;
|
|
15391
|
+
}
|
|
15392
|
+
function prependWellKnown(url, wellKnown, allowTerminatingSlash = false) {
|
|
15393
|
+
if (url.pathname === "/") {
|
|
15394
|
+
url.pathname = wellKnown;
|
|
15395
|
+
} else {
|
|
15396
|
+
url.pathname = replaceDoubleSlash(`${wellKnown}/${allowTerminatingSlash ? url.pathname : url.pathname.replace(/(\/)$/, "")}`);
|
|
15397
|
+
}
|
|
15398
|
+
return url;
|
|
15399
|
+
}
|
|
15400
|
+
function appendWellKnown(url, wellKnown) {
|
|
15401
|
+
url.pathname = replaceDoubleSlash(`${url.pathname}/${wellKnown}`);
|
|
15402
|
+
return url;
|
|
15403
|
+
}
|
|
15404
|
+
async function performDiscovery(input, urlName, transform, options) {
|
|
15405
|
+
if (!(input instanceof URL)) {
|
|
15406
|
+
throw CodedTypeError(`"${urlName}" must be an instance of URL`, ERR_INVALID_ARG_TYPE);
|
|
15407
|
+
}
|
|
15408
|
+
checkProtocol(input, options?.[allowInsecureRequests] !== true);
|
|
15409
|
+
const url = transform(new URL(input.href));
|
|
15410
|
+
const headers = prepareHeaders(options?.headers);
|
|
15411
|
+
headers.set("accept", "application/json");
|
|
15412
|
+
return (options?.[customFetch] || fetch)(url.href, {
|
|
15413
|
+
body: undefined,
|
|
15414
|
+
headers: Object.fromEntries(headers.entries()),
|
|
15415
|
+
method: "GET",
|
|
15416
|
+
redirect: "manual",
|
|
15417
|
+
signal: signal(url, options?.signal)
|
|
15418
|
+
});
|
|
15419
|
+
}
|
|
15420
|
+
async function discoveryRequest(issuerIdentifier, options) {
|
|
15421
|
+
return performDiscovery(issuerIdentifier, "issuerIdentifier", (url) => {
|
|
15422
|
+
switch (options?.algorithm) {
|
|
15423
|
+
case undefined:
|
|
15424
|
+
case "oidc":
|
|
15425
|
+
appendWellKnown(url, ".well-known/openid-configuration");
|
|
15426
|
+
break;
|
|
15427
|
+
case "oauth2":
|
|
15428
|
+
prependWellKnown(url, ".well-known/oauth-authorization-server");
|
|
15429
|
+
break;
|
|
15430
|
+
default:
|
|
15431
|
+
throw CodedTypeError('"options.algorithm" must be "oidc" (default), or "oauth2"', ERR_INVALID_ARG_VALUE);
|
|
15432
|
+
}
|
|
15433
|
+
return url;
|
|
15434
|
+
}, options);
|
|
15435
|
+
}
|
|
15265
15436
|
function assertNumber(input, allow0, it, code, cause) {
|
|
15266
15437
|
try {
|
|
15267
15438
|
if (typeof input !== "number" || !Number.isFinite(input)) {
|
|
@@ -15298,6 +15469,25 @@ function assertString(input, it, code, cause) {
|
|
|
15298
15469
|
throw err;
|
|
15299
15470
|
}
|
|
15300
15471
|
}
|
|
15472
|
+
async function processDiscoveryResponse(expectedIssuerIdentifier, response) {
|
|
15473
|
+
const expected = expectedIssuerIdentifier;
|
|
15474
|
+
if (!(expected instanceof URL) && expected !== _nodiscoverycheck) {
|
|
15475
|
+
throw CodedTypeError('"expectedIssuerIdentifier" must be an instance of URL', ERR_INVALID_ARG_TYPE);
|
|
15476
|
+
}
|
|
15477
|
+
if (!looseInstanceOf(response, Response)) {
|
|
15478
|
+
throw CodedTypeError('"response" must be an instance of Response', ERR_INVALID_ARG_TYPE);
|
|
15479
|
+
}
|
|
15480
|
+
if (response.status !== 200) {
|
|
15481
|
+
throw OPE('"response" is not a conform Authorization Server Metadata response (unexpected HTTP status code)', RESPONSE_IS_NOT_CONFORM, response);
|
|
15482
|
+
}
|
|
15483
|
+
assertReadableResponse(response);
|
|
15484
|
+
const json = await getResponseJsonBody(response);
|
|
15485
|
+
assertString(json.issuer, '"response" body "issuer" property', INVALID_RESPONSE, { body: json });
|
|
15486
|
+
if (expected !== _nodiscoverycheck && new URL(json.issuer).href !== expected.href) {
|
|
15487
|
+
throw OPE('"response" body "issuer" property does not match the expected value', JSON_ATTRIBUTE_COMPARISON, { expected: expected.href, body: json, attribute: "issuer" });
|
|
15488
|
+
}
|
|
15489
|
+
return json;
|
|
15490
|
+
}
|
|
15301
15491
|
function assertApplicationJson(response) {
|
|
15302
15492
|
assertContentType(response, "application/json");
|
|
15303
15493
|
}
|
|
@@ -15577,6 +15767,14 @@ async function tokenEndpointRequest(as, client, clientAuthentication, grantType,
|
|
|
15577
15767
|
options?.DPoP?.cacheNonce(response, url);
|
|
15578
15768
|
return response;
|
|
15579
15769
|
}
|
|
15770
|
+
async function refreshTokenGrantRequest(as, client, clientAuthentication, refreshToken, options) {
|
|
15771
|
+
assertAs(as);
|
|
15772
|
+
assertClient(client);
|
|
15773
|
+
assertString(refreshToken, '"refreshToken"');
|
|
15774
|
+
const parameters = new URLSearchParams(options?.additionalParameters);
|
|
15775
|
+
parameters.set("refresh_token", refreshToken);
|
|
15776
|
+
return tokenEndpointRequest(as, client, clientAuthentication, "refresh_token", parameters, options);
|
|
15777
|
+
}
|
|
15580
15778
|
var idTokenClaims = new WeakMap;
|
|
15581
15779
|
var jwtRefs = new WeakMap;
|
|
15582
15780
|
function getValidatedIdTokenClaims(ref) {
|
|
@@ -15654,6 +15852,9 @@ function checkAuthenticationChallenges(response) {
|
|
|
15654
15852
|
throw new WWWAuthenticateChallengeError("server responded with a challenge in the WWW-Authenticate HTTP Header", { cause: challenges, response });
|
|
15655
15853
|
}
|
|
15656
15854
|
}
|
|
15855
|
+
async function processRefreshTokenResponse(as, client, response, options) {
|
|
15856
|
+
return processGenericAccessTokenResponse(as, client, response, undefined, options?.[jweDecrypt], options?.recognizedTokenTypes);
|
|
15857
|
+
}
|
|
15657
15858
|
function validateAudience(expected, result) {
|
|
15658
15859
|
if (Array.isArray(result.claims.aud)) {
|
|
15659
15860
|
if (!result.claims.aud.includes(expected)) {
|
|
@@ -16006,6 +16207,73 @@ function errorHandler(err) {
|
|
|
16006
16207
|
}
|
|
16007
16208
|
throw new ClientError("something went wrong", { cause: err });
|
|
16008
16209
|
}
|
|
16210
|
+
function handleEntraId(server, as, options) {
|
|
16211
|
+
if (server.origin === "https://login.microsoftonline.com" && (!options?.algorithm || options.algorithm === "oidc")) {
|
|
16212
|
+
as[kEntraId] = true;
|
|
16213
|
+
return true;
|
|
16214
|
+
}
|
|
16215
|
+
return false;
|
|
16216
|
+
}
|
|
16217
|
+
function handleB2Clogin(server, options) {
|
|
16218
|
+
if (server.hostname.endsWith(".b2clogin.com") && (!options?.algorithm || options.algorithm === "oidc")) {
|
|
16219
|
+
return true;
|
|
16220
|
+
}
|
|
16221
|
+
return false;
|
|
16222
|
+
}
|
|
16223
|
+
async function discovery(server, clientId, metadata, clientAuthentication, options) {
|
|
16224
|
+
const as = await performDiscovery2(server, options);
|
|
16225
|
+
const instance = new Configuration(as, clientId, metadata, clientAuthentication);
|
|
16226
|
+
let internals = int2(instance);
|
|
16227
|
+
if (options?.[customFetch2]) {
|
|
16228
|
+
internals.fetch = options[customFetch2];
|
|
16229
|
+
}
|
|
16230
|
+
if (options?.timeout) {
|
|
16231
|
+
internals.timeout = options.timeout;
|
|
16232
|
+
}
|
|
16233
|
+
if (options?.execute) {
|
|
16234
|
+
for (const extension of options.execute) {
|
|
16235
|
+
extension(instance);
|
|
16236
|
+
}
|
|
16237
|
+
}
|
|
16238
|
+
return instance;
|
|
16239
|
+
}
|
|
16240
|
+
async function performDiscovery2(server, options) {
|
|
16241
|
+
if (!(server instanceof URL)) {
|
|
16242
|
+
throw CodedTypeError2('"server" must be an instance of URL', ERR_INVALID_ARG_TYPE2);
|
|
16243
|
+
}
|
|
16244
|
+
const resolve = !server.href.includes("/.well-known/");
|
|
16245
|
+
const timeout = options?.timeout ?? 30;
|
|
16246
|
+
const signal = AbortSignal.timeout(timeout * 1000);
|
|
16247
|
+
const as = await (resolve ? discoveryRequest(server, {
|
|
16248
|
+
algorithm: options?.algorithm,
|
|
16249
|
+
[customFetch]: options?.[customFetch2],
|
|
16250
|
+
[allowInsecureRequests]: options?.execute?.includes(allowInsecureRequests2),
|
|
16251
|
+
signal,
|
|
16252
|
+
headers: new Headers(headers)
|
|
16253
|
+
}) : (options?.[customFetch2] || fetch)((() => {
|
|
16254
|
+
checkProtocol(server, options?.execute?.includes(allowInsecureRequests2) ? false : true);
|
|
16255
|
+
return server.href;
|
|
16256
|
+
})(), {
|
|
16257
|
+
headers: Object.fromEntries(new Headers({ accept: "application/json", ...headers }).entries()),
|
|
16258
|
+
body: undefined,
|
|
16259
|
+
method: "GET",
|
|
16260
|
+
redirect: "manual",
|
|
16261
|
+
signal
|
|
16262
|
+
})).then((response) => processDiscoveryResponse(_nodiscoverycheck, response)).catch(errorHandler);
|
|
16263
|
+
if (resolve && new URL(as.issuer).href !== server.href) {
|
|
16264
|
+
handleEntraId(server, as, options) || handleB2Clogin(server, options) || (() => {
|
|
16265
|
+
throw new ClientError("discovered metadata issuer does not match the expected issuer", {
|
|
16266
|
+
code: JSON_ATTRIBUTE_COMPARISON,
|
|
16267
|
+
cause: {
|
|
16268
|
+
expected: server.href,
|
|
16269
|
+
body: as,
|
|
16270
|
+
attribute: "issuer"
|
|
16271
|
+
}
|
|
16272
|
+
});
|
|
16273
|
+
})();
|
|
16274
|
+
}
|
|
16275
|
+
return as;
|
|
16276
|
+
}
|
|
16009
16277
|
function getServerHelpers(metadata) {
|
|
16010
16278
|
return {
|
|
16011
16279
|
supportsPKCE: {
|
|
@@ -16122,6 +16390,37 @@ function addHelpers(response) {
|
|
|
16122
16390
|
function allowInsecureRequests2(config) {
|
|
16123
16391
|
int2(config).tlsOnly = false;
|
|
16124
16392
|
}
|
|
16393
|
+
async function refreshTokenGrant(config, refreshToken, parameters, options) {
|
|
16394
|
+
checkConfig(config);
|
|
16395
|
+
parameters = new URLSearchParams(parameters);
|
|
16396
|
+
const { as, c, auth, fetch: fetch2, tlsOnly, nonRepudiation, timeout, decrypt } = int2(config);
|
|
16397
|
+
const response = await refreshTokenGrantRequest(as, c, auth, refreshToken, {
|
|
16398
|
+
[customFetch]: fetch2,
|
|
16399
|
+
[allowInsecureRequests]: !tlsOnly,
|
|
16400
|
+
additionalParameters: parameters,
|
|
16401
|
+
DPoP: options?.DPoP,
|
|
16402
|
+
headers: new Headers(headers),
|
|
16403
|
+
signal: signal2(timeout)
|
|
16404
|
+
}).catch(errorHandler);
|
|
16405
|
+
const p = processRefreshTokenResponse(as, c, response, {
|
|
16406
|
+
[jweDecrypt]: decrypt
|
|
16407
|
+
});
|
|
16408
|
+
let result;
|
|
16409
|
+
try {
|
|
16410
|
+
result = await p;
|
|
16411
|
+
} catch (err) {
|
|
16412
|
+
if (retryable(err, options)) {
|
|
16413
|
+
return refreshTokenGrant(config, refreshToken, parameters, {
|
|
16414
|
+
...options,
|
|
16415
|
+
flag: retry
|
|
16416
|
+
});
|
|
16417
|
+
}
|
|
16418
|
+
errorHandler(err);
|
|
16419
|
+
}
|
|
16420
|
+
result.id_token && await nonRepudiation?.(response);
|
|
16421
|
+
addHelpers(result);
|
|
16422
|
+
return result;
|
|
16423
|
+
}
|
|
16125
16424
|
function checkConfig(input) {
|
|
16126
16425
|
if (!(input instanceof Configuration)) {
|
|
16127
16426
|
throw CodedTypeError2('"config" must be an instance of Configuration', ERR_INVALID_ARG_TYPE2);
|
|
@@ -16175,6 +16474,136 @@ async function genericGrantRequest(config, grantType, parameters, options) {
|
|
|
16175
16474
|
return result;
|
|
16176
16475
|
}
|
|
16177
16476
|
|
|
16477
|
+
// ../broker-client/src/signins.ts
|
|
16478
|
+
import { readdir, readFile } from "fs/promises";
|
|
16479
|
+
import { join as join2 } from "path";
|
|
16480
|
+
var SIGNIN_DIR_VAR = "AGENT_SECRETS_SIGNIN_DIR";
|
|
16481
|
+
var PERSON_VAR = "AGENT_SECRETS_PERSON";
|
|
16482
|
+
var PERSON_TRADE_VAR = "PERSON_SIGNIN_TRADE";
|
|
16483
|
+
var SIGNIN_ISSUER_VAR = "AGENT_SECRETS_SIGNIN_ISSUER";
|
|
16484
|
+
var SIGNIN_CLIENT_ID_VAR = "AGENT_SECRETS_SIGNIN_CLIENT_ID";
|
|
16485
|
+
var SIGNIN_CLIENT_SECRET_FILE_VAR = "AGENT_SECRETS_SIGNIN_CLIENT_SECRET_FILE";
|
|
16486
|
+
var DEFAULT_SIGNIN_ISSUER = "https://accounts.google.com";
|
|
16487
|
+
var SIGNIN_COMMAND = "agent-secrets login";
|
|
16488
|
+
var SIGNINS_DIR = "signins";
|
|
16489
|
+
var RECORD_MEMBERS = [
|
|
16490
|
+
"person",
|
|
16491
|
+
"issuer",
|
|
16492
|
+
"client_id",
|
|
16493
|
+
"refresh_token",
|
|
16494
|
+
"box_key",
|
|
16495
|
+
"thumbprint",
|
|
16496
|
+
"enrollment_id",
|
|
16497
|
+
"signed_in_at"
|
|
16498
|
+
];
|
|
16499
|
+
function signInDirectory(env) {
|
|
16500
|
+
const named = env[SIGNIN_DIR_VAR] ?? "";
|
|
16501
|
+
if (named !== "")
|
|
16502
|
+
return named;
|
|
16503
|
+
const state = env.XDG_STATE_HOME ?? "";
|
|
16504
|
+
const base = state !== "" ? state : join2(env.HOME ?? "", ".local", "state");
|
|
16505
|
+
return join2(base, "agent-secrets", SIGNINS_DIR);
|
|
16506
|
+
}
|
|
16507
|
+
async function readSignIn(path) {
|
|
16508
|
+
let text;
|
|
16509
|
+
try {
|
|
16510
|
+
text = await readFile(path, "utf8");
|
|
16511
|
+
} catch (error) {
|
|
16512
|
+
throw new Error(`read the sign-in at ${path}: ${messageOf(error)}`, { cause: error });
|
|
16513
|
+
}
|
|
16514
|
+
let parsed;
|
|
16515
|
+
try {
|
|
16516
|
+
parsed = JSON.parse(text);
|
|
16517
|
+
} catch {
|
|
16518
|
+
throw new Error(`read the sign-in at ${path}: it is not JSON; run: ${SIGNIN_COMMAND}`);
|
|
16519
|
+
}
|
|
16520
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
16521
|
+
throw new Error(`read the sign-in at ${path}: it holds no JSON object; run: ${SIGNIN_COMMAND}`);
|
|
16522
|
+
}
|
|
16523
|
+
const fields = parsed;
|
|
16524
|
+
const record = {};
|
|
16525
|
+
for (const member of RECORD_MEMBERS) {
|
|
16526
|
+
const given = fields[member] ?? "";
|
|
16527
|
+
if (typeof given !== "string") {
|
|
16528
|
+
throw new Error(`read the sign-in at ${path}: its ${member} is not a string; run: ${SIGNIN_COMMAND}`);
|
|
16529
|
+
}
|
|
16530
|
+
record[member] = given;
|
|
16531
|
+
}
|
|
16532
|
+
const read = record;
|
|
16533
|
+
if (read.person === "" || read.refresh_token === "" || read.box_key === "") {
|
|
16534
|
+
throw new Error(`the sign-in at ${path} names no person, no refresh token or no box key; run: ${SIGNIN_COMMAND}`);
|
|
16535
|
+
}
|
|
16536
|
+
return read;
|
|
16537
|
+
}
|
|
16538
|
+
async function listSignIns(dir) {
|
|
16539
|
+
let entries;
|
|
16540
|
+
try {
|
|
16541
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
16542
|
+
} catch (error) {
|
|
16543
|
+
if (error.code === "ENOENT")
|
|
16544
|
+
return [];
|
|
16545
|
+
throw new Error(`read this machine's sign-ins: ${messageOf(error)}`, { cause: error });
|
|
16546
|
+
}
|
|
16547
|
+
const names = entries.filter((entry) => !entry.isDirectory() && entry.name.endsWith(".json")).map((entry) => entry.name).sort();
|
|
16548
|
+
const records = [];
|
|
16549
|
+
for (const name of names)
|
|
16550
|
+
records.push(await readSignIn(join2(dir, name)));
|
|
16551
|
+
return records.sort((a, b) => a.person < b.person ? -1 : a.person > b.person ? 1 : 0);
|
|
16552
|
+
}
|
|
16553
|
+
function namesOf(records) {
|
|
16554
|
+
return records.map((record) => record.person).join(", ");
|
|
16555
|
+
}
|
|
16556
|
+
function personTradeOff(env) {
|
|
16557
|
+
const written = (env[PERSON_TRADE_VAR] ?? "").trim();
|
|
16558
|
+
if (written === "")
|
|
16559
|
+
return false;
|
|
16560
|
+
if (written === "off")
|
|
16561
|
+
return true;
|
|
16562
|
+
throw new Error(`${PERSON_TRADE_VAR} is "${written}" and takes off or nothing: set it to off where this process's own credentials trade this machine and never a person signed in on it, and leave it unset where they trade the person signed in here`);
|
|
16563
|
+
}
|
|
16564
|
+
async function chooseSignIn(env) {
|
|
16565
|
+
if (personTradeOff(env))
|
|
16566
|
+
return;
|
|
16567
|
+
const records = await listSignIns(signInDirectory(env));
|
|
16568
|
+
const person = (env[PERSON_VAR] ?? "").trim().toLowerCase();
|
|
16569
|
+
if (records.length === 0)
|
|
16570
|
+
return;
|
|
16571
|
+
if (person !== "") {
|
|
16572
|
+
const named = records.find((record) => record.person.toLowerCase() === person);
|
|
16573
|
+
if (named !== undefined)
|
|
16574
|
+
return named;
|
|
16575
|
+
throw new Error(`${PERSON_VAR} names ${person}, who is not signed in on this machine; signed in here: ${namesOf(records)}; run: ${SIGNIN_COMMAND}`);
|
|
16576
|
+
}
|
|
16577
|
+
if (records.length === 1)
|
|
16578
|
+
return records[0];
|
|
16579
|
+
throw new Error(`${records.length} people are signed in on this machine (${namesOf(records)}), so this session must say which one it acts for: set ${PERSON_VAR} to one of them`);
|
|
16580
|
+
}
|
|
16581
|
+
function signInSessionName(record) {
|
|
16582
|
+
const name = `signin-${record.person}`;
|
|
16583
|
+
return record.thumbprint === "" ? name : `${name}-${record.thumbprint.slice(0, 12)}`;
|
|
16584
|
+
}
|
|
16585
|
+
async function signInFlow(env) {
|
|
16586
|
+
const clientId = (env[SIGNIN_CLIENT_ID_VAR] ?? "").trim();
|
|
16587
|
+
const secretFile = (env[SIGNIN_CLIENT_SECRET_FILE_VAR] ?? "").trim();
|
|
16588
|
+
if (clientId === "" && secretFile === "")
|
|
16589
|
+
return;
|
|
16590
|
+
if (clientId === "" || secretFile === "") {
|
|
16591
|
+
throw new Error(`a person's sign-in needs both ${SIGNIN_CLIENT_ID_VAR} and ${SIGNIN_CLIENT_SECRET_FILE_VAR}; this machine has one of them`);
|
|
16592
|
+
}
|
|
16593
|
+
let secret;
|
|
16594
|
+
try {
|
|
16595
|
+
secret = await readFile(secretFile, "utf8");
|
|
16596
|
+
} catch (error) {
|
|
16597
|
+
throw new Error(`read the sign-in client's secret (${SIGNIN_CLIENT_SECRET_FILE_VAR}): ${messageOf(error)}`, { cause: error });
|
|
16598
|
+
}
|
|
16599
|
+
const issuer = (env[SIGNIN_ISSUER_VAR] ?? "").trim();
|
|
16600
|
+
return {
|
|
16601
|
+
issuer: withoutTrailingSlashes(issuer === "" ? DEFAULT_SIGNIN_ISSUER : issuer),
|
|
16602
|
+
clientId,
|
|
16603
|
+
clientSecret: secret.trim()
|
|
16604
|
+
};
|
|
16605
|
+
}
|
|
16606
|
+
|
|
16178
16607
|
// ../broker-client/src/token-client.ts
|
|
16179
16608
|
var DEFAULT_RENEW_BEFORE_MS = 5 * 60000;
|
|
16180
16609
|
var TOO_MANY_REQUESTS = 429;
|
|
@@ -16185,8 +16614,8 @@ class BrokerRefusedError extends Error {
|
|
|
16185
16614
|
status;
|
|
16186
16615
|
code;
|
|
16187
16616
|
description;
|
|
16188
|
-
constructor(status, code, description) {
|
|
16189
|
-
super(description === "" ? `the secrets broker refused
|
|
16617
|
+
constructor(status, code, description, refused) {
|
|
16618
|
+
super(description === "" ? `the secrets broker refused ${refused}: HTTP ${status} ${code}` : `the secrets broker refused ${refused}: HTTP ${status} ${code}: ${description}`);
|
|
16190
16619
|
this.status = status;
|
|
16191
16620
|
this.code = code;
|
|
16192
16621
|
this.description = description;
|
|
@@ -16196,6 +16625,20 @@ class BrokerRefusedError extends Error {
|
|
|
16196
16625
|
return this.status !== TOO_MANY_REQUESTS && this.code !== INVALID_DPOP_PROOF && this.code !== USE_DPOP_NONCE;
|
|
16197
16626
|
}
|
|
16198
16627
|
}
|
|
16628
|
+
|
|
16629
|
+
class SignInEndedError extends Error {
|
|
16630
|
+
issuer;
|
|
16631
|
+
person;
|
|
16632
|
+
constructor(issuer, person, options) {
|
|
16633
|
+
super(`${issuer} no longer honours ${person}'s sign-in on this machine; run: ${SIGNIN_COMMAND}`, options);
|
|
16634
|
+
this.issuer = issuer;
|
|
16635
|
+
this.person = person;
|
|
16636
|
+
this.name = "SignInEndedError";
|
|
16637
|
+
}
|
|
16638
|
+
}
|
|
16639
|
+
function isFinalRefusal(error) {
|
|
16640
|
+
return error instanceof BrokerRefusedError && error.permanent || error instanceof SignInEndedError;
|
|
16641
|
+
}
|
|
16199
16642
|
function statusOf(error) {
|
|
16200
16643
|
let cause = error;
|
|
16201
16644
|
for (let depth = 0;depth < 4; depth += 1) {
|
|
@@ -16207,6 +16650,16 @@ function statusOf(error) {
|
|
|
16207
16650
|
}
|
|
16208
16651
|
return 0;
|
|
16209
16652
|
}
|
|
16653
|
+
function requestInit(options, signal, headers) {
|
|
16654
|
+
const signals = [options.signal, signal].filter((one) => one !== undefined);
|
|
16655
|
+
return {
|
|
16656
|
+
method: options.method,
|
|
16657
|
+
headers,
|
|
16658
|
+
body: options.body,
|
|
16659
|
+
redirect: options.redirect,
|
|
16660
|
+
...signals.length === 0 ? {} : { signal: AbortSignal.any(signals) }
|
|
16661
|
+
};
|
|
16662
|
+
}
|
|
16210
16663
|
|
|
16211
16664
|
class BrokerTokenClient {
|
|
16212
16665
|
#brokerUrl;
|
|
@@ -16255,6 +16708,9 @@ class BrokerTokenClient {
|
|
|
16255
16708
|
this.#trading = null;
|
|
16256
16709
|
}
|
|
16257
16710
|
}
|
|
16711
|
+
drop() {
|
|
16712
|
+
this.#held = null;
|
|
16713
|
+
}
|
|
16258
16714
|
async#renew(signal) {
|
|
16259
16715
|
try {
|
|
16260
16716
|
const token = await this.#trade(signal);
|
|
@@ -16262,7 +16718,7 @@ class BrokerTokenClient {
|
|
|
16262
16718
|
return token;
|
|
16263
16719
|
} catch (error) {
|
|
16264
16720
|
const held = this.#held;
|
|
16265
|
-
if (!(error
|
|
16721
|
+
if (!isFinalRefusal(error) && held !== null && this.#now() < held.expiresAt.getTime()) {
|
|
16266
16722
|
return held;
|
|
16267
16723
|
}
|
|
16268
16724
|
this.#held = null;
|
|
@@ -16285,8 +16741,9 @@ class BrokerTokenClient {
|
|
|
16285
16741
|
try {
|
|
16286
16742
|
actor = await this.#actor.read(signal);
|
|
16287
16743
|
} catch (error) {
|
|
16288
|
-
|
|
16289
|
-
|
|
16744
|
+
throw new Error(`read the machine this person signed in on: ${messageOf(error)}`, {
|
|
16745
|
+
cause: error
|
|
16746
|
+
});
|
|
16290
16747
|
}
|
|
16291
16748
|
form["actor_token"] = actor.token;
|
|
16292
16749
|
form["actor_token_type"] = actor.tokenType === SUBJECT_TOKEN_TYPE_GOOGLE ? ACTOR_TOKEN_TYPE_MACHINE : actor.tokenType;
|
|
@@ -16306,10 +16763,10 @@ class BrokerTokenClient {
|
|
|
16306
16763
|
}
|
|
16307
16764
|
#refusal(endpoint, error) {
|
|
16308
16765
|
if (error instanceof ResponseBodyError && error.status >= 400 && error.status < 500) {
|
|
16309
|
-
return new BrokerRefusedError(error.status, error.error, error.error_description ?? "");
|
|
16766
|
+
return new BrokerRefusedError(error.status, error.error, error.error_description ?? "", this.#subject.describe());
|
|
16310
16767
|
}
|
|
16311
16768
|
const status = statusOf(error);
|
|
16312
|
-
const message =
|
|
16769
|
+
const message = messageOf(error);
|
|
16313
16770
|
const said = status === 0 ? message : `HTTP ${status}: ${message}`;
|
|
16314
16771
|
return new Error(`trade ${this.#subject.describe()} at ${endpoint}: ${said}`, {
|
|
16315
16772
|
cause: error
|
|
@@ -16319,15 +16776,7 @@ class BrokerTokenClient {
|
|
|
16319
16776
|
const send = async () => {
|
|
16320
16777
|
const headers = new Headers(options.headers);
|
|
16321
16778
|
headers.set("DPoP", await this.#key.proof({ method: options.method, url, nonce: this.#nonce }));
|
|
16322
|
-
|
|
16323
|
-
const body = options.body;
|
|
16324
|
-
return await this.#fetch(url, {
|
|
16325
|
-
method: options.method,
|
|
16326
|
-
headers,
|
|
16327
|
-
body,
|
|
16328
|
-
redirect: options.redirect,
|
|
16329
|
-
...signals.length === 0 ? {} : { signal: AbortSignal.any(signals) }
|
|
16330
|
-
});
|
|
16779
|
+
return await this.#fetch(url, requestInit(options, signal, headers));
|
|
16331
16780
|
};
|
|
16332
16781
|
let answer = await send();
|
|
16333
16782
|
const given = answer.headers.get("DPoP-Nonce") ?? "";
|
|
@@ -16375,15 +16824,241 @@ class BrokerTokenClient {
|
|
|
16375
16824
|
}
|
|
16376
16825
|
}
|
|
16377
16826
|
|
|
16827
|
+
// ../broker-client/src/person-identity.ts
|
|
16828
|
+
var ISSUER_TIMEOUT_SECONDS = 30;
|
|
16829
|
+
var INVALID_GRANT = "invalid_grant";
|
|
16830
|
+
function issuerSaid(error) {
|
|
16831
|
+
if (error instanceof ResponseBodyError) {
|
|
16832
|
+
const description = error.error_description ?? "";
|
|
16833
|
+
return `HTTP ${error.status} ${error.error}${description === "" ? "" : `: ${description}`}`;
|
|
16834
|
+
}
|
|
16835
|
+
let message = messageOf(error);
|
|
16836
|
+
const cause = error instanceof Error ? error.cause : undefined;
|
|
16837
|
+
if (cause instanceof Error && cause.message !== message)
|
|
16838
|
+
message += `: ${cause.message}`;
|
|
16839
|
+
const status = statusOf(error);
|
|
16840
|
+
return status === 0 ? message : `HTTP ${status}: ${message}`;
|
|
16841
|
+
}
|
|
16842
|
+
function personIdentity(record, flow, options = {}) {
|
|
16843
|
+
const { person, refresh_token: refreshToken, client_id: clientId } = record;
|
|
16844
|
+
if (clientId !== "" && clientId !== flow.clientId) {
|
|
16845
|
+
throw new Error(`${person} signed in with another client than this machine now has (${clientId}), so their sign-in cannot be renewed; run: ${SIGNIN_COMMAND}`);
|
|
16846
|
+
}
|
|
16847
|
+
const send = options.fetch ?? fetch;
|
|
16848
|
+
const insecure = flow.issuer.startsWith("http:");
|
|
16849
|
+
let discovered = null;
|
|
16850
|
+
const through = (signal) => (url, init) => send(url, requestInit(init, signal, init.headers));
|
|
16851
|
+
const discover = async (signal) => {
|
|
16852
|
+
try {
|
|
16853
|
+
const config = await discovery(new URL(flow.issuer), flow.clientId, undefined, undefined, {
|
|
16854
|
+
[customFetch2]: through(signal),
|
|
16855
|
+
timeout: ISSUER_TIMEOUT_SECONDS,
|
|
16856
|
+
...insecure ? { execute: [allowInsecureRequests2] } : {}
|
|
16857
|
+
});
|
|
16858
|
+
return config.serverMetadata();
|
|
16859
|
+
} catch (error) {
|
|
16860
|
+
throw new Error(`read the sign-in issuer ${flow.issuer}: ${issuerSaid(error)}`, {
|
|
16861
|
+
cause: error
|
|
16862
|
+
});
|
|
16863
|
+
}
|
|
16864
|
+
};
|
|
16865
|
+
return {
|
|
16866
|
+
async read(signal) {
|
|
16867
|
+
discovered ??= discover(signal);
|
|
16868
|
+
const discovering = discovered;
|
|
16869
|
+
let metadata;
|
|
16870
|
+
try {
|
|
16871
|
+
metadata = await discovering;
|
|
16872
|
+
} catch (error) {
|
|
16873
|
+
if (discovered === discovering)
|
|
16874
|
+
discovered = null;
|
|
16875
|
+
throw error;
|
|
16876
|
+
}
|
|
16877
|
+
let answered;
|
|
16878
|
+
try {
|
|
16879
|
+
const config = new Configuration(metadata, flow.clientId, undefined, ClientSecretPost2(flow.clientSecret));
|
|
16880
|
+
if (insecure)
|
|
16881
|
+
allowInsecureRequests2(config);
|
|
16882
|
+
config.timeout = ISSUER_TIMEOUT_SECONDS;
|
|
16883
|
+
config[customFetch2] = through(signal);
|
|
16884
|
+
answered = await refreshTokenGrant(config, refreshToken);
|
|
16885
|
+
} catch (error) {
|
|
16886
|
+
if (error instanceof ResponseBodyError && error.error === INVALID_GRANT) {
|
|
16887
|
+
throw new SignInEndedError(flow.issuer, person, { cause: error });
|
|
16888
|
+
}
|
|
16889
|
+
throw new Error(`renew ${person}'s sign-in at ${flow.issuer}: ${issuerSaid(error)}`);
|
|
16890
|
+
}
|
|
16891
|
+
const idToken = answered.id_token ?? "";
|
|
16892
|
+
if (idToken === "") {
|
|
16893
|
+
throw new Error(`${flow.issuer} answered no id_token: the sign-in asked for the "openid" scope and must be answered one`);
|
|
16894
|
+
}
|
|
16895
|
+
return { token: idToken, tokenType: SUBJECT_TOKEN_TYPE_PERSON };
|
|
16896
|
+
},
|
|
16897
|
+
describe: () => `${person}'s sign-in on this machine`
|
|
16898
|
+
};
|
|
16899
|
+
}
|
|
16900
|
+
|
|
16901
|
+
// ../broker-client/src/pool-identity.ts
|
|
16902
|
+
var DEFAULT_SECURITY_TOKEN_SERVICE_URL = "https://sts.googleapis.com/v1/token";
|
|
16903
|
+
var DEFAULT_IAM_CREDENTIALS_URL = "https://iamcredentials.googleapis.com";
|
|
16904
|
+
var REQUESTED_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token";
|
|
16905
|
+
var SCOPE = "https://www.googleapis.com/auth/cloud-platform";
|
|
16906
|
+
var SUBJECT_TOKEN_TYPE_JWT = "urn:ietf:params:oauth:token-type:jwt";
|
|
16907
|
+
var POOL_TIMEOUT_MS = 20000;
|
|
16908
|
+
var MAX_ANSWER_BYTES2 = 1 << 20;
|
|
16909
|
+
function poolSubjectTokenType(brokerType) {
|
|
16910
|
+
return brokerType === SUBJECT_TOKEN_TYPE_KUBERNETES ? SUBJECT_TOKEN_TYPE_JWT : brokerType;
|
|
16911
|
+
}
|
|
16912
|
+
function pathEscape(segment) {
|
|
16913
|
+
return encodeURIComponent(segment).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`).replace(/%(?:24|26|2B|3A|3D|40)/g, decodeURIComponent);
|
|
16914
|
+
}
|
|
16915
|
+
async function answerText(answer) {
|
|
16916
|
+
const reader = answer.body?.getReader();
|
|
16917
|
+
if (reader === undefined)
|
|
16918
|
+
return "";
|
|
16919
|
+
const chunks = [];
|
|
16920
|
+
let read = 0;
|
|
16921
|
+
while (read < MAX_ANSWER_BYTES2) {
|
|
16922
|
+
const { done, value } = await reader.read();
|
|
16923
|
+
if (done)
|
|
16924
|
+
break;
|
|
16925
|
+
const kept = value.subarray(0, MAX_ANSWER_BYTES2 - read);
|
|
16926
|
+
chunks.push(kept);
|
|
16927
|
+
read += kept.length;
|
|
16928
|
+
}
|
|
16929
|
+
await reader.cancel().catch(() => {
|
|
16930
|
+
return;
|
|
16931
|
+
});
|
|
16932
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
16933
|
+
}
|
|
16934
|
+
function poolIdentity(options) {
|
|
16935
|
+
const securityTokenService = options.securityTokenServiceUrl ?? DEFAULT_SECURITY_TOKEN_SERVICE_URL;
|
|
16936
|
+
const iamCredentials = (options.iamCredentialsUrl ?? DEFAULT_IAM_CREDENTIALS_URL).replace(/\/$/, "");
|
|
16937
|
+
const account = pathEscape(options.serviceAccount);
|
|
16938
|
+
const mintEndpoint = `${iamCredentials}/v1/projects/-/serviceAccounts/${account}:generateIdToken`;
|
|
16939
|
+
const call = async (endpoint, bearer, body, member, signal) => {
|
|
16940
|
+
const signals = [AbortSignal.timeout(POOL_TIMEOUT_MS), signal].filter((one) => one !== undefined);
|
|
16941
|
+
const answer = await fetch(endpoint, {
|
|
16942
|
+
method: "POST",
|
|
16943
|
+
headers: {
|
|
16944
|
+
"Content-Type": "application/json",
|
|
16945
|
+
...bearer === "" ? {} : { Authorization: `Bearer ${bearer}` }
|
|
16946
|
+
},
|
|
16947
|
+
body: JSON.stringify(body),
|
|
16948
|
+
signal: AbortSignal.any(signals)
|
|
16949
|
+
});
|
|
16950
|
+
const text = await answerText(answer);
|
|
16951
|
+
if (answer.status < 200 || answer.status > 299) {
|
|
16952
|
+
throw new Error(`HTTP ${answer.status}: ${text.trim()}`);
|
|
16953
|
+
}
|
|
16954
|
+
let parsed;
|
|
16955
|
+
try {
|
|
16956
|
+
parsed = JSON.parse(text);
|
|
16957
|
+
} catch {
|
|
16958
|
+
throw new Error(`HTTP ${answer.status}: the answer is not JSON`);
|
|
16959
|
+
}
|
|
16960
|
+
const value = typeof parsed === "object" && parsed !== null ? parsed[member] : undefined;
|
|
16961
|
+
return typeof value === "string" ? value : "";
|
|
16962
|
+
};
|
|
16963
|
+
const federate = async (subject, signal) => {
|
|
16964
|
+
let federated;
|
|
16965
|
+
try {
|
|
16966
|
+
federated = await call(securityTokenService, "", {
|
|
16967
|
+
audience: options.audience,
|
|
16968
|
+
grantType: GRANT_TYPE_TOKEN_EXCHANGE,
|
|
16969
|
+
requestedTokenType: REQUESTED_TOKEN_TYPE,
|
|
16970
|
+
scope: SCOPE,
|
|
16971
|
+
subjectTokenType: poolSubjectTokenType(subject.tokenType),
|
|
16972
|
+
subjectToken: subject.token
|
|
16973
|
+
}, "access_token", signal);
|
|
16974
|
+
} catch (error) {
|
|
16975
|
+
throw new Error(`trade this machine's credential at the security token service ${securityTokenService}: ${messageOf(error)}`, { cause: error });
|
|
16976
|
+
}
|
|
16977
|
+
if (federated === "") {
|
|
16978
|
+
throw new Error(`the security token service ${securityTokenService} answered no access_token`);
|
|
16979
|
+
}
|
|
16980
|
+
return federated;
|
|
16981
|
+
};
|
|
16982
|
+
const generateIdToken = async (federated, signal) => {
|
|
16983
|
+
let token;
|
|
16984
|
+
try {
|
|
16985
|
+
token = await call(mintEndpoint, federated, { audience: options.tokenAudience, includeEmail: true }, "token", signal);
|
|
16986
|
+
} catch (error) {
|
|
16987
|
+
throw new Error(`mint this machine's identity token at ${mintEndpoint}: ${messageOf(error)}`, { cause: error });
|
|
16988
|
+
}
|
|
16989
|
+
if (token === "")
|
|
16990
|
+
throw new Error(`generateIdToken at ${mintEndpoint} answered no token`);
|
|
16991
|
+
return token;
|
|
16992
|
+
};
|
|
16993
|
+
return {
|
|
16994
|
+
async read(signal) {
|
|
16995
|
+
let subject;
|
|
16996
|
+
try {
|
|
16997
|
+
subject = await options.subject.read(signal);
|
|
16998
|
+
} catch (error) {
|
|
16999
|
+
throw new Error(`read the credential the workload identity pool federates: ${messageOf(error)}`, { cause: error });
|
|
17000
|
+
}
|
|
17001
|
+
const federated = await federate(subject, signal);
|
|
17002
|
+
return {
|
|
17003
|
+
token: await generateIdToken(federated, signal),
|
|
17004
|
+
tokenType: SUBJECT_TOKEN_TYPE_GOOGLE
|
|
17005
|
+
};
|
|
17006
|
+
},
|
|
17007
|
+
describe: () => `a Google identity token for ${options.tokenAudience} minted through the workload identity pool ${options.audience} as ${options.serviceAccount}`
|
|
17008
|
+
};
|
|
17009
|
+
}
|
|
17010
|
+
|
|
17011
|
+
// ../broker-client/src/projected-token.ts
|
|
17012
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
17013
|
+
function projectedToken(path) {
|
|
17014
|
+
return {
|
|
17015
|
+
async read(signal) {
|
|
17016
|
+
let contents;
|
|
17017
|
+
try {
|
|
17018
|
+
contents = await readFile2(path, {
|
|
17019
|
+
encoding: "utf8",
|
|
17020
|
+
...signal === undefined ? {} : { signal }
|
|
17021
|
+
});
|
|
17022
|
+
} catch (error) {
|
|
17023
|
+
throw new Error(`read the projected service-account token ${path}: ${error.message}`);
|
|
17024
|
+
}
|
|
17025
|
+
const token = contents.trim();
|
|
17026
|
+
if (token === "")
|
|
17027
|
+
throw new Error(`the projected service-account token ${path} is empty`);
|
|
17028
|
+
return { token, tokenType: SUBJECT_TOKEN_TYPE_KUBERNETES };
|
|
17029
|
+
},
|
|
17030
|
+
describe: () => `the projected service-account token ${path}`
|
|
17031
|
+
};
|
|
17032
|
+
}
|
|
17033
|
+
|
|
16378
17034
|
// ../broker-client/src/environment.ts
|
|
16379
17035
|
var BROKER_URL_VAR = "AGENT_SECRETS_URL";
|
|
16380
17036
|
var IDENTITY_VAR = "AGENT_SECRETS_MACHINE_IDENTITY";
|
|
16381
17037
|
var MACHINE_TOKEN_FILE_VAR = "AGENT_SECRETS_MACHINE_TOKEN_FILE";
|
|
16382
17038
|
var MACHINE_AUDIENCE_VAR = "AGENT_SECRETS_MACHINE_AUDIENCE";
|
|
16383
17039
|
var SESSION_VAR = "AGENT_SECRETS_SESSION";
|
|
17040
|
+
var POOL_AUDIENCE_VAR = "AGENT_SECRETS_POOL_AUDIENCE";
|
|
17041
|
+
var POOL_ACCOUNT_VAR = "AGENT_SECRETS_POOL_ACCOUNT";
|
|
16384
17042
|
var IDENTITY_KUBERNETES = "kubernetes";
|
|
16385
17043
|
var IDENTITY_METADATA = "metadata";
|
|
16386
17044
|
var IDENTITY_POOL = "pool";
|
|
17045
|
+
var BROKER_VARIABLES = [
|
|
17046
|
+
BROKER_URL_VAR,
|
|
17047
|
+
IDENTITY_VAR,
|
|
17048
|
+
MACHINE_TOKEN_FILE_VAR,
|
|
17049
|
+
MACHINE_AUDIENCE_VAR,
|
|
17050
|
+
SESSION_VAR,
|
|
17051
|
+
POOL_AUDIENCE_VAR,
|
|
17052
|
+
POOL_ACCOUNT_VAR,
|
|
17053
|
+
SIGNIN_DIR_VAR,
|
|
17054
|
+
"XDG_STATE_HOME",
|
|
17055
|
+
"HOME",
|
|
17056
|
+
PERSON_VAR,
|
|
17057
|
+
PERSON_TRADE_VAR,
|
|
17058
|
+
SIGNIN_ISSUER_VAR,
|
|
17059
|
+
SIGNIN_CLIENT_ID_VAR,
|
|
17060
|
+
SIGNIN_CLIENT_SECRET_FILE_VAR
|
|
17061
|
+
];
|
|
16387
17062
|
function value(env, name) {
|
|
16388
17063
|
return (env[name] ?? "").trim();
|
|
16389
17064
|
}
|
|
@@ -16402,10 +17077,20 @@ function machineIdentity(env) {
|
|
|
16402
17077
|
return projectedToken(required2(env, MACHINE_TOKEN_FILE_VAR, kind));
|
|
16403
17078
|
case IDENTITY_METADATA:
|
|
16404
17079
|
return metadataIdentity(required2(env, MACHINE_AUDIENCE_VAR, kind));
|
|
16405
|
-
case IDENTITY_POOL:
|
|
16406
|
-
|
|
17080
|
+
case IDENTITY_POOL: {
|
|
17081
|
+
const subject = required2(env, MACHINE_TOKEN_FILE_VAR, kind);
|
|
17082
|
+
const tokenAudience = required2(env, MACHINE_AUDIENCE_VAR, kind);
|
|
17083
|
+
const audience = required2(env, POOL_AUDIENCE_VAR, kind);
|
|
17084
|
+
const serviceAccount = required2(env, POOL_ACCOUNT_VAR, kind);
|
|
17085
|
+
return poolIdentity({
|
|
17086
|
+
subject: projectedToken(subject),
|
|
17087
|
+
audience,
|
|
17088
|
+
serviceAccount,
|
|
17089
|
+
tokenAudience
|
|
17090
|
+
});
|
|
17091
|
+
}
|
|
16407
17092
|
default:
|
|
16408
|
-
throw new Error(`${IDENTITY_VAR}="${kind}" names no identity this machine can prove; it takes ${IDENTITY_KUBERNETES} or ${
|
|
17093
|
+
throw new Error(`${IDENTITY_VAR}="${kind}" names no identity this machine can prove; it takes ${IDENTITY_KUBERNETES}, ${IDENTITY_METADATA} or ${IDENTITY_POOL}`);
|
|
16409
17094
|
}
|
|
16410
17095
|
}
|
|
16411
17096
|
function tradeSession(env, own = "") {
|
|
@@ -16414,16 +17099,32 @@ function tradeSession(env, own = "") {
|
|
|
16414
17099
|
}
|
|
16415
17100
|
async function brokerTokenClient(env, own = "") {
|
|
16416
17101
|
const brokerUrl = value(env, BROKER_URL_VAR);
|
|
16417
|
-
const
|
|
16418
|
-
if (brokerUrl === "" ||
|
|
17102
|
+
const machine = machineIdentity(env);
|
|
17103
|
+
if (brokerUrl === "" || machine === undefined)
|
|
16419
17104
|
return;
|
|
16420
|
-
|
|
17105
|
+
const record = await chooseSignIn(env);
|
|
17106
|
+
if (record === undefined) {
|
|
17107
|
+
const client = new BrokerTokenClient({
|
|
17108
|
+
brokerUrl,
|
|
17109
|
+
key: await newDpopKey(),
|
|
17110
|
+
subject: machine,
|
|
17111
|
+
session: tradeSession(env, own),
|
|
17112
|
+
launcherCredential: false
|
|
17113
|
+
});
|
|
17114
|
+
return { client, signIn: undefined };
|
|
17115
|
+
}
|
|
17116
|
+
const flow = await signInFlow(env);
|
|
17117
|
+
if (flow === undefined) {
|
|
17118
|
+
throw new Error(`${record.person} is signed in on this machine, and this machine configures no sign-in client to renew that sign-in with: set ${SIGNIN_CLIENT_ID_VAR} and ${SIGNIN_CLIENT_SECRET_FILE_VAR}`);
|
|
17119
|
+
}
|
|
17120
|
+
const client = new BrokerTokenClient({
|
|
16421
17121
|
brokerUrl,
|
|
16422
|
-
key: await
|
|
16423
|
-
subject,
|
|
16424
|
-
|
|
16425
|
-
|
|
17122
|
+
key: await importDpopKey(record.box_key, `${record.person}'s box key`),
|
|
17123
|
+
subject: personIdentity(record, flow),
|
|
17124
|
+
actor: machine,
|
|
17125
|
+
session: own.trim() || signInSessionName(record)
|
|
16426
17126
|
});
|
|
17127
|
+
return { client, signIn: { person: record.person, thumbprint: record.thumbprint } };
|
|
16427
17128
|
}
|
|
16428
17129
|
|
|
16429
17130
|
// ../envoy-client/src/broker-credential.ts
|
|
@@ -16436,49 +17137,97 @@ function brokerRouteConfigured(env) {
|
|
|
16436
17137
|
if ((env[BROKER_URL_VAR] ?? "").trim() === "") {
|
|
16437
17138
|
return { traded: false, gap: `${BROKER_URL_VAR} names no secrets broker to trade it at` };
|
|
16438
17139
|
}
|
|
16439
|
-
if ((env[IDENTITY_VAR] ?? "").trim() === IDENTITY_POOL) {
|
|
16440
|
-
return {
|
|
16441
|
-
traded: false,
|
|
16442
|
-
gap: `${IDENTITY_VAR}=${IDENTITY_POOL} is traded by the Go client alone (agent-secrets), never by this process`
|
|
16443
|
-
};
|
|
16444
|
-
}
|
|
16445
17140
|
if (machineIdentity(env) === undefined) {
|
|
16446
17141
|
return { traded: false, gap: `${IDENTITY_VAR} names no identity this machine proves` };
|
|
16447
17142
|
}
|
|
16448
17143
|
return { traded: true };
|
|
16449
17144
|
}
|
|
16450
17145
|
var CLIENTS = new Map;
|
|
16451
|
-
function
|
|
16452
|
-
|
|
16453
|
-
|
|
16454
|
-
|
|
16455
|
-
|
|
16456
|
-
|
|
16457
|
-
env
|
|
16458
|
-
|
|
16459
|
-
|
|
16460
|
-
|
|
16461
|
-
|
|
16462
|
-
|
|
16463
|
-
|
|
17146
|
+
function signInName(record) {
|
|
17147
|
+
return `${record.person}\x00${record.thumbprint}`;
|
|
17148
|
+
}
|
|
17149
|
+
function keyOf(env, own) {
|
|
17150
|
+
return {
|
|
17151
|
+
session: own.trim(),
|
|
17152
|
+
variables: JSON.stringify(BROKER_VARIABLES.map((name) => env[name]))
|
|
17153
|
+
};
|
|
17154
|
+
}
|
|
17155
|
+
function entryFor(key) {
|
|
17156
|
+
let sessions = CLIENTS.get(key.session);
|
|
17157
|
+
if (sessions === undefined) {
|
|
17158
|
+
sessions = new Map;
|
|
17159
|
+
CLIENTS.set(key.session, sessions);
|
|
17160
|
+
}
|
|
17161
|
+
let entry = sessions.get(key.variables);
|
|
17162
|
+
if (entry === undefined) {
|
|
17163
|
+
entry = { making: undefined, made: undefined, refused: new Map };
|
|
17164
|
+
sessions.set(key.variables, entry);
|
|
17165
|
+
}
|
|
17166
|
+
return entry;
|
|
17167
|
+
}
|
|
17168
|
+
async function make(env, own) {
|
|
17169
|
+
const made = await brokerTokenClient(env, own);
|
|
17170
|
+
const signIn = made?.signIn;
|
|
17171
|
+
return { client: made?.client, signIn: signIn === undefined ? "" : signInName(signIn) };
|
|
17172
|
+
}
|
|
17173
|
+
async function clientFor(env, own) {
|
|
17174
|
+
const key = keyOf(env, own);
|
|
17175
|
+
const entry = entryFor(key);
|
|
17176
|
+
if (entry.refused.size > 0) {
|
|
17177
|
+
const record = await chooseSignIn(env);
|
|
17178
|
+
const chosen = record === undefined ? "" : signInName(record);
|
|
17179
|
+
if (entry.refused.has(chosen))
|
|
17180
|
+
throw entry.refused.get(chosen);
|
|
17181
|
+
if (entry.made !== undefined && entry.made.signIn !== chosen) {
|
|
17182
|
+
entry.making = undefined;
|
|
17183
|
+
entry.made = undefined;
|
|
17184
|
+
}
|
|
17185
|
+
}
|
|
17186
|
+
let making = entry.making;
|
|
17187
|
+
if (making === undefined) {
|
|
17188
|
+
const started = make(env, key.session);
|
|
17189
|
+
making = started;
|
|
17190
|
+
entry.making = started;
|
|
17191
|
+
started.then((made) => {
|
|
17192
|
+
if (entry.making === started)
|
|
17193
|
+
entry.made = made;
|
|
17194
|
+
}, () => {
|
|
17195
|
+
if (entry.making === started)
|
|
17196
|
+
entry.making = undefined;
|
|
17197
|
+
});
|
|
17198
|
+
}
|
|
17199
|
+
return { entry, made: await making };
|
|
16464
17200
|
}
|
|
16465
17201
|
function brokerCredential(env, session) {
|
|
16466
|
-
let
|
|
17202
|
+
let presented = "";
|
|
17203
|
+
const own = (acting) => (acting ?? "").trim() || sessionName(session);
|
|
16467
17204
|
return {
|
|
16468
17205
|
scheme: "DPoP",
|
|
16469
17206
|
async headers(method, url, acting) {
|
|
16470
|
-
const
|
|
17207
|
+
const { entry, made } = await clientFor(env, own(acting));
|
|
17208
|
+
const { client } = made;
|
|
16471
17209
|
if (client === undefined) {
|
|
16472
17210
|
throw new Error("AGENT_SECRETS_URL and AGENT_SECRETS_MACHINE_IDENTITY no longer name a broker and an identity to trade");
|
|
16473
17211
|
}
|
|
16474
|
-
|
|
16475
|
-
|
|
17212
|
+
let token;
|
|
17213
|
+
try {
|
|
17214
|
+
token = await client.token();
|
|
17215
|
+
} catch (error) {
|
|
17216
|
+
if (made.signIn !== "" && isFinalRefusal(error))
|
|
17217
|
+
entry.refused.set(made.signIn, error);
|
|
17218
|
+
throw error;
|
|
17219
|
+
}
|
|
17220
|
+
presented = token.accessToken;
|
|
16476
17221
|
return {
|
|
16477
17222
|
Authorization: `DPoP ${token.accessToken}`,
|
|
16478
17223
|
DPoP: await client.key.proof({ method, url, accessToken: token.accessToken })
|
|
16479
17224
|
};
|
|
16480
17225
|
},
|
|
16481
|
-
|
|
17226
|
+
stale(acting) {
|
|
17227
|
+
const key = keyOf(env, own(acting));
|
|
17228
|
+
CLIENTS.get(key.session)?.get(key.variables)?.made?.client?.drop();
|
|
17229
|
+
},
|
|
17230
|
+
secret: () => presented
|
|
16482
17231
|
};
|
|
16483
17232
|
}
|
|
16484
17233
|
|
|
@@ -16554,7 +17303,7 @@ function brokerRouteForced(env) {
|
|
|
16554
17303
|
return false;
|
|
16555
17304
|
if (written === "1")
|
|
16556
17305
|
return true;
|
|
16557
|
-
return offWithError(`DISPATCH_TOKEN_BROKER is "${written}" and takes 1: set it to 1 where this session's Dispatch credential is
|
|
17306
|
+
return offWithError(`DISPATCH_TOKEN_BROKER is "${written}" and takes 1: set it to 1 where this session's Dispatch credential is traded at the secrets broker (the person signed in on this machine, else the machine's own identity), and leave it unset where a bearer is read as usual`);
|
|
16558
17307
|
}
|
|
16559
17308
|
function dispatchTarget(env, config) {
|
|
16560
17309
|
if (config.kind === "failed")
|
|
@@ -16605,7 +17354,7 @@ function dispatchTarget(env, config) {
|
|
|
16605
17354
|
return { enabled: true, url: brokered, source: { kind: "broker" } };
|
|
16606
17355
|
}
|
|
16607
17356
|
if (forced) {
|
|
16608
|
-
return offWithError(`DISPATCH_TOKEN_BROKER is set, so this session's Dispatch credential is
|
|
17357
|
+
return offWithError(`DISPATCH_TOKEN_BROKER is set, so this session's Dispatch credential is traded at the secrets broker (the person signed in on this machine, else the machine's own identity), and ${route.gap}: Dispatch is off rather than written with whatever bearer this box holds`);
|
|
16609
17358
|
}
|
|
16610
17359
|
return offWithError(explicitUrl === undefined ? `${config.path}: dispatch is enabled and no token is: set dispatch.token, or DISPATCH_TOKEN or DISPATCH_TOKEN_FILE` : `DISPATCH_URL is set and no token is: set DISPATCH_TOKEN or DISPATCH_TOKEN_FILE, or dispatch.token in ${config.path}`);
|
|
16611
17360
|
}
|
|
@@ -17239,6 +17988,14 @@ var handoffValidation = {
|
|
|
17239
17988
|
message: HANDOFF_RULE,
|
|
17240
17989
|
refusal: (value) => handoffProblem(value) ?? HANDOFF_RULE
|
|
17241
17990
|
};
|
|
17991
|
+
var REVIEW_REASON_RULE = "verdict changes_requested requires reason, a non-empty note saying what must change before the document can be approved.";
|
|
17992
|
+
var reviewValidation = {
|
|
17993
|
+
check: (value) => {
|
|
17994
|
+
const input = value;
|
|
17995
|
+
return input.verdict !== "changes_requested" || typeof input.reason === "string" && input.reason.trim() !== "";
|
|
17996
|
+
},
|
|
17997
|
+
message: REVIEW_REASON_RULE
|
|
17998
|
+
};
|
|
17242
17999
|
var RESULT_REQUIRED_FIELDS = ["issue", "check", "role", "verdict", "commit"];
|
|
17243
18000
|
var RESULT_RECORD_FIELDS = [
|
|
17244
18001
|
...RESULT_REQUIRED_FIELDS,
|
|
@@ -17622,6 +18379,20 @@ var dispatchToolSpecs = [
|
|
|
17622
18379
|
version: z.number({ int: true, min: 1 }).describe("The plan version the review read and approved.")
|
|
17623
18380
|
})
|
|
17624
18381
|
},
|
|
18382
|
+
{
|
|
18383
|
+
name: "dispatch_review",
|
|
18384
|
+
example: { issue: "DSP-1", version: 2, verdict: "approved" },
|
|
18385
|
+
description: "Approve one version of an issue's document, or request changes on it, as the person your " + "token names: what the document header's Approve and Request changes record. A review " + "records an approval or a changes request in the person's name at that version, so it " + "works only with a token that names a person who holds approve; Dispatch refuses it to " + "any other token, with 403 OAUTH_SCOPE_REQUIRED or 403 SIGN_IN_REQUIRED. --version is the " + "version you read, sent in the review route's path, and it must be the version the review " + "would record: the document's latest settled version, or, while an approval request is " + "open on it, the version that request names for it. Any other is refused 409 " + "REVIEW_VERSION_MOVED, which names the version to read, and nothing is recorded. A " + "Dispatch build without the versioned review route answers no route and records nothing; " + "the command then says so and names /healthz, which names the commit of the build that " + "answers it when that build was stamped with one. With a request open the review answers " + "it, and records the same verdict on every document the " + "request names, each at the version the request names. A later version makes an approval " + "stale. A review is the one browser action this command takes for a person; every other " + "browser action is dispatch://INFRA-910's. " + ISSUE_REFERENCE,
|
|
18386
|
+
arguments: (z) => ({
|
|
18387
|
+
issue: z.string().describe(ISSUE_REFERENCE),
|
|
18388
|
+
artifact: z.string().describe("Document artifact id, slug, or filename; the issue's spec by default.").optional(),
|
|
18389
|
+
version: z.number({ int: true, min: 1 }).describe("The document version you read and review, sent as a segment of the review route's path: the version the review would record, or Dispatch refuses it 409 REVIEW_VERSION_MOVED naming the version to read."),
|
|
18390
|
+
verdict: z.enum(["approved", "changes_requested"]).describe("approved records an approval; changes_requested records a request for changes and requires reason."),
|
|
18391
|
+
reason: z.string().describe("What must change before the document can be approved. changes_requested requires it; an approval may carry one.").optional()
|
|
18392
|
+
}),
|
|
18393
|
+
validation: reviewValidation,
|
|
18394
|
+
strict: true
|
|
18395
|
+
},
|
|
17625
18396
|
{
|
|
17626
18397
|
name: "dispatch_result",
|
|
17627
18398
|
example: {
|
|
@@ -20294,6 +21065,9 @@ class DispatchNoRouteError extends DispatchServiceError {
|
|
|
20294
21065
|
super("NOT_FOUND", 404, NO_ROUTE_ROLLING_RELEASE_MESSAGE);
|
|
20295
21066
|
}
|
|
20296
21067
|
}
|
|
21068
|
+
function reviewNoRouteRefusal(baseUrl, route, version) {
|
|
21069
|
+
return new DispatchServiceError("NOT_FOUND", 404, `Dispatch at ${baseUrl} answered no route for ${route} each time the review was sent, and an ` + "answer of no route records no review. This build is older than that route. No other review " + "route was asked: the review route an older build serves takes no version, and it " + `would record the review at a version that build chooses, which may not be version ${version}. ` + `The no-route answer does not name the build; GET ${baseUrl}/healthz names the commit of the ` + "build that answers it, when that build was stamped with one.");
|
|
21070
|
+
}
|
|
20297
21071
|
var ISSUE_EVENTS_PAGE_MAX = 200;
|
|
20298
21072
|
var DISPATCH_TOOL_DEADLINE_MS = 60000;
|
|
20299
21073
|
function requestSignal(signal) {
|
|
@@ -20398,6 +21172,15 @@ async function writeAnswer(response, key) {
|
|
|
20398
21172
|
unjudged: !echoed && typeof shape.error === "string"
|
|
20399
21173
|
};
|
|
20400
21174
|
}
|
|
21175
|
+
var BROKER_TOKEN_NOT_ACTIVE = "BROKER_TOKEN_NOT_ACTIVE";
|
|
21176
|
+
async function tokenNotLive(response) {
|
|
21177
|
+
if (response.status !== 401 || !isJson(response))
|
|
21178
|
+
return false;
|
|
21179
|
+
const body = await response.clone().json().catch(() => {
|
|
21180
|
+
return;
|
|
21181
|
+
});
|
|
21182
|
+
return asErrorShape(body).code === BROKER_TOKEN_NOT_ACTIVE;
|
|
21183
|
+
}
|
|
20401
21184
|
function whyNotAPage(answer) {
|
|
20402
21185
|
if (typeof answer === "string") {
|
|
20403
21186
|
return `text that is not a JSON page, ${GATEWAY_PAGE}, so a retry may succeed.`;
|
|
@@ -20720,6 +21503,18 @@ class DispatchClient {
|
|
|
20720
21503
|
async planApprove(artifactID, input) {
|
|
20721
21504
|
return this.#json("POST", ["api", "v1", "artifacts", artifactID, "plan-reviews"], input);
|
|
20722
21505
|
}
|
|
21506
|
+
async reviewArtifact(artifactID, version, input) {
|
|
21507
|
+
const path = ["api", "v1", "artifacts", artifactID, "versions", String(version), "reviews"];
|
|
21508
|
+
const body = { state: input.state, reason: input.reason };
|
|
21509
|
+
try {
|
|
21510
|
+
return await this.#json("POST", path, body);
|
|
21511
|
+
} catch (error) {
|
|
21512
|
+
if (!(error instanceof DispatchNoRouteError) && !isNoRoute(error))
|
|
21513
|
+
throw error;
|
|
21514
|
+
const route = `POST /${path.map((segment) => encodeURIComponent(segment)).join("/")}`;
|
|
21515
|
+
throw reviewNoRouteRefusal(this.#baseUrl, route, version);
|
|
21516
|
+
}
|
|
21517
|
+
}
|
|
20723
21518
|
async recordResult(issue, input) {
|
|
20724
21519
|
return this.#json("POST", ["api", "v1", "issues", await this.#resolveIssue(issue), "results"], input);
|
|
20725
21520
|
}
|
|
@@ -20955,11 +21750,20 @@ class DispatchClient {
|
|
|
20955
21750
|
});
|
|
20956
21751
|
}
|
|
20957
21752
|
async#send(method, url, body, accept = "application/json", signal, callerKey) {
|
|
21753
|
+
const key = method === "POST" || method === "PATCH" ? callerKey ?? crypto.randomUUID() : undefined;
|
|
21754
|
+
const send = async () => this.#sendPresenting(method, url, body, accept, signal, key);
|
|
21755
|
+
const answer = await send();
|
|
21756
|
+
if (this.#credential.stale === undefined || !await tokenNotLive(answer))
|
|
21757
|
+
return answer;
|
|
21758
|
+
this.#credential.stale();
|
|
21759
|
+
return await send();
|
|
21760
|
+
}
|
|
21761
|
+
async#sendPresenting(method, url, body, accept, signal, key) {
|
|
20958
21762
|
const headers = { Accept: accept };
|
|
20959
21763
|
let presented = await this.#credential.headers(method, url);
|
|
20960
21764
|
if (body !== undefined)
|
|
20961
21765
|
headers["Content-Type"] = body.type;
|
|
20962
|
-
if (
|
|
21766
|
+
if (key === undefined) {
|
|
20963
21767
|
return this.fetchImpl(url, {
|
|
20964
21768
|
method,
|
|
20965
21769
|
headers: { ...headers, ...presented },
|
|
@@ -20967,7 +21771,6 @@ class DispatchClient {
|
|
|
20967
21771
|
...body === undefined ? {} : { body: body.content }
|
|
20968
21772
|
});
|
|
20969
21773
|
}
|
|
20970
|
-
const key = callerKey ?? crypto.randomUUID();
|
|
20971
21774
|
headers[IDEMPOTENCY_KEY_HEADER] = key;
|
|
20972
21775
|
const { pausesMs, withinMs, silenceMs, inFlightPauseMs, inFlightWithinMs } = this.writeRetry;
|
|
20973
21776
|
const pause = this.writeRetry.pause ?? pauseFor;
|
|
@@ -24906,24 +25709,59 @@ async function readCommand(context) {
|
|
|
24906
25709
|
};
|
|
24907
25710
|
}
|
|
24908
25711
|
|
|
25712
|
+
// ../envoy-client/src/dispatch-commands/reviews.ts
|
|
25713
|
+
async function reviewCommand(context) {
|
|
25714
|
+
const { args, client, documentOwner, resolveDocument } = context;
|
|
25715
|
+
const resolved = await resolveDocument(documentOwner(), optionalString(args, "artifact"));
|
|
25716
|
+
const { artifact } = resolved;
|
|
25717
|
+
let review;
|
|
25718
|
+
try {
|
|
25719
|
+
review = await client.reviewArtifact(artifact.id, numberArg(args, "version"), {
|
|
25720
|
+
state: stringArg(args, "verdict"),
|
|
25721
|
+
...defined("reason", optionalString(args, "reason"))
|
|
25722
|
+
});
|
|
25723
|
+
} catch (error) {
|
|
25724
|
+
throw refusalWithCode(error);
|
|
25725
|
+
}
|
|
25726
|
+
const issue = resolved.issue?.key;
|
|
25727
|
+
const answered = review.ask_id === null ? [] : [
|
|
25728
|
+
`It also answered the open approval request (ask ${review.ask_id}), so Dispatch recorded the same verdict on every document that request named, each at the version the request named for it. Dispatch answered with the review of ${artifact.name} alone, so no other document's review is shown here; ${commandLine("dispatch_doc_read", { issue })} --artifact <document> prints the approval each of them now reads.`
|
|
25729
|
+
];
|
|
25730
|
+
return {
|
|
25731
|
+
text: [
|
|
25732
|
+
`Recorded the review of ${artifact.name} (document id ${artifact.id}): ${review.state} at version ${review.version}, by ${actorText(review.actor)}.`,
|
|
25733
|
+
...review.reason === null ? [] : [`Reason: ${JSON.stringify(review.reason)}`],
|
|
25734
|
+
...answered
|
|
25735
|
+
].join(`
|
|
25736
|
+
`),
|
|
25737
|
+
details: {
|
|
25738
|
+
issue,
|
|
25739
|
+
artifact: artifact.id,
|
|
25740
|
+
version: review.version,
|
|
25741
|
+
state: review.state,
|
|
25742
|
+
...review.ask_id === null ? {} : { ask: review.ask_id }
|
|
25743
|
+
}
|
|
25744
|
+
};
|
|
25745
|
+
}
|
|
25746
|
+
|
|
24909
25747
|
// ../envoy-client/src/dispatch-session-directory.ts
|
|
24910
25748
|
import { randomUUID } from "crypto";
|
|
24911
25749
|
import { renameSync, rmSync as rmSync2, writeFileSync } from "fs";
|
|
24912
25750
|
import { homedir as homedir2 } from "os";
|
|
24913
|
-
import { join as
|
|
25751
|
+
import { join as join3 } from "path";
|
|
24914
25752
|
var SESSION_ID_PATH_SEGMENT = /^[A-Za-z0-9._-]{1,128}$/;
|
|
24915
25753
|
var PRIVATE_FILE = 384;
|
|
24916
25754
|
function sessionStateRoot(env) {
|
|
24917
25755
|
const stateDir = env["DISPATCH_STATE_DIR"];
|
|
24918
25756
|
if (stateDir)
|
|
24919
25757
|
return stateDir;
|
|
24920
|
-
return
|
|
25758
|
+
return join3(env["XDG_STATE_HOME"] || join3(homedir2(), ".local", "state"), "dispatch");
|
|
24921
25759
|
}
|
|
24922
25760
|
function sessionDirectory(env, sessionId) {
|
|
24923
25761
|
if (!SESSION_ID_PATH_SEGMENT.test(sessionId) || sessionId === "." || sessionId === "..") {
|
|
24924
25762
|
throw new Error(`dispatch: the session id ${JSON.stringify(sessionId)} is not 1-128 letters, digits, ".", "_" or "-"`);
|
|
24925
25763
|
}
|
|
24926
|
-
return
|
|
25764
|
+
return join3(sessionStateRoot(env), "sessions", sessionId);
|
|
24927
25765
|
}
|
|
24928
25766
|
function writeAtomically(path, text) {
|
|
24929
25767
|
const temporary = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
@@ -25238,6 +26076,7 @@ var dispatchCommands = {
|
|
|
25238
26076
|
dispatch_doc_read: acting(docReadCommand),
|
|
25239
26077
|
dispatch_request_approval: acting(requestApprovalCommand),
|
|
25240
26078
|
dispatch_plan_approve: acting(planApproveCommand),
|
|
26079
|
+
dispatch_review: acting(reviewCommand),
|
|
25241
26080
|
dispatch_result: acting(resultCommand),
|
|
25242
26081
|
dispatch_results: acting(resultsCommand),
|
|
25243
26082
|
dispatch_artifact: acting(artifactCommand),
|
|
@@ -25263,7 +26102,7 @@ import {
|
|
|
25263
26102
|
statSync as statSync2,
|
|
25264
26103
|
writeFileSync as writeFileSync2
|
|
25265
26104
|
} from "fs";
|
|
25266
|
-
import { join as
|
|
26105
|
+
import { join as join4 } from "path";
|
|
25267
26106
|
|
|
25268
26107
|
// ../envoy-client/src/askgate-marker.ts
|
|
25269
26108
|
var MARKER_MAX_AGE_MS = 2 * 60 * 60000;
|
|
@@ -25278,7 +26117,7 @@ function ensureDirectory(dir) {
|
|
|
25278
26117
|
}
|
|
25279
26118
|
function writeSessionTitle(dir, title) {
|
|
25280
26119
|
ensureDirectory(dir);
|
|
25281
|
-
writeAtomically(
|
|
26120
|
+
writeAtomically(join4(dir, "title"), title);
|
|
25282
26121
|
}
|
|
25283
26122
|
|
|
25284
26123
|
// ../envoy-client/src/envoy-settings.ts
|
|
@@ -28092,7 +28931,7 @@ function subagentSessionCheck() {
|
|
|
28092
28931
|
import { logger as logger5 } from "@oh-my-pi/pi-utils";
|
|
28093
28932
|
|
|
28094
28933
|
// src/capability-report.ts
|
|
28095
|
-
import { readdir, stat } from "fs/promises";
|
|
28934
|
+
import { readdir as readdir2, stat } from "fs/promises";
|
|
28096
28935
|
import path3 from "path";
|
|
28097
28936
|
var CAPABILITY_CHECK_TIMEOUT_MS = 8000;
|
|
28098
28937
|
var CAPABILITY_REPORT_TIMEOUT_MS = 1e4;
|
|
@@ -28235,7 +29074,7 @@ var EXTENSION_INDEXES = ["index.ts", "index.js", "index.mjs", "index.cjs"];
|
|
|
28235
29074
|
var SKILL_DIRECTORIES = [path3.join(".omp", "skills"), path3.join(".claude", "skills")];
|
|
28236
29075
|
async function namesIn(directory) {
|
|
28237
29076
|
try {
|
|
28238
|
-
return await
|
|
29077
|
+
return await readdir2(directory);
|
|
28239
29078
|
} catch (error) {
|
|
28240
29079
|
if (hasErrnoCode(error, "ENOENT") || hasErrnoCode(error, "ENOTDIR"))
|
|
28241
29080
|
return [];
|
|
@@ -28883,7 +29722,7 @@ import { logger as logger3 } from "@oh-my-pi/pi-utils";
|
|
|
28883
29722
|
// package.json
|
|
28884
29723
|
var package_default = {
|
|
28885
29724
|
name: "@sjawhar/pi-legion",
|
|
28886
|
-
version: "19.
|
|
29725
|
+
version: "19.27.0",
|
|
28887
29726
|
license: "Apache-2.0",
|
|
28888
29727
|
type: "module",
|
|
28889
29728
|
omp: {
|
|
@@ -29697,7 +30536,7 @@ function createStallAborter(deps) {
|
|
|
29697
30536
|
}
|
|
29698
30537
|
|
|
29699
30538
|
// src/stall-evidence.ts
|
|
29700
|
-
import { readdir as
|
|
30539
|
+
import { readdir as readdir3, readFile as readFile3 } from "fs/promises";
|
|
29701
30540
|
import path5 from "path";
|
|
29702
30541
|
var CLOCK_TICKS_PER_SECOND = 100;
|
|
29703
30542
|
var STALL_READ_ENVIRONMENT = ["PI_TOOL_CALL_ID", "PI_SESSION_FILE"];
|
|
@@ -29708,7 +30547,7 @@ async function readStallProcesses(deps = {}) {
|
|
|
29708
30547
|
const agentPid = deps.agentPid ?? process.pid;
|
|
29709
30548
|
let entries;
|
|
29710
30549
|
try {
|
|
29711
|
-
entries = await
|
|
30550
|
+
entries = await readdir3(procRoot);
|
|
29712
30551
|
} catch (error) {
|
|
29713
30552
|
return { read: false, reason: `${procRoot} could not be read: ${messageFor(error)}` };
|
|
29714
30553
|
}
|
|
@@ -29857,7 +30696,7 @@ async function readEnvironment(file) {
|
|
|
29857
30696
|
}
|
|
29858
30697
|
async function readTextFile(file) {
|
|
29859
30698
|
try {
|
|
29860
|
-
return await
|
|
30699
|
+
return await readFile3(file, "utf8");
|
|
29861
30700
|
} catch {
|
|
29862
30701
|
return;
|
|
29863
30702
|
}
|