Skip to content

Commit b6f86e2

Browse files
committed
feat(vertex): add AuthClient interface support for improved auth flexibility
- Add authClient option to ClientOptions interface - Support both authClient and googleAuth for backward compatibility - Update documentation with examples for different auth methods - Add support for Impersonated credentials via AuthClient interface Addresses CE-673
1 parent 6496105 commit b6f86e2

File tree

3 files changed

+118
-6
lines changed

3 files changed

+118
-6
lines changed

packages/vertex-sdk/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,66 @@ main();
4141

4242
For more details on how to use the SDK, see the [README.md for the main Anthropic SDK](https://github.com/anthropics/anthropic-sdk-typescript/tree/main#anthropic-typescript-api-library) which this library extends.
4343

44+
## Authentication
45+
46+
This library supports multiple authentication methods:
47+
48+
### Default authentication
49+
50+
The client automatically uses the default Google Cloud authentication flow:
51+
52+
```js
53+
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
54+
55+
// Uses default authentication and environment variables
56+
const client = new AnthropicVertex({
57+
region: 'us-central1',
58+
projectId: 'my-project-id',
59+
});
60+
```
61+
62+
### Custom GoogleAuth configuration
63+
64+
You can customize the authentication using the `googleAuth` option:
65+
66+
```js
67+
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
68+
import { GoogleAuth } from 'google-auth-library';
69+
70+
const client = new AnthropicVertex({
71+
googleAuth: new GoogleAuth({
72+
scopes: 'https://www.googleapis.com/auth/cloud-platform',
73+
keyFile: '/path/to/service-account.json',
74+
}),
75+
region: 'us-central1',
76+
projectId: 'my-project-id',
77+
});
78+
```
79+
80+
### Pre-configured AuthClient
81+
82+
For advanced use cases like impersonation, you can provide a pre-configured `AuthClient`:
83+
84+
```js
85+
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
86+
import { GoogleAuth, Impersonated } from 'google-auth-library';
87+
88+
// Create an impersonated credential
89+
const authClient = new Impersonated({
90+
sourceClient: await new GoogleAuth().getClient(),
91+
targetPrincipal: '[email protected]',
92+
lifetime: 30,
93+
delegates: [],
94+
targetScopes: ['https://www.googleapis.com/auth/cloud-platform'],
95+
});
96+
97+
const client = new AnthropicVertex({
98+
authClient,
99+
region: 'us-central1',
100+
projectId: 'my-project-id',
101+
});
102+
```
103+
44104
## Requirements
45105

46106
TypeScript >= 4.5 is supported.

packages/vertex-sdk/examples/vertex.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
#!/usr/bin/env -S npm run tsn -T
22

33
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
4+
import { GoogleAuth, Impersonated } from 'google-auth-library';
45

6+
// Example 1: Default configuration
57
// Reads from the `CLOUD_ML_REGION` & `ANTHROPIC_VERTEX_PROJECT_ID`
68
// environment variables.
79
const client = new AnthropicVertex();
810

11+
// Example 2: Using googleAuth (existing approach)
12+
const clientWithGoogleAuth = new AnthropicVertex({
13+
googleAuth: new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' }),
14+
region: 'us-central1',
15+
projectId: 'my-project',
16+
});
17+
18+
// Example 3: Using authClient (new approach) with Impersonated credentials
19+
async function createClientWithImpersonation() {
20+
const authClient = new Impersonated({
21+
sourceClient: await new GoogleAuth().getClient(),
22+
targetPrincipal: '[email protected]',
23+
lifetime: 30,
24+
delegates: [],
25+
targetScopes: ['https://www.googleapis.com/auth/cloud-platform'],
26+
});
27+
28+
return new AnthropicVertex({
29+
authClient,
30+
region: 'us-central1',
31+
projectId: 'my-project',
32+
});
33+
}
34+
935
async function main() {
1036
const result = await client.messages.create({
1137
messages: [

packages/vertex-sdk/src/client.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BaseAnthropic, ClientOptions as CoreClientOptions } from '@anthropic-ai/sdk/client';
22
import * as Resources from '@anthropic-ai/sdk/resources/index';
3-
import { GoogleAuth } from 'google-auth-library';
3+
import { GoogleAuth, AuthClient } from 'google-auth-library';
44
import { readEnv } from './internal/utils/env';
55
import { FinalRequestOptions } from './internal/request-options';
66
import { FinalizedRequestInit } from './internal/types';
@@ -27,22 +27,43 @@ export type ClientOptions = Omit<CoreClientOptions, 'apiKey' | 'authToken'> & {
2727
* ```
2828
*/
2929
googleAuth?: GoogleAuth | null | undefined;
30+
31+
/**
32+
* Provide a pre-configured `AuthClient` instance from the
33+
* [google-auth-library](https://www.npmjs.com/package/google-auth-library) package.
34+
*
35+
* This is useful when you want to use a specific authentication method like
36+
* Impersonated credentials:
37+
* ```ts
38+
* new AnthropicVertex({
39+
* authClient: new Impersonated({
40+
* sourceClient: await new GoogleAuth().getClient(),
41+
* targetPrincipal: 'impersonated-account@projectID.iam.gserviceaccount.com',
42+
* lifetime: 30,
43+
* delegates: [],
44+
* targetScopes: ['https://www.googleapis.com/auth/cloud-platform']
45+
* })
46+
* })
47+
* ```
48+
*/
49+
authClient?: AuthClient | null | undefined;
3050
};
3151

3252
export class AnthropicVertex extends BaseAnthropic {
3353
region: string;
3454
projectId: string | null;
3555
accessToken: string | null;
3656

37-
private _auth: GoogleAuth;
38-
private _authClientPromise: ReturnType<GoogleAuth['getClient']>;
57+
private _auth?: GoogleAuth;
58+
private _authClientPromise: Promise<AuthClient>;
3959

4060
/**
4161
* API Client for interfacing with the Anthropic Vertex API.
4262
*
4363
* @param {string | null} opts.accessToken
4464
* @param {string | null} opts.projectId
4565
* @param {GoogleAuth} opts.googleAuth - Override the default google auth config
66+
* @param {AuthClient} opts.authClient - Provide a pre-configured AuthClient instance (alternative to googleAuth)
4667
* @param {string | null} [opts.region=process.env['CLOUD_ML_REGION']]
4768
* @param {string} [opts.baseURL=process.env['ANTHROPIC_VERTEX__BASE_URL'] ?? https://${region}-aiplatform.googleapis.com/v1] - Override the default base URL for the API.
4869
* @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
@@ -74,9 +95,14 @@ export class AnthropicVertex extends BaseAnthropic {
7495
this.projectId = projectId;
7596
this.accessToken = opts.accessToken ?? null;
7697

77-
this._auth =
78-
opts.googleAuth ?? new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' });
79-
this._authClientPromise = this._auth.getClient();
98+
// Support both authClient and googleAuth for backward compatibility
99+
if (opts.authClient) {
100+
this._authClientPromise = Promise.resolve(opts.authClient);
101+
} else {
102+
this._auth =
103+
opts.googleAuth ?? new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' });
104+
this._authClientPromise = this._auth.getClient();
105+
}
80106
}
81107

82108
messages: MessagesResource = makeMessagesResource(this);

0 commit comments

Comments
 (0)