-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathauth.ts
67 lines (56 loc) · 1.82 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import {
ensureAuthOptionScopes,
GoogleAbstractedClientOps,
GoogleAbstractedFetchClient,
GoogleBaseLLMInput,
} from "@langchain/google-common";
import {
getAccessToken,
getCredentials,
Credentials,
} from "web-auth-library/google";
export type WebGoogleAuthOptions = {
credentials: string | Credentials;
scope?: string | string[];
accessToken?: string;
responseModality?: string;
};
export class WebGoogleAuth extends GoogleAbstractedFetchClient {
options: WebGoogleAuthOptions;
constructor(fields: GoogleBaseLLMInput<WebGoogleAuthOptions> | undefined) {
super();
const options = fields?.authOptions;
const accessToken = options?.accessToken;
const credentials =
options?.credentials ??
getEnvironmentVariable("GOOGLE_WEB_CREDENTIALS") ??
getEnvironmentVariable("GOOGLE_VERTEX_AI_WEB_CREDENTIALS");
if (credentials === undefined)
throw new Error(
`Credentials not found. Please set the GOOGLE_WEB_CREDENTIALS environment variable or pass credentials into "authOptions.credentials".`
);
this.options = ensureAuthOptionScopes<WebGoogleAuthOptions>(
{ ...options, accessToken, credentials },
"scope",
fields?.platformType
);
}
get clientType(): string {
return "webauth";
}
async getProjectId(): Promise<string> {
const credentials = getCredentials(this.options.credentials);
return credentials.project_id;
}
async request(opts: GoogleAbstractedClientOps): Promise<unknown> {
let { accessToken } = this.options;
if (accessToken === undefined) {
accessToken = await getAccessToken(this.options);
}
const authHeader = {
Authorization: `Bearer ${accessToken}`,
};
return this._request(opts?.url, opts, authHeader);
}
}