1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | exports.FirebaseApp = exports.FirebaseAppInternals = void 0;
|
21 | const credential_internal_1 = require("./credential-internal");
|
22 | const validator = require("../utils/validator");
|
23 | const deep_copy_1 = require("../utils/deep-copy");
|
24 | const error_1 = require("../utils/error");
|
25 | const TOKEN_EXPIRY_THRESHOLD_MILLIS = 5 * 60 * 1000;
|
26 |
|
27 |
|
28 |
|
29 | class FirebaseAppInternals {
|
30 |
|
31 | constructor(credential_) {
|
32 | this.credential_ = credential_;
|
33 | this.tokenListeners_ = [];
|
34 | }
|
35 | getToken(forceRefresh = false) {
|
36 | if (forceRefresh || this.shouldRefresh()) {
|
37 | return this.refreshToken();
|
38 | }
|
39 | return Promise.resolve(this.cachedToken_);
|
40 | }
|
41 | getCachedToken() {
|
42 | return this.cachedToken_ || null;
|
43 | }
|
44 | refreshToken() {
|
45 | return Promise.resolve(this.credential_.getAccessToken())
|
46 | .then((result) => {
|
47 |
|
48 |
|
49 | if (!validator.isNonNullObject(result) ||
|
50 | typeof result.expires_in !== 'number' ||
|
51 | typeof result.access_token !== 'string') {
|
52 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, `Invalid access token generated: "${JSON.stringify(result)}". Valid access ` +
|
53 | 'tokens must be an object with the "expires_in" (number) and "access_token" ' +
|
54 | '(string) properties.');
|
55 | }
|
56 | const token = {
|
57 | accessToken: result.access_token,
|
58 | expirationTime: Date.now() + (result.expires_in * 1000),
|
59 | };
|
60 | if (!this.cachedToken_
|
61 | || this.cachedToken_.accessToken !== token.accessToken
|
62 | || this.cachedToken_.expirationTime !== token.expirationTime) {
|
63 |
|
64 |
|
65 | this.cachedToken_ = token;
|
66 | this.tokenListeners_.forEach((listener) => {
|
67 | listener(token.accessToken);
|
68 | });
|
69 | }
|
70 | return token;
|
71 | })
|
72 | .catch((error) => {
|
73 | let errorMessage = (typeof error === 'string') ? error : error.message;
|
74 | errorMessage = 'Credential implementation provided to initializeApp() via the ' +
|
75 | '"credential" property failed to fetch a valid Google OAuth2 access token with the ' +
|
76 | `following error: "${errorMessage}".`;
|
77 | if (errorMessage.indexOf('invalid_grant') !== -1) {
|
78 | errorMessage += ' There are two likely causes: (1) your server time is not properly ' +
|
79 | 'synced or (2) your certificate key file has been revoked. To solve (1), re-sync the ' +
|
80 | 'time on your server. To solve (2), make sure the key ID for your key file is still ' +
|
81 | 'present at https://console.firebase.google.com/iam-admin/serviceaccounts/project. If ' +
|
82 | 'not, generate a new key file at ' +
|
83 | 'https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk.';
|
84 | }
|
85 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
|
86 | });
|
87 | }
|
88 | shouldRefresh() {
|
89 | return !this.cachedToken_ || (this.cachedToken_.expirationTime - Date.now()) <= TOKEN_EXPIRY_THRESHOLD_MILLIS;
|
90 | }
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 | addAuthTokenListener(listener) {
|
97 | this.tokenListeners_.push(listener);
|
98 | if (this.cachedToken_) {
|
99 | listener(this.cachedToken_.accessToken);
|
100 | }
|
101 | }
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 | removeAuthTokenListener(listener) {
|
108 | this.tokenListeners_ = this.tokenListeners_.filter((other) => other !== listener);
|
109 | }
|
110 | }
|
111 | exports.FirebaseAppInternals = FirebaseAppInternals;
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | class FirebaseApp {
|
118 | constructor(options, name, appStore) {
|
119 | this.appStore = appStore;
|
120 | this.services_ = {};
|
121 | this.isDeleted_ = false;
|
122 | this.name_ = name;
|
123 | this.options_ = (0, deep_copy_1.deepCopy)(options);
|
124 | if (!validator.isNonNullObject(this.options_)) {
|
125 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_OPTIONS, 'Invalid Firebase app options passed as the first argument to initializeApp() for the ' +
|
126 | `app named "${this.name_}". Options must be a non-null object.`);
|
127 | }
|
128 | const hasCredential = ('credential' in this.options_);
|
129 | if (!hasCredential) {
|
130 | this.options_.credential = (0, credential_internal_1.getApplicationDefault)(this.options_.httpAgent);
|
131 | }
|
132 | const credential = this.options_.credential;
|
133 | if (typeof credential !== 'object' || credential === null || typeof credential.getAccessToken !== 'function') {
|
134 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_OPTIONS, 'Invalid Firebase app options passed as the first argument to initializeApp() for the ' +
|
135 | `app named "${this.name_}". The "credential" property must be an object which implements ` +
|
136 | 'the Credential interface.');
|
137 | }
|
138 | this.INTERNAL = new FirebaseAppInternals(credential);
|
139 | }
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 | get name() {
|
146 | this.checkDestroyed_();
|
147 | return this.name_;
|
148 | }
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 | get options() {
|
155 | this.checkDestroyed_();
|
156 | return (0, deep_copy_1.deepCopy)(this.options_);
|
157 | }
|
158 | |
159 |
|
160 |
|
161 | getOrInitService(name, init) {
|
162 | return this.ensureService_(name, () => init(this));
|
163 | }
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 | delete() {
|
170 | this.checkDestroyed_();
|
171 |
|
172 |
|
173 |
|
174 | this.appStore?.removeApp(this.name);
|
175 | return Promise.all(Object.keys(this.services_).map((serviceName) => {
|
176 | const service = this.services_[serviceName];
|
177 | if (isStateful(service)) {
|
178 | return service.delete();
|
179 | }
|
180 | return Promise.resolve();
|
181 | })).then(() => {
|
182 | this.services_ = {};
|
183 | this.isDeleted_ = true;
|
184 | });
|
185 | }
|
186 |
|
187 | ensureService_(serviceName, initializer) {
|
188 | this.checkDestroyed_();
|
189 | if (!(serviceName in this.services_)) {
|
190 | this.services_[serviceName] = initializer();
|
191 | }
|
192 | return this.services_[serviceName];
|
193 | }
|
194 | |
195 |
|
196 |
|
197 |
|
198 | checkDestroyed_() {
|
199 | if (this.isDeleted_) {
|
200 | throw new error_1.FirebaseAppError(error_1.AppErrorCodes.APP_DELETED, `Firebase app named "${this.name_}" has already been deleted.`);
|
201 | }
|
202 | }
|
203 | }
|
204 | exports.FirebaseApp = FirebaseApp;
|
205 | function isStateful(service) {
|
206 | return typeof service.delete === 'function';
|
207 | }
|