@thzero/library_client_firebase 0.17.6 → 0.17.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/service/index.js +149 -64
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thzero/library_client_firebase",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.7",
|
|
4
4
|
"version_major": 0,
|
|
5
5
|
"version_minor": 17,
|
|
6
|
-
"version_patch":
|
|
7
|
-
"version_date": "
|
|
6
|
+
"version_patch": 7,
|
|
7
|
+
"version_date": "04/19/2023",
|
|
8
8
|
"author": "thZero",
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"repository": {
|
package/service/index.js
CHANGED
|
@@ -86,6 +86,149 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
86
86
|
// }
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
async refreshToken(correlationId, user, forceRefresh) {
|
|
90
|
+
forceRefresh = forceRefresh !== null ? forceRefresh : false;
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'user', user, correlationId);
|
|
94
|
+
if (!user) {
|
|
95
|
+
await this._serviceUser.setTokenResult(correlationId, null);
|
|
96
|
+
await this._serviceUser.setClaims(correlationId, null);
|
|
97
|
+
this.announceToken(correlationId, user, null);
|
|
98
|
+
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'forceRefresh', forceRefresh, correlationId);
|
|
103
|
+
const currentUser = await getAuth().currentUser;
|
|
104
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'currentUser', currentUser, correlationId);
|
|
105
|
+
if (!currentUser)
|
|
106
|
+
return;
|
|
107
|
+
|
|
108
|
+
const tokenResult = await currentUser.getIdTokenResult(forceRefresh);
|
|
109
|
+
if (tokenResult) {
|
|
110
|
+
await this._serviceUser.setTokenResult(correlationId, tokenResult);
|
|
111
|
+
const token = tokenResult.token;
|
|
112
|
+
let claims = token != null ? tokenResult.claims : null;
|
|
113
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims', claims, correlationId);
|
|
114
|
+
claims = claims != null ? claims.custom : null;
|
|
115
|
+
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims.custom', claims, correlationId);
|
|
116
|
+
await this._serviceUser.setClaims(correlationId, claims);
|
|
117
|
+
|
|
118
|
+
this.announceToken(correlationId, user, token);
|
|
119
|
+
|
|
120
|
+
const expired = LibraryCommonUtility.getDateParse(tokenResult.expirationTime);
|
|
121
|
+
const now = LibraryCommonUtility.getDate();
|
|
122
|
+
const diff = expired.diff(now);
|
|
123
|
+
const min = 5 * 60 * 1000;
|
|
124
|
+
if (diff <= min) {
|
|
125
|
+
await this.refreshToken(correlationId, getAuth().currentUser, true).then();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (this._polling)
|
|
130
|
+
clearInterval(this._polling);
|
|
131
|
+
|
|
132
|
+
const self = this;
|
|
133
|
+
this._polling = setInterval(async () => {
|
|
134
|
+
await self.refreshToken(correlationId, self.user, true).then();
|
|
135
|
+
}, diff); // 60 * 1000)
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
await this._serviceUser.setTokenResult(correlationId, null);
|
|
139
|
+
await this._serviceUser.setClaims(correlationId, null);
|
|
140
|
+
|
|
141
|
+
this.announceToken(correlationId, user, token);
|
|
142
|
+
|
|
143
|
+
if (this._polling)
|
|
144
|
+
clearInterval(this._polling);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
this._logger.exception('FirebaseAuthService', 'refreshToken', err, correlationId);
|
|
149
|
+
throw err;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async resolveAuthorization(correlationId, requiresAuthRoles, requiresAuthLogical) {
|
|
154
|
+
// const serviceAuth = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_AUTH);
|
|
155
|
+
// const serviceLogger = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_LOGGER);
|
|
156
|
+
// const serviceSecurity = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_SECURITY);
|
|
157
|
+
// const serviceStore = LibraryClientUtility.$injector.getService(LibraryClientConstants.InjectorKeys.SERVICE_STORE);
|
|
158
|
+
this._serviceLogger.info2('requiresAuth');
|
|
159
|
+
let isLoggedIn = this.isAuthenticated;
|
|
160
|
+
this._serviceLogger.info2('authorization.isLoggedIn', isLoggedIn);
|
|
161
|
+
console.log('authorization.isLoggedIn', isLoggedIn);
|
|
162
|
+
if (!isLoggedIn) {
|
|
163
|
+
// Briefly wait for authentication to settle...
|
|
164
|
+
let i = 0;
|
|
165
|
+
while (await this.sleep(150)) {
|
|
166
|
+
if (this._serviceStore.userAuthCompleted) {
|
|
167
|
+
this._serviceLogger.info2('authorization.userAuthCompleted', userAuthCompleted);
|
|
168
|
+
console.log('authorization.userAuthCompleted', userAuthCompleted);
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
i++;
|
|
172
|
+
this._serviceLogger.info2('waiting... ' + i);
|
|
173
|
+
if (i > 5) {
|
|
174
|
+
this._serviceLogger.warn2('authorization.userAuthCompleted failed');
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const isLoggedInAuthCompleted = this.isAuthenticated;
|
|
179
|
+
this._serviceLogger.info2('authorization.isLoggedIn.userAuthCompleted', isLoggedInAuthCompleted);
|
|
180
|
+
console.log('authorization.isLoggedIn.userAuthCompleted', isLoggedInAuthCompleted);
|
|
181
|
+
isLoggedIn = isLoggedInAuthCompleted;
|
|
182
|
+
}
|
|
183
|
+
this._serviceLogger.info2('authorization.isLoggedIn.final', isLoggedIn);
|
|
184
|
+
console.log('authorization.isLoggedIn.final', isLoggedIn);
|
|
185
|
+
if (!isLoggedIn) {
|
|
186
|
+
this._serviceLogger.warn2('authorization.isLoggedIn - failed');
|
|
187
|
+
console.log('authorization.isLoggedIn - failed');
|
|
188
|
+
// LibraryClientUtility.$EventBus.on('auth-refresh', (user) => {
|
|
189
|
+
// this._serviceLogger.debug('auth-refresh', user)
|
|
190
|
+
// this._serviceLogger.debug('middleware', 'auth-refresh', null, user, correlationId);
|
|
191
|
+
// next()
|
|
192
|
+
// })
|
|
193
|
+
// return
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
this._serviceLogger.info2('authorization.isLoggedIn - success');
|
|
198
|
+
console.log('authorization.isLoggedIn - success');
|
|
199
|
+
|
|
200
|
+
const user = this._serviceStore.user;
|
|
201
|
+
let success = true;
|
|
202
|
+
this._serviceLogger.info2('authorization.requiresAuthRoles', requiresAuthRoles);
|
|
203
|
+
console.log('authorization.requiresAuthRoles', requiresAuthRoles);
|
|
204
|
+
this._serviceLogger.info2('authorization.requiresAuthLogical', requiresAuthLogical);
|
|
205
|
+
console.log('authorization.requiresAuthLogical', requiresAuthLogical);
|
|
206
|
+
|
|
207
|
+
if (requiresAuthRoles) {
|
|
208
|
+
success = await this._serviceSecurity.authorizationCheckRoles(correlationId, user, roles, record.meta.requiresAuthLogical);
|
|
209
|
+
this._serviceLogger.info2('authorization.roles.success', success);
|
|
210
|
+
console.log('authorization.roles.success', success);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
this._serviceLogger.debug('middleware', 'authorization', 'success', success, correlationId);
|
|
214
|
+
console.log('authorization.roles.success', success);
|
|
215
|
+
this._serviceLogger.info2('authorization.roles.success', success);
|
|
216
|
+
if (!success) {
|
|
217
|
+
this._serviceLogger.warn2('authorization.roles - failed');
|
|
218
|
+
console.log('authorization.roles - failed');
|
|
219
|
+
LibraryClientUtility.$navRouter.push('/', null, () => {
|
|
220
|
+
// LibraryClientUtility.$navRouter.push('/')
|
|
221
|
+
// window.location.href = '/'
|
|
222
|
+
});
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
this._serviceLogger.info2('authorization.roles - success');
|
|
227
|
+
console.log('authorization.roles - success');
|
|
228
|
+
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
|
|
89
232
|
async signIn(correlationId) {
|
|
90
233
|
if (this.isAuthenticated)
|
|
91
234
|
return false;
|
|
@@ -168,6 +311,12 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
168
311
|
}
|
|
169
312
|
}
|
|
170
313
|
|
|
314
|
+
sleep(ms) {
|
|
315
|
+
return new Promise((resolve) => {
|
|
316
|
+
setTimeout(resolve, ms);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
171
320
|
// async token(forceRefresh) {
|
|
172
321
|
// if (!forceRefresh)
|
|
173
322
|
// forceRefresh = false
|
|
@@ -181,70 +330,6 @@ class FirebaseAuthService extends UserAuthService {
|
|
|
181
330
|
// return this.refreshToken'FirebaseAuthService', 'token', user, forceRefresh)
|
|
182
331
|
// }
|
|
183
332
|
|
|
184
|
-
async refreshToken(correlationId, user, forceRefresh) {
|
|
185
|
-
forceRefresh = forceRefresh !== null ? forceRefresh : false;
|
|
186
|
-
|
|
187
|
-
try {
|
|
188
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'user', user, correlationId);
|
|
189
|
-
if (!user) {
|
|
190
|
-
await this._serviceUser.setTokenResult(correlationId, null);
|
|
191
|
-
await this._serviceUser.setClaims(correlationId, null);
|
|
192
|
-
this.announceToken(correlationId, user, null);
|
|
193
|
-
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'forceRefresh', forceRefresh, correlationId);
|
|
198
|
-
const currentUser = await getAuth().currentUser;
|
|
199
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'currentUser', currentUser, correlationId);
|
|
200
|
-
if (!currentUser)
|
|
201
|
-
return;
|
|
202
|
-
|
|
203
|
-
const tokenResult = await currentUser.getIdTokenResult(forceRefresh);
|
|
204
|
-
if (tokenResult) {
|
|
205
|
-
await this._serviceUser.setTokenResult(correlationId, tokenResult);
|
|
206
|
-
const token = tokenResult.token;
|
|
207
|
-
let claims = token != null ? tokenResult.claims : null;
|
|
208
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims', claims, correlationId);
|
|
209
|
-
claims = claims != null ? claims.custom : null;
|
|
210
|
-
this._logger.debug('FirebaseAuthService', 'refreshToken', 'claims.custom', claims, correlationId);
|
|
211
|
-
await this._serviceUser.setClaims(correlationId, claims);
|
|
212
|
-
|
|
213
|
-
this.announceToken(correlationId, user, token);
|
|
214
|
-
|
|
215
|
-
const expired = LibraryCommonUtility.getDateParse(tokenResult.expirationTime);
|
|
216
|
-
const now = LibraryCommonUtility.getDate();
|
|
217
|
-
const diff = expired.diff(now);
|
|
218
|
-
const min = 5 * 60 * 1000;
|
|
219
|
-
if (diff <= min) {
|
|
220
|
-
await this.refreshToken(correlationId, getAuth().currentUser, true).then();
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (this._polling)
|
|
225
|
-
clearInterval(this._polling);
|
|
226
|
-
|
|
227
|
-
const self = this;
|
|
228
|
-
this._polling = setInterval(async () => {
|
|
229
|
-
await self.refreshToken(correlationId, self.user, true).then();
|
|
230
|
-
}, diff); // 60 * 1000)
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
await this._serviceUser.setTokenResult(correlationId, null);
|
|
234
|
-
await this._serviceUser.setClaims(correlationId, null);
|
|
235
|
-
|
|
236
|
-
this.announceToken(correlationId, user, token);
|
|
237
|
-
|
|
238
|
-
if (this._polling)
|
|
239
|
-
clearInterval(this._polling);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
catch (err) {
|
|
243
|
-
this._logger.exception('FirebaseAuthService', 'refreshToken', err, correlationId);
|
|
244
|
-
throw err;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
333
|
async updateExternalUser(correlationId, user) {
|
|
249
334
|
// if (this._lock)
|
|
250
335
|
// return
|