Skip to content

Commit b4f4057

Browse files
authored
Use an OAuth 2.0 access token for Domain-Wide Delegation (#388)
Fixes #387
1 parent 39c96a3 commit b4f4057

4 files changed

Lines changed: 13 additions & 10 deletions

File tree

dist/main/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/iamcredentials.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class IAMCredentialsClient extends Client {
139139
method: `POST`,
140140
path: pth,
141141
headers: headers,
142-
body: body,
142+
body: body.toString(),
143143
});
144144

145145
try {
@@ -149,8 +149,8 @@ export class IAMCredentialsClient extends Client {
149149
if (statusCode < 200 || statusCode > 299) {
150150
throw new Error(`Failed to call ${pth}: HTTP ${statusCode}: ${respBody || '[no body]'}`);
151151
}
152-
const parsed = JSON.parse(respBody) as { accessToken: string };
153-
return parsed.accessToken;
152+
const parsed = JSON.parse(respBody) as { access_token: string };
153+
return parsed.access_token;
154154
} catch (err) {
155155
const msg = errorMessage(err);
156156
throw new Error(

src/client/workload_identity_federation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class WorkloadIdentityFederationClient extends Client implements AuthClie
8080
const logger = this._logger.withNamespace(`getToken`);
8181

8282
const now = new Date().getTime();
83-
if (this.#cachedToken && this.#cachedAt && now - this.#cachedAt > 60_000) {
83+
if (this.#cachedToken && this.#cachedAt && now - this.#cachedAt < 30_000) {
8484
logger.debug(`Using cached token`, {
8585
now: now,
8686
cachedAt: this.#cachedAt,
@@ -141,7 +141,7 @@ export class WorkloadIdentityFederationClient extends Client implements AuthClie
141141
const pth = `${this._endpoints.iamcredentials}/projects/-/serviceAccounts/${this.#serviceAccount}:signJwt`;
142142

143143
const headers = {
144-
Authorization: `Bearer ${this.getToken()}`,
144+
Authorization: `Bearer ${await this.getToken()}`,
145145
};
146146

147147
const body = {

src/main.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,14 @@ export async function run(logger: Logger) {
253253
);
254254
}
255255

256+
let accessToken: string;
257+
256258
// If a subject was provided, use the traditional OAuth 2.0 flow to
257259
// perform Domain-Wide Delegation. Otherwise, use the modern IAM
258260
// Credentials endpoints.
259-
let accessToken;
260261
if (accessTokenSubject) {
262+
logger.debug(`Using Domain-Wide Delegation flow`);
263+
261264
if (accessTokenLifetime > 3600) {
262265
logger.info(
263266
`An access token subject was specified, triggering Domain-Wide ` +
@@ -273,10 +276,10 @@ export async function run(logger: Logger) {
273276
accessTokenLifetime,
274277
);
275278
const signedJWT = await client.signJWT(unsignedJWT);
276-
277279
accessToken =
278280
await iamCredentialsClient.generateDomainWideDelegationAccessToken(signedJWT);
279281
} else {
282+
logger.debug(`Using normal access token flow`);
280283
accessToken = await iamCredentialsClient.generateAccessToken({
281284
serviceAccount,
282285
delegates,

0 commit comments

Comments
 (0)