Skip to content

Commit 794f40a

Browse files
authored
feat(toolbox-core): Add helper methods for retrieving Google ID Tokens (#41)
* feat: add client headers * lint * lint * resolve comments * disable any lint warning on a line * working code * fix tests * lint * Only intercept request if it is sent to the toolbox server * move changes to correct pr
1 parent c7ff232 commit 794f40a

File tree

4 files changed

+86
-43
lines changed

4 files changed

+86
-43
lines changed

packages/toolbox-core/package-lock.json

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

packages/toolbox-core/package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@
1515
"agents",
1616
"mcp"
1717
],
18-
"main": "build/client.js",
19-
"types": "build/client.d.ts",
18+
"exports": {
19+
".": {
20+
"import": "./build/client.js",
21+
"types": "./build/client.d.ts"
22+
},
23+
"./auth": {
24+
"import": "./build/authMethods.js",
25+
"types": "./build/authMethods.d.ts"
26+
}
27+
},
2028
"repository": {
2129
"type": "git",
2230
"url": "git+https://github.com/googleapis/mcp-toolbox-sdk-js.git"
@@ -45,7 +53,6 @@
4553
"@types/tmp": "^0.2.6",
4654
"cross-env": "^7.0.3",
4755
"fs-extra": "^11.3.0",
48-
"google-auth-library": "^9.15.1",
4956
"gts": "^5.3.1",
5057
"jest": "^29.7.0",
5158
"tmp": "^0.2.3",
@@ -54,6 +61,7 @@
5461
},
5562
"dependencies": {
5663
"axios": "^1.9.0",
64+
"google-auth-library": "^9.15.1",
5765
"zod": "^3.24.4"
5866
}
5967
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {GoogleAuth} from 'google-auth-library';
2+
3+
async function getGoogleIdToken(url: string) {
4+
const auth = new GoogleAuth();
5+
const client = await auth.getIdTokenClient(url);
6+
const id_token = await client.idTokenProvider.fetchIdToken(url);
7+
return `Bearer ${id_token}`;
8+
}
9+
10+
export {getGoogleIdToken};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import {getGoogleIdToken} from '../src/toolbox_core/authMethods';
16+
import {GoogleAuth} from 'google-auth-library';
17+
18+
jest.mock('google-auth-library', () => ({GoogleAuth: jest.fn()}));
19+
20+
describe('getGoogleIdToken', () => {
21+
const mockUrl = 'https://example.com';
22+
const mockToken = 'mock-id-token';
23+
24+
let mockGetIdTokenClient: jest.Mock;
25+
let mockFetchIdToken: jest.Mock;
26+
27+
beforeEach(() => {
28+
// Reset mocks before each test
29+
mockFetchIdToken = jest.fn().mockResolvedValue(mockToken);
30+
mockGetIdTokenClient = jest.fn().mockResolvedValue({
31+
idTokenProvider: {
32+
fetchIdToken: mockFetchIdToken,
33+
},
34+
});
35+
(GoogleAuth as jest.MockedClass<typeof GoogleAuth>).mockImplementation(
36+
() =>
37+
({
38+
getIdTokenClient: mockGetIdTokenClient,
39+
}) as unknown as GoogleAuth
40+
);
41+
});
42+
43+
it('should return a Bearer token on successful fetch', async () => {
44+
const token = await getGoogleIdToken(mockUrl);
45+
expect(token).toBe(`Bearer ${mockToken}`);
46+
expect(GoogleAuth).toHaveBeenCalledTimes(1);
47+
expect(mockGetIdTokenClient).toHaveBeenCalledWith(mockUrl);
48+
expect(mockFetchIdToken).toHaveBeenCalledWith(mockUrl);
49+
});
50+
51+
it('should propagate errors from getIdTokenClient', async () => {
52+
const errorMessage = 'Failed to get ID token client';
53+
mockGetIdTokenClient.mockRejectedValue(new Error(errorMessage));
54+
55+
await expect(getGoogleIdToken(mockUrl)).rejects.toThrow(errorMessage);
56+
});
57+
58+
it('should propagate errors from fetchIdToken', async () => {
59+
const errorMessage = 'Failed to fetch ID token';
60+
mockFetchIdToken.mockRejectedValue(new Error(errorMessage));
61+
62+
await expect(getGoogleIdToken(mockUrl)).rejects.toThrow(errorMessage);
63+
});
64+
});

0 commit comments

Comments
 (0)