Skip to content

Commit 257355c

Browse files
yyyu-googlecopybara-github
authored andcommitted
feat: include all supported authentication options
PiperOrigin-RevId: 595444823
1 parent ea0dcb7 commit 257355c

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

src/index.ts

+30-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
/* tslint:disable */
19-
import {GoogleAuth} from 'google-auth-library';
19+
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
2020

2121
import {
2222
processCountTokenResponse,
@@ -64,7 +64,8 @@ export class VertexAI {
6464
this.preview = new VertexAI_Preview(
6565
init.project,
6666
init.location,
67-
init.apiEndpoint
67+
init.apiEndpoint,
68+
init.googleAuthOptions
6869
);
6970
}
7071
}
@@ -73,26 +74,48 @@ export class VertexAI {
7374
* VertexAI class internal implementation for authentication.
7475
*/
7576
export class VertexAI_Preview {
76-
protected googleAuth: GoogleAuth = new GoogleAuth({
77-
scopes: 'https://www.googleapis.com/auth/cloud-platform',
78-
});
77+
protected googleAuth: GoogleAuth;
7978

8079
/**
8180
* @constructor
8281
* @param {string} - project The Google Cloud project to use for the request
8382
* @param {string} - location The Google Cloud project location to use for the request
84-
* @param {string} - apiEndpoint The base Vertex AI endpoint to use for the request. If
83+
* @param {string} - [apiEndpoint] The base Vertex AI endpoint to use for the request. If
8584
* not provided, the default regionalized endpoint
8685
* (i.e. us-central1-aiplatform.googleapis.com) will be used.
86+
* @param {GoogleAuthOptions} - [googleAuthOptions] The Authentication options provided by google-auth-library.
87+
* Complete list of authentication options is documented in the GoogleAuthOptions interface:
88+
* https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts
8789
*/
8890
constructor(
8991
readonly project: string,
9092
readonly location: string,
91-
readonly apiEndpoint?: string
93+
readonly apiEndpoint?: string,
94+
readonly googleAuthOptions?: GoogleAuthOptions
9295
) {
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+
}
93115
this.project = project;
94116
this.location = location;
95117
this.apiEndpoint = apiEndpoint;
118+
this.googleAuth = new GoogleAuth(opts);
96119
}
97120

98121
/**

src/types/content.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,23 @@
1515
* limitations under the License.
1616
*/
1717

18+
// @ts-nocheck
19+
import {GoogleAuthOptions} from 'google-auth-library';
20+
1821
/**
1922
* Params used to initialize the Vertex SDK
2023
* @param{string} project - the project name of your Google Cloud project. It is not the numeric project ID.
2124
* @param{string} location - the location of your project.
22-
* @param{string} apiEndpoint - Optional. If not specified, a default value will be resolved by SDK.
25+
* @param{string} [apiEndpoint] - If not specified, a default value will be resolved by SDK.
26+
* @param {GoogleAuthOptions} - [googleAuthOptions] The Authentication options provided by google-auth-library.
27+
* Complete list of authentication options is documented in the GoogleAuthOptions interface:
28+
* https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts
2329
*/
2430
export declare interface VertexInit {
2531
project: string;
2632
location: string;
2733
apiEndpoint?: string;
34+
googleAuthOptions?: GoogleAuthOptions;
2835
}
2936

3037
/**

test/index_test.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,39 @@ describe('VertexAI', () => {
187187
fetchSpy = spyOn(global, 'fetch').and.resolveTo(fetchResult);
188188
});
189189

190-
it('should be instantiated', () => {
190+
it('given undefined google auth options, should be instantiated', () => {
191191
expect(vertexai).toBeInstanceOf(VertexAI);
192192
});
193193

194+
it('given specified google auth options, should be instantiated', () => {
195+
const googleAuthOptions = {
196+
scopes: 'test.scopes',
197+
};
198+
const vetexai1 = new VertexAI({
199+
project: PROJECT,
200+
location: LOCATION,
201+
googleAuthOptions: googleAuthOptions,
202+
});
203+
expect(vetexai1).toBeInstanceOf(VertexAI);
204+
});
205+
206+
it('given inconsistent project ID, should throw error', () => {
207+
const googleAuthOptions = {
208+
projectId: 'another_project',
209+
};
210+
expect(() => {
211+
new VertexAI({
212+
project: PROJECT,
213+
location: LOCATION,
214+
googleAuthOptions: googleAuthOptions,
215+
});
216+
}).toThrow(
217+
new Error(
218+
'inconsistent project ID values. argument project got value test_project but googleAuthOptions.projectId got value another_project'
219+
)
220+
);
221+
});
222+
194223
describe('generateContent', () => {
195224
it('returns a GenerateContentResponse', async () => {
196225
const req: GenerateContentRequest = {

0 commit comments

Comments
 (0)