Skip to content

Commit 558aee9

Browse files
yyyu-googlecopybara-github
authored andcommitted
fix: throw error when GoogleAuthOptions.scopes doesn't include required scope.
PiperOrigin-RevId: 597912107
1 parent 885fbb7 commit 558aee9

File tree

2 files changed

+74
-20
lines changed

2 files changed

+74
-20
lines changed

src/index.ts

+37-19
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,7 @@ export class VertexAI_Preview {
9393
readonly apiEndpoint?: string,
9494
readonly googleAuthOptions?: GoogleAuthOptions
9595
) {
96-
let opts: GoogleAuthOptions;
97-
if (!googleAuthOptions) {
98-
opts = {
99-
scopes: 'https://www.googleapis.com/auth/cloud-platform',
100-
};
101-
} else {
102-
if (
103-
googleAuthOptions.projectId &&
104-
googleAuthOptions.projectId !== project
105-
) {
106-
throw new Error(
107-
`inconsistent project ID values. argument project got value ${project} but googleAuthOptions.projectId got value ${googleAuthOptions.projectId}`
108-
);
109-
}
110-
opts = googleAuthOptions;
111-
if (!opts.scopes) {
112-
opts.scopes = 'https://www.googleapis.com/auth/cloud-platform';
113-
}
114-
}
96+
const opts = this.validateGoogleAuthOptions(project, googleAuthOptions);
11597
this.project = project;
11698
this.location = location;
11799
this.apiEndpoint = apiEndpoint;
@@ -155,6 +137,42 @@ export class VertexAI_Preview {
155137
modelParams.safety_settings
156138
);
157139
}
140+
141+
validateGoogleAuthOptions(
142+
project: string,
143+
googleAuthOptions?: GoogleAuthOptions
144+
): GoogleAuthOptions {
145+
let opts: GoogleAuthOptions;
146+
const requiredScope = 'https://www.googleapis.com/auth/cloud-platform';
147+
if (!googleAuthOptions) {
148+
opts = {
149+
scopes: requiredScope,
150+
};
151+
return opts;
152+
}
153+
if (
154+
googleAuthOptions.projectId &&
155+
googleAuthOptions.projectId !== project
156+
) {
157+
throw new Error(
158+
`inconsistent project ID values. argument project got value ${project} but googleAuthOptions.projectId got value ${googleAuthOptions.projectId}`
159+
);
160+
}
161+
opts = googleAuthOptions;
162+
if (!opts.scopes) {
163+
opts.scopes = requiredScope;
164+
return opts;
165+
}
166+
if (
167+
(typeof opts.scopes === 'string' && opts.scopes !== requiredScope) ||
168+
(Array.isArray(opts.scopes) && opts.scopes.indexOf(requiredScope) < 0)
169+
) {
170+
throw new GoogleAuthError(
171+
`input GoogleAuthOptions.scopes ${opts.scopes} doesn't contain required scope ${requiredScope}, please include ${requiredScope} into GoogleAuthOptions.scopes or leave GoogleAuthOptions.scopes undefined`
172+
);
173+
}
174+
return opts;
175+
}
158176
}
159177

160178
/**

test/index_test.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
SafetySetting,
3838
StreamGenerateContentResult,
3939
} from '../src/types/content';
40+
import {GoogleAuthError} from '../src/types/errors';
4041
import {constants} from '../src/util';
4142

4243
const PROJECT = 'test_project';
@@ -214,7 +215,7 @@ describe('VertexAI', () => {
214215

215216
it('given specified google auth options, should be instantiated', () => {
216217
const googleAuthOptions = {
217-
scopes: 'test.scopes',
218+
scopes: 'https://www.googleapis.com/auth/cloud-platform',
218219
};
219220
const vetexai1 = new VertexAI({
220221
project: PROJECT,
@@ -241,6 +242,41 @@ describe('VertexAI', () => {
241242
);
242243
});
243244

245+
it('given scopes missing required scope, should throw GoogleAuthError', () => {
246+
const invalidGoogleAuthOptionsStringScopes = {scopes: 'test.scopes'};
247+
expect(() => {
248+
new VertexAI({
249+
project: PROJECT,
250+
location: LOCATION,
251+
googleAuthOptions: invalidGoogleAuthOptionsStringScopes,
252+
});
253+
}).toThrow(
254+
new GoogleAuthError(
255+
"input GoogleAuthOptions.scopes test.scopes doesn't contain required scope " +
256+
'https://www.googleapis.com/auth/cloud-platform, ' +
257+
'please include https://www.googleapis.com/auth/cloud-platform into GoogleAuthOptions.scopes ' +
258+
'or leave GoogleAuthOptions.scopes undefined'
259+
)
260+
);
261+
const invalidGoogleAuthOptionsArrayScopes = {
262+
scopes: ['test1.scopes', 'test2.scopes'],
263+
};
264+
expect(() => {
265+
new VertexAI({
266+
project: PROJECT,
267+
location: LOCATION,
268+
googleAuthOptions: invalidGoogleAuthOptionsArrayScopes,
269+
});
270+
}).toThrow(
271+
new GoogleAuthError(
272+
"input GoogleAuthOptions.scopes test1.scopes,test2.scopes doesn't contain required scope " +
273+
'https://www.googleapis.com/auth/cloud-platform, ' +
274+
'please include https://www.googleapis.com/auth/cloud-platform into GoogleAuthOptions.scopes ' +
275+
'or leave GoogleAuthOptions.scopes undefined'
276+
)
277+
);
278+
});
279+
244280
describe('generateContent', () => {
245281
it('returns a GenerateContentResponse', async () => {
246282
const req: GenerateContentRequest = {

0 commit comments

Comments
 (0)