UNPKG

4.7 kBJavaScriptView Raw
1"use strict";
2// Copyright 2012 Google LLC
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.AuthClient = exports.DEFAULT_EAGER_REFRESH_THRESHOLD_MILLIS = exports.DEFAULT_UNIVERSE = void 0;
17const events_1 = require("events");
18const gaxios_1 = require("gaxios");
19const transporters_1 = require("../transporters");
20const util_1 = require("../util");
21/**
22 * The default cloud universe
23 *
24 * @see {@link AuthJSONOptions.universe_domain}
25 */
26exports.DEFAULT_UNIVERSE = 'googleapis.com';
27/**
28 * The default {@link AuthClientOptions.eagerRefreshThresholdMillis}
29 */
30exports.DEFAULT_EAGER_REFRESH_THRESHOLD_MILLIS = 5 * 60 * 1000;
31class AuthClient extends events_1.EventEmitter {
32 constructor(opts = {}) {
33 var _a, _b, _c, _d, _e;
34 super();
35 this.credentials = {};
36 this.eagerRefreshThresholdMillis = exports.DEFAULT_EAGER_REFRESH_THRESHOLD_MILLIS;
37 this.forceRefreshOnFailure = false;
38 this.universeDomain = exports.DEFAULT_UNIVERSE;
39 const options = (0, util_1.originalOrCamelOptions)(opts);
40 // Shared auth options
41 this.projectId = (_a = options.get('project_id')) !== null && _a !== void 0 ? _a : null;
42 this.quotaProjectId = options.get('quota_project_id');
43 this.credentials = (_b = options.get('credentials')) !== null && _b !== void 0 ? _b : {};
44 this.universeDomain = (_c = options.get('universe_domain')) !== null && _c !== void 0 ? _c : exports.DEFAULT_UNIVERSE;
45 // Shared client options
46 this.transporter = (_d = opts.transporter) !== null && _d !== void 0 ? _d : new transporters_1.DefaultTransporter();
47 if (opts.transporterOptions) {
48 this.transporter.defaults = opts.transporterOptions;
49 }
50 if (opts.eagerRefreshThresholdMillis) {
51 this.eagerRefreshThresholdMillis = opts.eagerRefreshThresholdMillis;
52 }
53 this.forceRefreshOnFailure = (_e = opts.forceRefreshOnFailure) !== null && _e !== void 0 ? _e : false;
54 }
55 /**
56 * Return the {@link Gaxios `Gaxios`} instance from the {@link AuthClient.transporter}.
57 *
58 * @expiremental
59 */
60 get gaxios() {
61 if (this.transporter instanceof gaxios_1.Gaxios) {
62 return this.transporter;
63 }
64 else if (this.transporter instanceof transporters_1.DefaultTransporter) {
65 return this.transporter.instance;
66 }
67 else if ('instance' in this.transporter &&
68 this.transporter.instance instanceof gaxios_1.Gaxios) {
69 return this.transporter.instance;
70 }
71 return null;
72 }
73 /**
74 * Sets the auth credentials.
75 */
76 setCredentials(credentials) {
77 this.credentials = credentials;
78 }
79 /**
80 * Append additional headers, e.g., x-goog-user-project, shared across the
81 * classes inheriting AuthClient. This method should be used by any method
82 * that overrides getRequestMetadataAsync(), which is a shared helper for
83 * setting request information in both gRPC and HTTP API calls.
84 *
85 * @param headers object to append additional headers to.
86 */
87 addSharedMetadataHeaders(headers) {
88 // quota_project_id, stored in application_default_credentials.json, is set in
89 // the x-goog-user-project header, to indicate an alternate account for
90 // billing and quota:
91 if (!headers['x-goog-user-project'] && // don't override a value the user sets.
92 this.quotaProjectId) {
93 headers['x-goog-user-project'] = this.quotaProjectId;
94 }
95 return headers;
96 }
97 /**
98 * Retry config for Auth-related requests.
99 *
100 * @remarks
101 *
102 * This is not a part of the default {@link AuthClient.transporter transporter/gaxios}
103 * config as some downstream APIs would prefer if customers explicitly enable retries,
104 * such as GCS.
105 */
106 static get RETRY_CONFIG() {
107 return {
108 retry: true,
109 retryConfig: {
110 httpMethodsToRetry: ['GET', 'PUT', 'POST', 'HEAD', 'OPTIONS', 'DELETE'],
111 },
112 };
113 }
114}
115exports.AuthClient = AuthClient;