|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | import {EventEmitter} from 'events';
|
16 |
| -import {GaxiosOptions, GaxiosPromise, GaxiosResponse} from 'gaxios'; |
| 16 | +import {Gaxios, GaxiosOptions, GaxiosPromise, GaxiosResponse} from 'gaxios'; |
17 | 17 |
|
18 | 18 | import {DefaultTransporter, Transporter} from '../transporters';
|
19 | 19 | import {Credentials} from './credentials';
|
20 | 20 | import {Headers} from './oauth2client';
|
| 21 | +import {OriginalAndCamel, originalOrCamelOptions} from '../util'; |
21 | 22 |
|
22 | 23 | /**
|
23 |
| - * Defines the root interface for all clients that generate credentials |
24 |
| - * for calling Google APIs. All clients should implement this interface. |
| 24 | + * Base auth configurations (e.g. from JWT or `.json` files) with conventional |
| 25 | + * camelCased options. |
| 26 | + * |
| 27 | + * @privateRemarks |
| 28 | + * |
| 29 | + * This interface is purposely not exported so that it can be removed once |
| 30 | + * {@link https://github.com/microsoft/TypeScript/issues/50715} has been |
| 31 | + * resolved. Then, we can use {@link OriginalAndCamel} to shrink this interface. |
| 32 | + * |
| 33 | + * Tracking: {@link https://github.com/googleapis/google-auth-library-nodejs/issues/1686} |
25 | 34 | */
|
26 |
| -export interface CredentialsClient { |
| 35 | +interface AuthJSONOptions { |
27 | 36 | /**
|
28 | 37 | * The project ID corresponding to the current credentials if available.
|
29 | 38 | */
|
30 |
| - projectId?: string | null; |
| 39 | + project_id: string | null; |
| 40 | + /** |
| 41 | + * An alias for {@link AuthJSONOptions.project_id `project_id`}. |
| 42 | + */ |
| 43 | + projectId: AuthJSONOptions['project_id']; |
| 44 | + |
| 45 | + /** |
| 46 | + * The quota project ID. The quota project can be used by client libraries for the billing purpose. |
| 47 | + * See {@link https://cloud.google.com/docs/quota Working with quotas} |
| 48 | + */ |
| 49 | + quota_project_id: string; |
| 50 | + |
| 51 | + /** |
| 52 | + * An alias for {@link AuthJSONOptions.quota_project_id `quota_project_id`}. |
| 53 | + */ |
| 54 | + quotaProjectId: AuthJSONOptions['quota_project_id']; |
| 55 | + |
| 56 | + /** |
| 57 | + * The default service domain for a given Cloud universe. |
| 58 | + */ |
| 59 | + universe_domain: string; |
| 60 | + |
| 61 | + /** |
| 62 | + * An alias for {@link AuthJSONOptions.universe_domain `universe_domain`}. |
| 63 | + */ |
| 64 | + universeDomain: AuthJSONOptions['universe_domain']; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Base `AuthClient` configuration. |
| 69 | + * |
| 70 | + * The camelCased options are aliases of the snake_cased options, supporting both |
| 71 | + * JSON API and JS conventions. |
| 72 | + */ |
| 73 | +export interface AuthClientOptions |
| 74 | + extends Partial<OriginalAndCamel<AuthJSONOptions>> { |
| 75 | + credentials?: Credentials; |
| 76 | + |
| 77 | + /** |
| 78 | + * A `Gaxios` or `Transporter` instance to use for `AuthClient` requests. |
| 79 | + */ |
| 80 | + transporter?: Gaxios | Transporter; |
| 81 | + |
| 82 | + /** |
| 83 | + * Provides default options to the transporter, such as {@link GaxiosOptions.agent `agent`} or |
| 84 | + * {@link GaxiosOptions.retryConfig `retryConfig`}. |
| 85 | + */ |
| 86 | + transporterOptions?: GaxiosOptions; |
31 | 87 |
|
32 | 88 | /**
|
33 |
| - * The expiration threshold in milliseconds before forcing token refresh. |
| 89 | + * The expiration threshold in milliseconds before forcing token refresh of |
| 90 | + * unexpired tokens. |
34 | 91 | */
|
35 |
| - eagerRefreshThresholdMillis: number; |
| 92 | + eagerRefreshThresholdMillis?: number; |
36 | 93 |
|
37 | 94 | /**
|
38 |
| - * Whether to force refresh on failure when making an authorization request. |
| 95 | + * Whether to attempt to refresh tokens on status 401/403 responses |
| 96 | + * even if an attempt is made to refresh the token preemptively based |
| 97 | + * on the expiry_date. |
39 | 98 | */
|
40 |
| - forceRefreshOnFailure: boolean; |
| 99 | + forceRefreshOnFailure?: boolean; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * The default cloud universe |
| 104 | + * |
| 105 | + * @see {@link AuthJSONOptions.universe_domain} |
| 106 | + */ |
| 107 | +export const DEFAULT_UNIVERSE = 'googleapis.com'; |
| 108 | + |
| 109 | +/** |
| 110 | + * The default {@link AuthClientOptions.eagerRefreshThresholdMillis} |
| 111 | + */ |
| 112 | +export const DEFAULT_EAGER_REFRESH_THRESHOLD_MILLIS = 5 * 60 * 1000; |
| 113 | + |
| 114 | +/** |
| 115 | + * Defines the root interface for all clients that generate credentials |
| 116 | + * for calling Google APIs. All clients should implement this interface. |
| 117 | + */ |
| 118 | +export interface CredentialsClient { |
| 119 | + projectId?: AuthClientOptions['projectId']; |
| 120 | + eagerRefreshThresholdMillis: NonNullable< |
| 121 | + AuthClientOptions['eagerRefreshThresholdMillis'] |
| 122 | + >; |
| 123 | + forceRefreshOnFailure: NonNullable< |
| 124 | + AuthClientOptions['forceRefreshOnFailure'] |
| 125 | + >; |
41 | 126 |
|
42 | 127 | /**
|
43 | 128 | * @return A promise that resolves with the current GCP access token
|
@@ -88,16 +173,42 @@ export abstract class AuthClient
|
88 | 173 | extends EventEmitter
|
89 | 174 | implements CredentialsClient
|
90 | 175 | {
|
| 176 | + projectId?: string | null; |
91 | 177 | /**
|
92 | 178 | * The quota project ID. The quota project can be used by client libraries for the billing purpose.
|
93 |
| - * See {@link https://cloud.google.com/docs/quota| Working with quotas} |
| 179 | + * See {@link https://cloud.google.com/docs/quota Working with quotas} |
94 | 180 | */
|
95 | 181 | quotaProjectId?: string;
|
96 |
| - transporter: Transporter = new DefaultTransporter(); |
| 182 | + transporter: Transporter; |
97 | 183 | credentials: Credentials = {};
|
98 |
| - projectId?: string | null; |
99 |
| - eagerRefreshThresholdMillis = 5 * 60 * 1000; |
| 184 | + eagerRefreshThresholdMillis = DEFAULT_EAGER_REFRESH_THRESHOLD_MILLIS; |
100 | 185 | forceRefreshOnFailure = false;
|
| 186 | + universeDomain = DEFAULT_UNIVERSE; |
| 187 | + |
| 188 | + constructor(opts: AuthClientOptions = {}) { |
| 189 | + super(); |
| 190 | + |
| 191 | + const options = originalOrCamelOptions(opts); |
| 192 | + |
| 193 | + // Shared auth options |
| 194 | + this.projectId = options.get('project_id') ?? null; |
| 195 | + this.quotaProjectId = options.get('quota_project_id'); |
| 196 | + this.credentials = options.get('credentials') ?? {}; |
| 197 | + this.universeDomain = options.get('universe_domain') ?? DEFAULT_UNIVERSE; |
| 198 | + |
| 199 | + // Shared client options |
| 200 | + this.transporter = opts.transporter ?? new DefaultTransporter(); |
| 201 | + |
| 202 | + if (opts.transporterOptions) { |
| 203 | + this.transporter.defaults = opts.transporterOptions; |
| 204 | + } |
| 205 | + |
| 206 | + if (opts.eagerRefreshThresholdMillis) { |
| 207 | + this.eagerRefreshThresholdMillis = opts.eagerRefreshThresholdMillis; |
| 208 | + } |
| 209 | + |
| 210 | + this.forceRefreshOnFailure = opts.forceRefreshOnFailure ?? false; |
| 211 | + } |
101 | 212 |
|
102 | 213 | /**
|
103 | 214 | * Provides an alternative Gaxios request implementation with auth credentials
|
|
0 commit comments