UNPKG

7.45 kBTypeScriptView Raw
1import { EventEmitter } from 'events';
2import { Gaxios, GaxiosOptions, GaxiosPromise, GaxiosResponse } from 'gaxios';
3import { Transporter } from '../transporters';
4import { Credentials } from './credentials';
5import { GetAccessTokenResponse, Headers } from './oauth2client';
6import { OriginalAndCamel } from '../util';
7/**
8 * Base auth configurations (e.g. from JWT or `.json` files) with conventional
9 * camelCased options.
10 *
11 * @privateRemarks
12 *
13 * This interface is purposely not exported so that it can be removed once
14 * {@link https://github.com/microsoft/TypeScript/issues/50715} has been
15 * resolved. Then, we can use {@link OriginalAndCamel} to shrink this interface.
16 *
17 * Tracking: {@link https://github.com/googleapis/google-auth-library-nodejs/issues/1686}
18 */
19interface AuthJSONOptions {
20 /**
21 * The project ID corresponding to the current credentials if available.
22 */
23 project_id: string | null;
24 /**
25 * An alias for {@link AuthJSONOptions.project_id `project_id`}.
26 */
27 projectId: AuthJSONOptions['project_id'];
28 /**
29 * The quota project ID. The quota project can be used by client libraries for the billing purpose.
30 * See {@link https://cloud.google.com/docs/quota Working with quotas}
31 */
32 quota_project_id: string;
33 /**
34 * An alias for {@link AuthJSONOptions.quota_project_id `quota_project_id`}.
35 */
36 quotaProjectId: AuthJSONOptions['quota_project_id'];
37 /**
38 * The default service domain for a given Cloud universe.
39 *
40 * @example
41 * 'googleapis.com'
42 */
43 universe_domain: string;
44 /**
45 * An alias for {@link AuthJSONOptions.universe_domain `universe_domain`}.
46 */
47 universeDomain: AuthJSONOptions['universe_domain'];
48}
49/**
50 * Base `AuthClient` configuration.
51 *
52 * The camelCased options are aliases of the snake_cased options, supporting both
53 * JSON API and JS conventions.
54 */
55export interface AuthClientOptions extends Partial<OriginalAndCamel<AuthJSONOptions>> {
56 /**
57 * An API key to use, optional.
58 */
59 apiKey?: string;
60 credentials?: Credentials;
61 /**
62 * A `Gaxios` or `Transporter` instance to use for `AuthClient` requests.
63 */
64 transporter?: Gaxios | Transporter;
65 /**
66 * Provides default options to the transporter, such as {@link GaxiosOptions.agent `agent`} or
67 * {@link GaxiosOptions.retryConfig `retryConfig`}.
68 */
69 transporterOptions?: GaxiosOptions;
70 /**
71 * The expiration threshold in milliseconds before forcing token refresh of
72 * unexpired tokens.
73 */
74 eagerRefreshThresholdMillis?: number;
75 /**
76 * Whether to attempt to refresh tokens on status 401/403 responses
77 * even if an attempt is made to refresh the token preemptively based
78 * on the expiry_date.
79 */
80 forceRefreshOnFailure?: boolean;
81}
82/**
83 * The default cloud universe
84 *
85 * @see {@link AuthJSONOptions.universe_domain}
86 */
87export declare const DEFAULT_UNIVERSE = "googleapis.com";
88/**
89 * The default {@link AuthClientOptions.eagerRefreshThresholdMillis}
90 */
91export declare const DEFAULT_EAGER_REFRESH_THRESHOLD_MILLIS: number;
92/**
93 * Defines the root interface for all clients that generate credentials
94 * for calling Google APIs. All clients should implement this interface.
95 */
96export interface CredentialsClient {
97 projectId?: AuthClientOptions['projectId'];
98 eagerRefreshThresholdMillis: NonNullable<AuthClientOptions['eagerRefreshThresholdMillis']>;
99 forceRefreshOnFailure: NonNullable<AuthClientOptions['forceRefreshOnFailure']>;
100 /**
101 * @return A promise that resolves with the current GCP access token
102 * response. If the current credential is expired, a new one is retrieved.
103 */
104 getAccessToken(): Promise<GetAccessTokenResponse>;
105 /**
106 * The main authentication interface. It takes an optional url which when
107 * present is the endpoint being accessed, and returns a Promise which
108 * resolves with authorization header fields.
109 *
110 * The result has the form:
111 * { Authorization: 'Bearer <access_token_value>' }
112 * @param url The URI being authorized.
113 */
114 getRequestHeaders(url?: string): Promise<Headers>;
115 /**
116 * Provides an alternative Gaxios request implementation with auth credentials
117 */
118 request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
119 /**
120 * Sets the auth credentials.
121 */
122 setCredentials(credentials: Credentials): void;
123 /**
124 * Subscribes a listener to the tokens event triggered when a token is
125 * generated.
126 *
127 * @param event The tokens event to subscribe to.
128 * @param listener The listener that triggers on event trigger.
129 * @return The current client instance.
130 */
131 on(event: 'tokens', listener: (tokens: Credentials) => void): this;
132}
133export declare interface AuthClient {
134 on(event: 'tokens', listener: (tokens: Credentials) => void): this;
135}
136export declare abstract class AuthClient extends EventEmitter implements CredentialsClient {
137 apiKey?: string;
138 projectId?: string | null;
139 /**
140 * The quota project ID. The quota project can be used by client libraries for the billing purpose.
141 * See {@link https://cloud.google.com/docs/quota Working with quotas}
142 */
143 quotaProjectId?: string;
144 transporter: Transporter;
145 credentials: Credentials;
146 eagerRefreshThresholdMillis: number;
147 forceRefreshOnFailure: boolean;
148 universeDomain: string;
149 constructor(opts?: AuthClientOptions);
150 /**
151 * Return the {@link Gaxios `Gaxios`} instance from the {@link AuthClient.transporter}.
152 *
153 * @expiremental
154 */
155 get gaxios(): Gaxios | null;
156 /**
157 * Provides an alternative Gaxios request implementation with auth credentials
158 */
159 abstract request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
160 /**
161 * The main authentication interface. It takes an optional url which when
162 * present is the endpoint being accessed, and returns a Promise which
163 * resolves with authorization header fields.
164 *
165 * The result has the form:
166 * { Authorization: 'Bearer <access_token_value>' }
167 * @param url The URI being authorized.
168 */
169 abstract getRequestHeaders(url?: string): Promise<Headers>;
170 /**
171 * @return A promise that resolves with the current GCP access token
172 * response. If the current credential is expired, a new one is retrieved.
173 */
174 abstract getAccessToken(): Promise<{
175 token?: string | null;
176 res?: GaxiosResponse | null;
177 }>;
178 /**
179 * Sets the auth credentials.
180 */
181 setCredentials(credentials: Credentials): void;
182 /**
183 * Append additional headers, e.g., x-goog-user-project, shared across the
184 * classes inheriting AuthClient. This method should be used by any method
185 * that overrides getRequestMetadataAsync(), which is a shared helper for
186 * setting request information in both gRPC and HTTP API calls.
187 *
188 * @param headers object to append additional headers to.
189 */
190 protected addSharedMetadataHeaders(headers: Headers): Headers;
191 /**
192 * Retry config for Auth-related requests.
193 *
194 * @remarks
195 *
196 * This is not a part of the default {@link AuthClient.transporter transporter/gaxios}
197 * config as some downstream APIs would prefer if customers explicitly enable retries,
198 * such as GCS.
199 */
200 protected static get RETRY_CONFIG(): GaxiosOptions;
201}
202export {};