1 | import { GaxiosOptions, GaxiosResponse } from 'gaxios';
|
2 | import * as stream from 'stream';
|
3 | import { DefaultTransporter, Transporter } from '../transporters';
|
4 | import { CredentialBody, ImpersonatedJWTInput, JWTInput } from './credentials';
|
5 | import { IdTokenClient } from './idtokenclient';
|
6 | import { GCPEnv } from './envDetect';
|
7 | import { JWT, JWTOptions } from './jwtclient';
|
8 | import { Headers, OAuth2ClientOptions } from './oauth2client';
|
9 | import { UserRefreshClient, UserRefreshClientOptions } from './refreshclient';
|
10 | import { Impersonated, ImpersonatedOptions } from './impersonated';
|
11 | import { ExternalAccountClientOptions } from './externalclient';
|
12 | import { BaseExternalAccountClient } from './baseexternalclient';
|
13 | import { AuthClient, AuthClientOptions } from './authclient';
|
14 | import { ExternalAccountAuthorizedUserClient } from './externalAccountAuthorizedUserClient';
|
15 | import { AnyAuthClient } from '..';
|
16 | /**
|
17 | * Defines all types of explicit clients that are determined via ADC JSON
|
18 | * config file.
|
19 | */
|
20 | export type JSONClient = JWT | UserRefreshClient | BaseExternalAccountClient | ExternalAccountAuthorizedUserClient | Impersonated;
|
21 | export interface ProjectIdCallback {
|
22 | (err?: Error | null, projectId?: string | null): void;
|
23 | }
|
24 | export interface CredentialCallback {
|
25 | (err: Error | null, result?: JSONClient): void;
|
26 | }
|
27 | export interface ADCCallback {
|
28 | (err: Error | null, credential?: AuthClient, projectId?: string | null): void;
|
29 | }
|
30 | export interface ADCResponse {
|
31 | credential: AuthClient;
|
32 | projectId: string | null;
|
33 | }
|
34 | export interface GoogleAuthOptions<T extends AuthClient = JSONClient> {
|
35 | /**
|
36 | * An API key to use, optional. Cannot be used with {@link GoogleAuthOptions.credentials `credentials`}.
|
37 | */
|
38 | apiKey?: string;
|
39 | /**
|
40 | * An `AuthClient` to use
|
41 | */
|
42 | authClient?: T;
|
43 | /**
|
44 | * Path to a .json, .pem, or .p12 key file
|
45 | */
|
46 | keyFilename?: string;
|
47 | /**
|
48 | * Path to a .json, .pem, or .p12 key file
|
49 | */
|
50 | keyFile?: string;
|
51 | /**
|
52 | * Object containing client_email and private_key properties, or the
|
53 | * external account client options.
|
54 | * Cannot be used with {@link GoogleAuthOptions.apiKey `apiKey`}.
|
55 | */
|
56 | credentials?: JWTInput | ExternalAccountClientOptions;
|
57 | /**
|
58 | * Options object passed to the constructor of the client
|
59 | */
|
60 | clientOptions?: JWTOptions | OAuth2ClientOptions | UserRefreshClientOptions | ImpersonatedOptions;
|
61 | /**
|
62 | * Required scopes for the desired API request
|
63 | */
|
64 | scopes?: string | string[];
|
65 | /**
|
66 | * Your project ID.
|
67 | */
|
68 | projectId?: string;
|
69 | /**
|
70 | * The default service domain for a given Cloud universe.
|
71 | *
|
72 | * This is an ergonomic equivalent to {@link clientOptions}'s `universeDomain`
|
73 | * property and will be set for all generated {@link AuthClient}s.
|
74 | */
|
75 | universeDomain?: string;
|
76 | }
|
77 | export declare const CLOUD_SDK_CLIENT_ID = "764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com";
|
78 | export declare const GoogleAuthExceptionMessages: {
|
79 | readonly API_KEY_WITH_CREDENTIALS: "API Keys and Credentials are mutually exclusive authentication methods and cannot be used together.";
|
80 | readonly NO_PROJECT_ID_FOUND: string;
|
81 | readonly NO_CREDENTIALS_FOUND: string;
|
82 | readonly NO_ADC_FOUND: "Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.";
|
83 | readonly NO_UNIVERSE_DOMAIN_FOUND: string;
|
84 | };
|
85 | export declare class GoogleAuth<T extends AuthClient = JSONClient> {
|
86 | #private;
|
87 | transporter?: Transporter;
|
88 | /**
|
89 | * Caches a value indicating whether the auth layer is running on Google
|
90 | * Compute Engine.
|
91 | * @private
|
92 | */
|
93 | private checkIsGCE?;
|
94 | useJWTAccessWithScope?: boolean;
|
95 | defaultServicePath?: string;
|
96 | get isGCE(): boolean | undefined;
|
97 | private _findProjectIdPromise?;
|
98 | private _cachedProjectId?;
|
99 | jsonContent: JWTInput | ExternalAccountClientOptions | null;
|
100 | apiKey: string | null;
|
101 | cachedCredential: AnyAuthClient | T | null;
|
102 | /**
|
103 | * Scopes populated by the client library by default. We differentiate between
|
104 | * these and user defined scopes when deciding whether to use a self-signed JWT.
|
105 | */
|
106 | defaultScopes?: string | string[];
|
107 | private keyFilename?;
|
108 | private scopes?;
|
109 | private clientOptions;
|
110 | /**
|
111 | * Export DefaultTransporter as a static property of the class.
|
112 | */
|
113 | static DefaultTransporter: typeof DefaultTransporter;
|
114 | /**
|
115 | * Configuration is resolved in the following order of precedence:
|
116 | * - {@link GoogleAuthOptions.credentials `credentials`}
|
117 | * - {@link GoogleAuthOptions.keyFilename `keyFilename`}
|
118 | * - {@link GoogleAuthOptions.keyFile `keyFile`}
|
119 | *
|
120 | * {@link GoogleAuthOptions.clientOptions `clientOptions`} are passed to the
|
121 | * {@link AuthClient `AuthClient`s}.
|
122 | *
|
123 | * @param opts
|
124 | */
|
125 | constructor(opts?: GoogleAuthOptions<T>);
|
126 | setGapicJWTValues(client: JWT): void;
|
127 | /**
|
128 | * Obtains the default project ID for the application.
|
129 | *
|
130 | * Retrieves in the following order of precedence:
|
131 | * - The `projectId` provided in this object's construction
|
132 | * - GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variable
|
133 | * - GOOGLE_APPLICATION_CREDENTIALS JSON file
|
134 | * - Cloud SDK: `gcloud config config-helper --format json`
|
135 | * - GCE project ID from metadata server
|
136 | */
|
137 | getProjectId(): Promise<string>;
|
138 | getProjectId(callback: ProjectIdCallback): void;
|
139 | /**
|
140 | * A temporary method for internal `getProjectId` usages where `null` is
|
141 | * acceptable. In a future major release, `getProjectId` should return `null`
|
142 | * (as the `Promise<string | null>` base signature describes) and this private
|
143 | * method should be removed.
|
144 | *
|
145 | * @returns Promise that resolves with project id (or `null`)
|
146 | */
|
147 | private getProjectIdOptional;
|
148 | /**
|
149 | * A private method for finding and caching a projectId.
|
150 | *
|
151 | * Supports environments in order of precedence:
|
152 | * - GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variable
|
153 | * - GOOGLE_APPLICATION_CREDENTIALS JSON file
|
154 | * - Cloud SDK: `gcloud config config-helper --format json`
|
155 | * - GCE project ID from metadata server
|
156 | *
|
157 | * @returns projectId
|
158 | */
|
159 | private findAndCacheProjectId;
|
160 | private getProjectIdAsync;
|
161 | /**
|
162 | * Retrieves a universe domain from the metadata server via
|
163 | * { gcpMetadata.universe}.
|
164 | *
|
165 | * a universe domain
|
166 | */
|
167 | getUniverseDomainFromMetadataServer(): Promise<string>;
|
168 | /**
|
169 | * Retrieves, caches, and returns the universe domain in the following order
|
170 | * of precedence:
|
171 | * - The universe domain in {@link GoogleAuth.clientOptions}
|
172 | * - An existing or ADC {@link AuthClient}'s universe domain
|
173 | * - {@link gcpMetadata.universe}, if {@link Compute} client
|
174 | *
|
175 | * @returns The universe domain
|
176 | */
|
177 | getUniverseDomain(): Promise<string>;
|
178 | /**
|
179 | * @returns Any scopes (user-specified or default scopes specified by the
|
180 | * client library) that need to be set on the current Auth client.
|
181 | */
|
182 | private getAnyScopes;
|
183 | /**
|
184 | * Obtains the default service-level credentials for the application.
|
185 | * @param callback Optional callback.
|
186 | * @returns Promise that resolves with the ADCResponse (if no callback was
|
187 | * passed).
|
188 | */
|
189 | getApplicationDefault(): Promise<ADCResponse>;
|
190 | getApplicationDefault(callback: ADCCallback): void;
|
191 | getApplicationDefault(options: AuthClientOptions): Promise<ADCResponse>;
|
192 | getApplicationDefault(options: AuthClientOptions, callback: ADCCallback): void;
|
193 | private getApplicationDefaultAsync;
|
194 | /**
|
195 | * Determines whether the auth layer is running on Google Compute Engine.
|
196 | * Checks for GCP Residency, then fallback to checking if metadata server
|
197 | * is available.
|
198 | *
|
199 | * @returns A promise that resolves with the boolean.
|
200 | * @api private
|
201 | */
|
202 | _checkIsGCE(): Promise<boolean>;
|
203 | /**
|
204 | * Attempts to load default credentials from the environment variable path..
|
205 | * @returns Promise that resolves with the OAuth2Client or null.
|
206 | * @api private
|
207 | */
|
208 | _tryGetApplicationCredentialsFromEnvironmentVariable(options?: AuthClientOptions): Promise<JSONClient | null>;
|
209 | /**
|
210 | * Attempts to load default credentials from a well-known file location
|
211 | * @return Promise that resolves with the OAuth2Client or null.
|
212 | * @api private
|
213 | */
|
214 | _tryGetApplicationCredentialsFromWellKnownFile(options?: AuthClientOptions): Promise<JSONClient | null>;
|
215 | /**
|
216 | * Attempts to load default credentials from a file at the given path..
|
217 | * @param filePath The path to the file to read.
|
218 | * @returns Promise that resolves with the OAuth2Client
|
219 | * @api private
|
220 | */
|
221 | _getApplicationCredentialsFromFilePath(filePath: string, options?: AuthClientOptions): Promise<JSONClient>;
|
222 | /**
|
223 | * Create a credentials instance using a given impersonated input options.
|
224 | * @param json The impersonated input object.
|
225 | * @returns JWT or UserRefresh Client with data
|
226 | */
|
227 | fromImpersonatedJSON(json: ImpersonatedJWTInput): Impersonated;
|
228 | /**
|
229 | * Create a credentials instance using the given input options.
|
230 | * This client is not cached.
|
231 | *
|
232 | * @param json The input object.
|
233 | * @param options The JWT or UserRefresh options for the client
|
234 | * @returns JWT or UserRefresh Client with data
|
235 | */
|
236 | fromJSON(json: JWTInput | ImpersonatedJWTInput, options?: AuthClientOptions): JSONClient;
|
237 | /**
|
238 | * Return a JWT or UserRefreshClient from JavaScript object, caching both the
|
239 | * object used to instantiate and the client.
|
240 | * @param json The input object.
|
241 | * @param options The JWT or UserRefresh options for the client
|
242 | * @returns JWT or UserRefresh Client with data
|
243 | */
|
244 | private _cacheClientFromJSON;
|
245 | /**
|
246 | * Create a credentials instance using the given input stream.
|
247 | * @param inputStream The input stream.
|
248 | * @param callback Optional callback.
|
249 | */
|
250 | fromStream(inputStream: stream.Readable): Promise<JSONClient>;
|
251 | fromStream(inputStream: stream.Readable, callback: CredentialCallback): void;
|
252 | fromStream(inputStream: stream.Readable, options: AuthClientOptions): Promise<JSONClient>;
|
253 | fromStream(inputStream: stream.Readable, options: AuthClientOptions, callback: CredentialCallback): void;
|
254 | private fromStreamAsync;
|
255 | /**
|
256 | * Create a credentials instance using the given API key string.
|
257 | * The created client is not cached. In order to create and cache it use the {@link GoogleAuth.getClient `getClient`} method after first providing an {@link GoogleAuth.apiKey `apiKey`}.
|
258 | *
|
259 | * @param apiKey The API key string
|
260 | * @param options An optional options object.
|
261 | * @returns A JWT loaded from the key
|
262 | */
|
263 | fromAPIKey(apiKey: string, options?: AuthClientOptions): JWT;
|
264 | /**
|
265 | * Determines whether the current operating system is Windows.
|
266 | * @api private
|
267 | */
|
268 | private _isWindows;
|
269 | /**
|
270 | * Run the Google Cloud SDK command that prints the default project ID
|
271 | */
|
272 | private getDefaultServiceProjectId;
|
273 | /**
|
274 | * Loads the project id from environment variables.
|
275 | * @api private
|
276 | */
|
277 | private getProductionProjectId;
|
278 | /**
|
279 | * Loads the project id from the GOOGLE_APPLICATION_CREDENTIALS json file.
|
280 | * @api private
|
281 | */
|
282 | private getFileProjectId;
|
283 | /**
|
284 | * Gets the project ID from external account client if available.
|
285 | */
|
286 | private getExternalAccountClientProjectId;
|
287 | /**
|
288 | * Gets the Compute Engine project ID if it can be inferred.
|
289 | */
|
290 | private getGCEProjectId;
|
291 | /**
|
292 | * The callback function handles a credential object that contains the
|
293 | * client_email and private_key (if exists).
|
294 | * getCredentials first checks if the client is using an external account and
|
295 | * uses the service account email in place of client_email.
|
296 | * If that doesn't exist, it checks for these values from the user JSON.
|
297 | * If the user JSON doesn't exist, and the environment is on GCE, it gets the
|
298 | * client_email from the cloud metadata server.
|
299 | * @param callback Callback that handles the credential object that contains
|
300 | * a client_email and optional private key, or the error.
|
301 | * returned
|
302 | */
|
303 | getCredentials(): Promise<CredentialBody>;
|
304 | getCredentials(callback: (err: Error | null, credentials?: CredentialBody) => void): void;
|
305 | private getCredentialsAsync;
|
306 | /**
|
307 | * Automatically obtain an {@link AuthClient `AuthClient`} based on the
|
308 | * provided configuration. If no options were passed, use Application
|
309 | * Default Credentials.
|
310 | */
|
311 | getClient(): Promise<AnyAuthClient | T>;
|
312 | /**
|
313 | * Creates a client which will fetch an ID token for authorization.
|
314 | * @param targetAudience the audience for the fetched ID token.
|
315 | * @returns IdTokenClient for making HTTP calls authenticated with ID tokens.
|
316 | */
|
317 | getIdTokenClient(targetAudience: string): Promise<IdTokenClient>;
|
318 | /**
|
319 | * Automatically obtain application default credentials, and return
|
320 | * an access token for making requests.
|
321 | */
|
322 | getAccessToken(): Promise<string | null | undefined>;
|
323 | /**
|
324 | * Obtain the HTTP headers that will provide authorization for a given
|
325 | * request.
|
326 | */
|
327 | getRequestHeaders(url?: string): Promise<Headers>;
|
328 | /**
|
329 | * Obtain credentials for a request, then attach the appropriate headers to
|
330 | * the request options.
|
331 | * @param opts Axios or Request options on which to attach the headers
|
332 | */
|
333 | authorizeRequest(opts: {
|
334 | url?: string;
|
335 | uri?: string;
|
336 | headers?: Headers;
|
337 | }): Promise<{
|
338 | url?: string;
|
339 | uri?: string;
|
340 | headers?: Headers;
|
341 | }>;
|
342 | /**
|
343 | * Automatically obtain application default credentials, and make an
|
344 | * HTTP request using the given options.
|
345 | * @param opts Axios request options for the HTTP request.
|
346 | */
|
347 | request<T = any>(opts: GaxiosOptions): Promise<GaxiosResponse<T>>;
|
348 | /**
|
349 | * Determine the compute environment in which the code is running.
|
350 | */
|
351 | getEnv(): Promise<GCPEnv>;
|
352 | /**
|
353 | * Sign the given data with the current private key, or go out
|
354 | * to the IAM API to sign it.
|
355 | * @param data The data to be signed.
|
356 | * @param endpoint A custom endpoint to use.
|
357 | *
|
358 | * @example
|
359 | * ```
|
360 | * sign('data', 'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/');
|
361 | * ```
|
362 | */
|
363 | sign(data: string, endpoint?: string): Promise<string>;
|
364 | private signBlob;
|
365 | }
|
366 | export interface SignBlobResponse {
|
367 | keyId: string;
|
368 | signedBlob: string;
|
369 | }
|