@vizamodo/aws-sts-core 0.4.24 → 0.4.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/sts/issue.js +63 -56
  2. package/package.json +1 -1
package/dist/sts/issue.js CHANGED
@@ -39,30 +39,37 @@ async function getSigningMaterial(input) {
39
39
  cachedPrivateKeyBase64 === input.privateKeyPkcs8Base64) {
40
40
  return { signingKey: cachedSigningKey, certBase64: cachedCertBase64 };
41
41
  }
42
- // Material rotated discard the stale resolved key so we re-import.
43
- if (cachedSigningKey) {
42
+ // If material rotated, reset promise so we re-import.
43
+ if (cachedSigningKey &&
44
+ (cachedCertBase64 !== input.certBase64 ||
45
+ cachedPrivateKeyBase64 !== input.privateKeyPkcs8Base64)) {
44
46
  signingKeyPromise = null;
45
47
  cachedSigningKey = null;
46
48
  }
47
49
  if (!signingKeyPromise) {
50
+ let keyBuffer;
48
51
  try {
49
- const keyBuffer = base64ToBytes(input.privateKeyPkcs8Base64);
50
- signingKeyPromise = crypto.subtle.importKey("pkcs8", keyBuffer, { name: "ECDSA", namedCurve: "P-256" }, false, ["sign"]);
52
+ keyBuffer = base64ToBytes(input.privateKeyPkcs8Base64);
51
53
  }
52
54
  catch {
53
55
  throw new InternalError("invalid_signing_material");
54
56
  }
57
+ signingKeyPromise = crypto.subtle
58
+ .importKey("pkcs8", keyBuffer, { name: "ECDSA", namedCurve: "P-256" }, false, ["sign"])
59
+ .then((key) => {
60
+ // ✅ Atomic assignment inside then (no race)
61
+ cachedSigningKey = key;
62
+ cachedCertBase64 = input.certBase64;
63
+ cachedPrivateKeyBase64 = input.privateKeyPkcs8Base64;
64
+ return key;
65
+ })
66
+ .catch(() => {
67
+ signingKeyPromise = null; // allow retry
68
+ throw new InternalError("invalid_signing_material");
69
+ });
55
70
  }
56
- try {
57
- cachedSigningKey = await signingKeyPromise;
58
- cachedCertBase64 = input.certBase64;
59
- cachedPrivateKeyBase64 = input.privateKeyPkcs8Base64;
60
- return { signingKey: cachedSigningKey, certBase64: cachedCertBase64 };
61
- }
62
- catch {
63
- signingKeyPromise = null; // allow retry on next call
64
- throw new InternalError("invalid_signing_material");
65
- }
71
+ const key = await signingKeyPromise;
72
+ return { signingKey: key, certBase64: input.certBase64 };
66
73
  }
67
74
  // ---------------------------------------------------------------------------
68
75
  // Profile TTL resolution
@@ -89,49 +96,49 @@ export async function issueAwsCredentials(input) {
89
96
  cachedCertSerialSource = normalizedCert;
90
97
  }
91
98
  const cacheKey = `${region}|${roleArn}|${profileArn}|${trustAnchorArn}|${certSerialDec}`;
92
- // ---- Build SigV4 request ----
93
- const { signingKey } = await getSigningMaterial({
94
- certBase64: normalizedCert,
95
- privateKeyPkcs8Base64,
96
- });
97
- const host = `${SERVICE}.${region}.amazonaws.com`;
98
- const iso = new Date().toISOString();
99
- const amzDate = isoToAmzDate(iso);
100
- const dateStamp = iso.slice(0, 4) + iso.slice(5, 7) + iso.slice(8, 10);
101
- const body = JSON.stringify({ trustAnchorArn, profileArn, roleArn, durationSeconds: sessionTtl });
102
- const payloadHash = await sha256Hex(body);
103
- const baseHeaders = {
104
- "content-type": "application/json",
105
- "host": host,
106
- "x-amz-date": amzDate,
107
- "x-amz-x509": normalizedCert,
108
- };
109
- const { canonicalHeaders, signedHeaders } = canonicalizeHeaders(baseHeaders);
110
- const credentialScope = `${dateStamp}/${region}/${SERVICE}/aws4_request`;
111
- const canonicalRequest = buildCanonicalRequest({
112
- method: "POST",
113
- canonicalUri: PATH,
114
- query: "",
115
- canonicalHeaders,
116
- signedHeaders,
117
- payloadHash,
118
- });
119
- const canonicalRequestHash = await sha256Hex(canonicalRequest);
120
- const stringToSign = buildStringToSign({
121
- algorithm: ALGORITHM,
122
- amzDate,
123
- credentialScope,
124
- canonicalRequestHash,
125
- });
126
- const signatureHex = await signStringToSign(stringToSign, signingKey);
127
- const finalHeaders = new Headers({
128
- "Content-Type": "application/json",
129
- "X-Amz-Date": amzDate,
130
- "X-Amz-X509": normalizedCert,
131
- "Authorization": `${ALGORITHM} Credential=${certSerialDec}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signatureHex}`,
132
- });
133
- const issuedAt = Date.now(); // snapshot before the network round-trip
134
99
  return getCachedOrFetch(cacheKey, async () => {
100
+ // ---- Build SigV4 request (moved inside fetcher) ----
101
+ const { signingKey } = await getSigningMaterial({
102
+ certBase64: normalizedCert,
103
+ privateKeyPkcs8Base64,
104
+ });
105
+ const host = `${SERVICE}.${region}.amazonaws.com`;
106
+ const iso = new Date().toISOString();
107
+ const amzDate = isoToAmzDate(iso);
108
+ const dateStamp = iso.slice(0, 4) + iso.slice(5, 7) + iso.slice(8, 10);
109
+ const body = JSON.stringify({ trustAnchorArn, profileArn, roleArn, durationSeconds: sessionTtl });
110
+ const payloadHash = await sha256Hex(body);
111
+ const baseHeaders = {
112
+ "content-type": "application/json",
113
+ "host": host,
114
+ "x-amz-date": amzDate,
115
+ "x-amz-x509": normalizedCert,
116
+ };
117
+ const { canonicalHeaders, signedHeaders } = canonicalizeHeaders(baseHeaders);
118
+ const credentialScope = `${dateStamp}/${region}/${SERVICE}/aws4_request`;
119
+ const canonicalRequest = buildCanonicalRequest({
120
+ method: "POST",
121
+ canonicalUri: PATH,
122
+ query: "",
123
+ canonicalHeaders,
124
+ signedHeaders,
125
+ payloadHash,
126
+ });
127
+ const canonicalRequestHash = await sha256Hex(canonicalRequest);
128
+ const stringToSign = buildStringToSign({
129
+ algorithm: ALGORITHM,
130
+ amzDate,
131
+ credentialScope,
132
+ canonicalRequestHash,
133
+ });
134
+ const signatureHex = await signStringToSign(stringToSign, signingKey);
135
+ const finalHeaders = new Headers({
136
+ "Content-Type": "application/json",
137
+ "X-Amz-Date": amzDate,
138
+ "X-Amz-X509": normalizedCert,
139
+ "Authorization": `${ALGORITHM} Credential=${certSerialDec}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signatureHex}`,
140
+ });
141
+ const issuedAt = Date.now();
135
142
  const res = await fetch(`https://${host}${PATH}`, {
136
143
  method: "POST",
137
144
  headers: finalHeaders,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizamodo/aws-sts-core",
3
- "version": "0.4.24",
3
+ "version": "0.4.26",
4
4
  "description": "Pure AWS STS + SigV4 (X509 Roles Anywhere) core logic",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",