@sebspark/gcp-iam 1.0.0 → 1.1.1

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/index.d.mts CHANGED
@@ -24,6 +24,6 @@ declare const clearCache: (key: string) => Promise<void>;
24
24
  * @param clientId OAUTH Client ID.
25
25
  * @returns ID Token.
26
26
  */
27
- declare const getApiGatewayTokenByClientId: (clientId: string) => Promise<string>;
27
+ declare const getApiGatewayTokenByClientId: (clientId: string, logger?: Logger) => Promise<string>;
28
28
 
29
29
  export { clearCache, getApiGatewayTokenByClientId, getApiGatewayTokenByUrl };
package/dist/index.d.ts CHANGED
@@ -24,6 +24,6 @@ declare const clearCache: (key: string) => Promise<void>;
24
24
  * @param clientId OAUTH Client ID.
25
25
  * @returns ID Token.
26
26
  */
27
- declare const getApiGatewayTokenByClientId: (clientId: string) => Promise<string>;
27
+ declare const getApiGatewayTokenByClientId: (clientId: string, logger?: Logger) => Promise<string>;
28
28
 
29
29
  export { clearCache, getApiGatewayTokenByClientId, getApiGatewayTokenByUrl };
package/dist/index.js CHANGED
@@ -80,6 +80,7 @@ var getApiGatewayTokenByUrl = async ({
80
80
  }) => {
81
81
  const cachedJwt = apiGatewayJwtCache.get(key || apiURL);
82
82
  if (cachedJwt) {
83
+ logger?.debug(`JWT for ${key || apiURL} found in cache.`);
83
84
  return cachedJwt;
84
85
  }
85
86
  try {
@@ -118,14 +119,16 @@ var getApiGatewayTokenByUrl = async ({
118
119
  "signBlob(...) returned an empty response. Cannot sign JWT."
119
120
  );
120
121
  }
121
- console.log("IAM KeyID", response.keyId);
122
+ logger?.debug(
123
+ `New JWT for ${key || apiURL} created. Signed with ${response.keyId}.`
124
+ );
122
125
  const signature = Buffer.from(response.signedBlob).toString("base64");
123
126
  const signedJWT = `${unsignedJWT}.${signature}`;
124
127
  apiGatewayJwtCache.put(key || apiURL, signedJWT, ttl);
125
128
  return signedJWT;
126
129
  } catch (error) {
127
130
  if (process.env.GCP_IAM_SOFT_FAIL === "true") {
128
- logger?.warn("Soft fail enabled, returning empty JWT");
131
+ logger?.info("Soft fail enabled, returning empty JWT");
129
132
  return "";
130
133
  }
131
134
  logger?.error("Error generating system JWT", error);
@@ -135,12 +138,22 @@ var getApiGatewayTokenByUrl = async ({
135
138
  var clearCache = async (key) => {
136
139
  apiGatewayJwtCache.clear(key);
137
140
  };
138
- var getApiGatewayTokenByClientId = async (clientId) => {
139
- const auth = new import_google_auth_library.GoogleAuth({
140
- scopes: "https://www.googleapis.com/auth/cloud-platform"
141
- });
142
- const client = await auth.getIdTokenClient(clientId);
143
- return await client.idTokenProvider.fetchIdToken(clientId);
141
+ var getApiGatewayTokenByClientId = async (clientId, logger) => {
142
+ try {
143
+ const auth = new import_google_auth_library.GoogleAuth({
144
+ scopes: "https://www.googleapis.com/auth/cloud-platform"
145
+ });
146
+ const client = await auth.getIdTokenClient(clientId);
147
+ return await client.idTokenProvider.fetchIdToken(clientId);
148
+ } catch (error) {
149
+ if (process.env.GCP_IAM_SOFT_FAIL === "true") {
150
+ logger?.info("Soft fail enabled, returning empty JWT.");
151
+ return "";
152
+ }
153
+ logger?.error("Error generating system JWT", error);
154
+ logger?.error(JSON.stringify(error, null, 2));
155
+ throw new Error(`Error generating system JWT: ${JSON.stringify(error)}`);
156
+ }
144
157
  };
145
158
  // Annotate the CommonJS export names for ESM import in node:
146
159
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -52,6 +52,7 @@ var getApiGatewayTokenByUrl = async ({
52
52
  }) => {
53
53
  const cachedJwt = apiGatewayJwtCache.get(key || apiURL);
54
54
  if (cachedJwt) {
55
+ logger?.debug(`JWT for ${key || apiURL} found in cache.`);
55
56
  return cachedJwt;
56
57
  }
57
58
  try {
@@ -90,14 +91,16 @@ var getApiGatewayTokenByUrl = async ({
90
91
  "signBlob(...) returned an empty response. Cannot sign JWT."
91
92
  );
92
93
  }
93
- console.log("IAM KeyID", response.keyId);
94
+ logger?.debug(
95
+ `New JWT for ${key || apiURL} created. Signed with ${response.keyId}.`
96
+ );
94
97
  const signature = Buffer.from(response.signedBlob).toString("base64");
95
98
  const signedJWT = `${unsignedJWT}.${signature}`;
96
99
  apiGatewayJwtCache.put(key || apiURL, signedJWT, ttl);
97
100
  return signedJWT;
98
101
  } catch (error) {
99
102
  if (process.env.GCP_IAM_SOFT_FAIL === "true") {
100
- logger?.warn("Soft fail enabled, returning empty JWT");
103
+ logger?.info("Soft fail enabled, returning empty JWT");
101
104
  return "";
102
105
  }
103
106
  logger?.error("Error generating system JWT", error);
@@ -107,12 +110,22 @@ var getApiGatewayTokenByUrl = async ({
107
110
  var clearCache = async (key) => {
108
111
  apiGatewayJwtCache.clear(key);
109
112
  };
110
- var getApiGatewayTokenByClientId = async (clientId) => {
111
- const auth = new GoogleAuth({
112
- scopes: "https://www.googleapis.com/auth/cloud-platform"
113
- });
114
- const client = await auth.getIdTokenClient(clientId);
115
- return await client.idTokenProvider.fetchIdToken(clientId);
113
+ var getApiGatewayTokenByClientId = async (clientId, logger) => {
114
+ try {
115
+ const auth = new GoogleAuth({
116
+ scopes: "https://www.googleapis.com/auth/cloud-platform"
117
+ });
118
+ const client = await auth.getIdTokenClient(clientId);
119
+ return await client.idTokenProvider.fetchIdToken(clientId);
120
+ } catch (error) {
121
+ if (process.env.GCP_IAM_SOFT_FAIL === "true") {
122
+ logger?.info("Soft fail enabled, returning empty JWT.");
123
+ return "";
124
+ }
125
+ logger?.error("Error generating system JWT", error);
126
+ logger?.error(JSON.stringify(error, null, 2));
127
+ throw new Error(`Error generating system JWT: ${JSON.stringify(error)}`);
128
+ }
116
129
  };
117
130
  export {
118
131
  clearCache,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/gcp-iam",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",