Skip to content

Commit a38857c

Browse files
feat(auth): use gtoken from internal class instead of dependency (#815)
1 parent 4ea941d commit a38857c

File tree

7 files changed

+69
-14
lines changed

7 files changed

+69
-14
lines changed

packages/google-auth-library-nodejs/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"gaxios": "7.1.3",
2727
"gcp-metadata": "8.1.2",
2828
"google-logging-utils": "1.1.3",
29-
"gtoken": "^8.0.0",
3029
"jws": "^4.0.0"
3130
},
3231
"devDependencies": {

packages/google-auth-library-nodejs/src/auth/jwtclient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import {GoogleToken} from 'gtoken';
15+
import {GoogleToken} from '../gtoken/googleToken';
16+
import {getCredentials} from '../gtoken/getCredentials';
1617
import * as stream from 'stream';
1718

1819
import {CredentialBody, Credentials, JWTInput} from './credentials';
@@ -257,8 +258,8 @@ export class JWT extends OAuth2Client implements IdTokenProvider {
257258
}
258259
this.credentials = result.tokens;
259260
this.credentials.refresh_token = 'jwt-placeholder';
260-
this.key = this.gtoken!.key;
261-
this.email = this.gtoken!.iss;
261+
this.key = this.gtoken!.googleTokenOptions?.key;
262+
this.email = this.gtoken!.googleTokenOptions?.iss;
262263
return result.tokens;
263264
}
264265

@@ -402,7 +403,7 @@ export class JWT extends OAuth2Client implements IdTokenProvider {
402403
return {private_key: this.key, client_email: this.email};
403404
} else if (this.keyFile) {
404405
const gtoken = this.createGToken();
405-
const creds = await gtoken.getCredentials(this.keyFile);
406+
const creds = await getCredentials(this.keyFile);
406407
return {private_key: creds.privateKey, client_email: creds.clientEmail};
407408
}
408409
throw new Error('A key or a keyFile must be provided to getCredentials.');

packages/google-auth-library-nodejs/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export {
9292
ExternalAccountAuthorizedUserClientOptions,
9393
} from './auth/externalAccountAuthorizedUserClient';
9494
export {PassThroughClient} from './auth/passthrough';
95+
export * from './gtoken/googleToken';
9596

9697
type ALL_EXPORTS = (typeof import('./'))[keyof typeof import('./')];
9798

packages/google-auth-library-nodejs/test/test.googleauth.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,7 +2936,10 @@ describe('googleauth', () => {
29362936
'Bearer initial-access-token',
29372937
);
29382938
scope.done();
2939-
assert.strictEqual('http://foo', (jwt as JWT).gtoken!.scope);
2939+
assert.strictEqual(
2940+
'http://foo',
2941+
(jwt as JWT).gtoken!.googleTokenOptions.scope,
2942+
);
29402943
});
29412944

29422945
// Allows a client to be instantiated from a certificate,
@@ -2958,7 +2961,10 @@ describe('googleauth', () => {
29582961
'Bearer initial-access-token',
29592962
);
29602963
scope.done();
2961-
assert.strictEqual('http://foo', (jwt as JWT).gtoken!.scope);
2964+
assert.strictEqual(
2965+
'http://foo',
2966+
(jwt as JWT).gtoken!.googleTokenOptions.scope,
2967+
);
29622968
});
29632969

29642970
// Allows a client to be instantiated from a certificate,
@@ -2980,6 +2986,9 @@ describe('googleauth', () => {
29802986
'Bearer initial-access-token',
29812987
);
29822988
scope.done();
2983-
assert.strictEqual('http://foo', (jwt as JWT).gtoken!.scope);
2989+
assert.strictEqual(
2990+
'http://foo',
2991+
(jwt as JWT).gtoken!.googleTokenOptions.scope,
2992+
);
29842993
});
29852994
});

packages/google-auth-library-nodejs/test/test.impersonated.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,28 @@ nock.disableNetConnect();
2727
const url = 'http://example.com';
2828

2929
function createGTokenMock(body: CredentialRequest) {
30-
return nock('https://oauth2.googleapis.com').post('/token').reply(200, body);
30+
return nock('https://oauth2.googleapis.com')
31+
.post(
32+
'/token',
33+
(newBody: string) => {
34+
const url = new URLSearchParams(newBody);
35+
const grantType = url.get('grant_type');
36+
const assertion = url.get('assertion');
37+
return (
38+
grantType === 'urn:ietf:params:oauth:grant-type:jwt-bearer' &&
39+
!!assertion
40+
);
41+
},
42+
{
43+
reqheaders: {
44+
'Content-Type': 'application/x-www-form-urlencoded',
45+
},
46+
},
47+
)
48+
.reply(200, {
49+
access_token: body.access_token,
50+
expires_in: 3600,
51+
});
3152
}
3253

3354
function createSampleJWTClient() {

packages/google-auth-library-nodejs/test/test.index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,27 @@ describe('index', () => {
3535
assert(gal.AwsClient);
3636
assert(gal.BaseExternalAccountClient);
3737
assert(gal.DownscopedClient);
38+
assert(gal.GoogleToken);
3839
assert(gal.Impersonated);
3940

4041
assert(gal.gaxios);
4142
assert(gal.gcpMetadata);
4243
});
44+
45+
it('should export GoogleToken types', () => {
46+
const tokenOptions: gal.TokenOptions = {
47+
key: 'key',
48+
};
49+
assert.ok(tokenOptions);
50+
51+
const tokenData: gal.TokenData = {
52+
access_token: 'token',
53+
};
54+
assert.ok(tokenData);
55+
56+
const transporter: gal.Transporter = {
57+
request: async () => ({}) as any,
58+
};
59+
assert.ok(transporter);
60+
});
4361
});

packages/google-auth-library-nodejs/test/test.jwt.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,19 @@ describe('jwt', () => {
9494
scope.done();
9595
assert.strictEqual(err, null);
9696
assert.notStrictEqual(creds, null);
97-
assert.strictEqual('foo@serviceaccount.com', jwt.gtoken!.iss);
98-
assert.strictEqual(PEM_PATH, jwt.gtoken!.keyFile);
97+
assert.strictEqual(
98+
'foo@serviceaccount.com',
99+
jwt.gtoken!.googleTokenOptions.iss,
100+
);
101+
assert.strictEqual(PEM_PATH, jwt.gtoken!.googleTokenOptions.keyFile);
99102
assert.strictEqual(
100103
['http://bar', 'http://foo'].join(' '),
101-
jwt.gtoken!.scope,
104+
jwt.gtoken!.googleTokenOptions.scope,
105+
);
106+
assert.strictEqual(
107+
'bar@subjectaccount.com',
108+
jwt.gtoken!.googleTokenOptions.sub,
102109
);
103-
assert.strictEqual('bar@subjectaccount.com', jwt.gtoken!.sub);
104110
assert.strictEqual('initial-access-token', jwt.credentials.access_token);
105111
assert.strictEqual(creds!.access_token, jwt.credentials.access_token);
106112
assert.strictEqual(creds!.refresh_token, jwt.credentials.refresh_token);
@@ -123,7 +129,7 @@ describe('jwt', () => {
123129

124130
jwt.authorize(() => {
125131
scope.done();
126-
assert.strictEqual('http://foo', jwt.gtoken!.scope);
132+
assert.strictEqual('http://foo', jwt.gtoken!.googleTokenOptions.scope);
127133
done();
128134
});
129135
});

0 commit comments

Comments
 (0)