@edsis/angular 21.3.12 → 21.3.15
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/fesm2022/edsis-angular-block-shell.mjs +32 -27
- package/fesm2022/edsis-angular-block-shell.mjs.map +1 -1
- package/fesm2022/edsis-angular-module-auth.mjs +294 -35
- package/fesm2022/edsis-angular-module-auth.mjs.map +1 -1
- package/package.json +2 -2
- package/types/edsis-angular-block-shell.d.ts +4 -1
- package/types/edsis-angular-module-auth.d.ts +14 -6
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { InjectionToken, inject, makeEnvironmentProviders, ChangeDetectionStrategy, Component, DestroyRef, signal, computed, input, effect } from '@angular/core';
|
|
3
|
-
import { HttpContextToken, HttpErrorResponse, provideHttpClient, withFetch, withInterceptors } from '@angular/common/http';
|
|
4
|
-
import { ApiConfiguration, provideApiConfiguration } from '@edsis/sdk/shared';
|
|
2
|
+
import { InjectionToken, inject, makeEnvironmentProviders, provideEnvironmentInitializer, ChangeDetectionStrategy, Component, DestroyRef, signal, computed, input, effect } from '@angular/core';
|
|
5
3
|
import { DOCUMENT, Location } from '@angular/common';
|
|
6
|
-
import {
|
|
4
|
+
import { HttpErrorResponse, HttpContextToken, provideHttpClient, withFetch, withInterceptors, HttpContext } from '@angular/common/http';
|
|
5
|
+
import { Router, RouterOutlet, RouterLink, ActivatedRoute } from '@angular/router';
|
|
6
|
+
import { AuthSessionService, AuthMagicLinkService } from '@edsis/sdk/auth';
|
|
7
|
+
import { ApiConfiguration, provideApiConfiguration } from '@edsis/sdk/shared';
|
|
8
|
+
import { UserProfileService } from '@edsis/sdk/user';
|
|
7
9
|
import { firstValueFrom, throwError, from, startWith, finalize } from 'rxjs';
|
|
8
10
|
import { switchMap, catchError } from 'rxjs/operators';
|
|
9
|
-
import { Router, RouterOutlet, RouterLink, ActivatedRoute } from '@angular/router';
|
|
10
11
|
import { UiLayoutComponent, UiLayoutFluidComponent } from '@edsis/ui/layout';
|
|
11
12
|
import { toSignal, takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
12
13
|
import * as i1 from '@angular/forms';
|
|
@@ -54,22 +55,136 @@ function normalizePath(value, fallback) {
|
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
const AUTH_SESSION_STORAGE_KEYS = ['access_token', 'user_id', 'role', 'name'];
|
|
58
|
+
const AUTH_SESSION_METADATA_STORAGE_KEYS = ['access_token_trusted'];
|
|
59
|
+
const AUTH_SESSION_PERSISTENT_HINT_KEY = 'edsis_auth_refresh_hint';
|
|
57
60
|
const AUTH_SESSION_CHANGE_EVENT = 'edsis-auth-session-change';
|
|
61
|
+
const AUTH_SESSION_PEER_CHANNEL_NAME = 'edsis-auth-session-peer';
|
|
58
62
|
const EMPTY_SESSION_FINGERPRINT = '__empty_session__';
|
|
59
63
|
const JWT_PART_COUNT = 3;
|
|
60
64
|
const TOKEN_EXPIRY_SKEW_MS = 30_000;
|
|
65
|
+
const PEER_SESSION_REQUEST_TIMEOUT_MS = 150;
|
|
66
|
+
const TRUSTED_OPAQUE_TOKEN_REVALIDATION_MS = 90_000;
|
|
67
|
+
const AUTH_SESSION_MONITOR_INTERVAL_MS = 10_000;
|
|
61
68
|
let validatedAccessToken = '';
|
|
62
69
|
let validatedUntilEpochMs = 0;
|
|
63
70
|
let failedSessionFingerprint = null;
|
|
64
71
|
let pendingSessionValidation = null;
|
|
72
|
+
let sessionPeerChannel = null;
|
|
73
|
+
let sessionPeerChannelOwner = null;
|
|
74
|
+
let sessionPeerId = '';
|
|
75
|
+
let sessionMonitorCleanup = null;
|
|
76
|
+
let sessionMonitorOwner = null;
|
|
77
|
+
function clearSessionMonitor() {
|
|
78
|
+
sessionMonitorCleanup?.();
|
|
79
|
+
sessionMonitorCleanup = null;
|
|
80
|
+
sessionMonitorOwner = null;
|
|
81
|
+
}
|
|
65
82
|
function ɵresetAuthSessionModuleState() {
|
|
66
83
|
clearValidatedSession();
|
|
67
84
|
clearFailedSessionFingerprint();
|
|
68
85
|
pendingSessionValidation = null;
|
|
86
|
+
clearSessionMonitor();
|
|
87
|
+
sessionPeerChannel?.close();
|
|
88
|
+
sessionPeerChannel = null;
|
|
89
|
+
sessionPeerChannelOwner = null;
|
|
90
|
+
sessionPeerId = '';
|
|
69
91
|
}
|
|
70
92
|
function getSessionStorage(documentRef) {
|
|
71
93
|
return documentRef.defaultView?.sessionStorage ?? null;
|
|
72
94
|
}
|
|
95
|
+
function getLocalStorage(documentRef) {
|
|
96
|
+
return documentRef.defaultView?.localStorage ?? null;
|
|
97
|
+
}
|
|
98
|
+
function createSessionPeerId(windowRef) {
|
|
99
|
+
return windowRef.crypto?.randomUUID?.() ?? `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
100
|
+
}
|
|
101
|
+
function isSessionPeerResponseMessage(message) {
|
|
102
|
+
return message.type === 'session-response';
|
|
103
|
+
}
|
|
104
|
+
function buildStoredSessionData(documentRef) {
|
|
105
|
+
const session = readStoredSession(documentRef);
|
|
106
|
+
if (!session) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
access_token: session.accessToken,
|
|
111
|
+
name: session.name,
|
|
112
|
+
role: session.role,
|
|
113
|
+
user_id: session.userId,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function ensureSessionPeerChannel(documentRef) {
|
|
117
|
+
const windowRef = documentRef.defaultView;
|
|
118
|
+
if (!windowRef?.BroadcastChannel) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (sessionPeerChannel && sessionPeerChannelOwner === windowRef) {
|
|
122
|
+
return sessionPeerChannel;
|
|
123
|
+
}
|
|
124
|
+
sessionPeerChannel?.close();
|
|
125
|
+
sessionPeerId = createSessionPeerId(windowRef);
|
|
126
|
+
sessionPeerChannelOwner = windowRef;
|
|
127
|
+
sessionPeerChannel = new windowRef.BroadcastChannel(AUTH_SESSION_PEER_CHANNEL_NAME);
|
|
128
|
+
sessionPeerChannel.addEventListener('message', (event) => {
|
|
129
|
+
const message = event.data;
|
|
130
|
+
if (message.type !== 'session-request' || message.senderId === sessionPeerId) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const sessionData = buildStoredSessionData(documentRef);
|
|
134
|
+
if (!sessionData) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
sessionPeerChannel?.postMessage({
|
|
138
|
+
type: 'session-response',
|
|
139
|
+
requestId: message.requestId,
|
|
140
|
+
senderId: sessionPeerId,
|
|
141
|
+
sessionData,
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
return sessionPeerChannel;
|
|
145
|
+
}
|
|
146
|
+
async function restoreSessionFromPeer(documentRef) {
|
|
147
|
+
const windowRef = documentRef.defaultView;
|
|
148
|
+
const channel = ensureSessionPeerChannel(documentRef);
|
|
149
|
+
if (!windowRef || !channel || !sessionPeerId) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
const requestId = createSessionPeerId(windowRef);
|
|
153
|
+
return new Promise((resolve) => {
|
|
154
|
+
let settled = false;
|
|
155
|
+
let timeoutId = 0;
|
|
156
|
+
const finish = (result) => {
|
|
157
|
+
if (settled) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
settled = true;
|
|
161
|
+
windowRef.clearTimeout(timeoutId);
|
|
162
|
+
channel.removeEventListener('message', handleMessage);
|
|
163
|
+
resolve(result);
|
|
164
|
+
};
|
|
165
|
+
const handleMessage = (event) => {
|
|
166
|
+
const message = event.data;
|
|
167
|
+
if (!isSessionPeerResponseMessage(message) ||
|
|
168
|
+
message.senderId === sessionPeerId ||
|
|
169
|
+
message.requestId !== requestId) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (!readSessionDataValue(message.sessionData, 'access_token')) {
|
|
173
|
+
finish(false);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
persistSession(documentRef, message.sessionData);
|
|
177
|
+
finish(true);
|
|
178
|
+
};
|
|
179
|
+
timeoutId = windowRef.setTimeout(() => finish(false), PEER_SESSION_REQUEST_TIMEOUT_MS);
|
|
180
|
+
channel.addEventListener('message', handleMessage);
|
|
181
|
+
channel.postMessage({
|
|
182
|
+
type: 'session-request',
|
|
183
|
+
requestId,
|
|
184
|
+
senderId: sessionPeerId,
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
}
|
|
73
188
|
function readSessionStorageValue(documentRef, storageKey) {
|
|
74
189
|
return getSessionStorage(documentRef)?.getItem(storageKey)?.trim() ?? '';
|
|
75
190
|
}
|
|
@@ -96,6 +211,23 @@ function hasFailedSessionFingerprint(documentRef) {
|
|
|
96
211
|
function markFailedSessionFingerprint(documentRef) {
|
|
97
212
|
failedSessionFingerprint = readSessionFingerprint(documentRef);
|
|
98
213
|
}
|
|
214
|
+
function readTrustedAccessToken(documentRef) {
|
|
215
|
+
return readSessionStorageValue(documentRef, 'access_token_trusted');
|
|
216
|
+
}
|
|
217
|
+
function hasRefreshCandidate(documentRef) {
|
|
218
|
+
return Boolean(readStoredAccessToken(documentRef) ||
|
|
219
|
+
readTrustedAccessToken(documentRef) ||
|
|
220
|
+
getLocalStorage(documentRef)?.getItem(AUTH_SESSION_PERSISTENT_HINT_KEY));
|
|
221
|
+
}
|
|
222
|
+
function shouldClearSessionAfterRefreshFailure(error) {
|
|
223
|
+
if (error instanceof HttpErrorResponse) {
|
|
224
|
+
return error.status === 401 || error.status === 403;
|
|
225
|
+
}
|
|
226
|
+
if (error instanceof Error) {
|
|
227
|
+
return /invalid refresh token|unauthorized|forbidden/iu.test(error.message);
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
99
231
|
function getTokenExpiryEpochMs(documentRef, accessToken) {
|
|
100
232
|
const windowRef = documentRef.defaultView;
|
|
101
233
|
const tokenParts = accessToken.split('.');
|
|
@@ -120,7 +252,12 @@ function markValidatedSession(documentRef) {
|
|
|
120
252
|
}
|
|
121
253
|
const tokenExpiryEpochMs = getTokenExpiryEpochMs(documentRef, accessToken);
|
|
122
254
|
if (!tokenExpiryEpochMs) {
|
|
123
|
-
|
|
255
|
+
if (readTrustedAccessToken(documentRef) !== accessToken) {
|
|
256
|
+
clearValidatedSession();
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
validatedAccessToken = accessToken;
|
|
260
|
+
validatedUntilEpochMs = Date.now() + TRUSTED_OPAQUE_TOKEN_REVALIDATION_MS;
|
|
124
261
|
return;
|
|
125
262
|
}
|
|
126
263
|
validatedAccessToken = accessToken;
|
|
@@ -163,6 +300,7 @@ function readStoredSession(documentRef) {
|
|
|
163
300
|
};
|
|
164
301
|
}
|
|
165
302
|
function persistSession(documentRef, sessionData) {
|
|
303
|
+
ensureSessionPeerChannel(documentRef);
|
|
166
304
|
const sessionStorageRef = getSessionStorage(documentRef);
|
|
167
305
|
if (!sessionStorageRef) {
|
|
168
306
|
clearValidatedSession();
|
|
@@ -174,12 +312,18 @@ function persistSession(documentRef, sessionData) {
|
|
|
174
312
|
for (const storageKey of AUTH_SESSION_STORAGE_KEYS) {
|
|
175
313
|
sessionStorageRef.removeItem(storageKey);
|
|
176
314
|
}
|
|
315
|
+
for (const storageKey of AUTH_SESSION_METADATA_STORAGE_KEYS) {
|
|
316
|
+
sessionStorageRef.removeItem(storageKey);
|
|
317
|
+
}
|
|
318
|
+
getLocalStorage(documentRef)?.removeItem(AUTH_SESSION_PERSISTENT_HINT_KEY);
|
|
177
319
|
clearValidatedSession();
|
|
178
320
|
clearFailedSessionFingerprint();
|
|
179
321
|
notifySessionChange(documentRef);
|
|
180
322
|
return null;
|
|
181
323
|
}
|
|
182
324
|
sessionStorageRef.setItem('access_token', nextAccessToken);
|
|
325
|
+
sessionStorageRef.setItem('access_token_trusted', nextAccessToken);
|
|
326
|
+
getLocalStorage(documentRef)?.setItem(AUTH_SESSION_PERSISTENT_HINT_KEY, '1');
|
|
183
327
|
for (const storageKey of AUTH_SESSION_STORAGE_KEYS.slice(1)) {
|
|
184
328
|
if (!sessionData || !Object.hasOwn(sessionData, storageKey)) {
|
|
185
329
|
continue;
|
|
@@ -201,29 +345,89 @@ function clearStoredSession(documentRef, options) {
|
|
|
201
345
|
for (const storageKey of AUTH_SESSION_STORAGE_KEYS) {
|
|
202
346
|
sessionStorageRef?.removeItem(storageKey);
|
|
203
347
|
}
|
|
348
|
+
for (const storageKey of AUTH_SESSION_METADATA_STORAGE_KEYS) {
|
|
349
|
+
sessionStorageRef?.removeItem(storageKey);
|
|
350
|
+
}
|
|
351
|
+
getLocalStorage(documentRef)?.removeItem(AUTH_SESSION_PERSISTENT_HINT_KEY);
|
|
204
352
|
clearValidatedSession();
|
|
205
353
|
if (!options?.preserveFailedSessionFingerprint) {
|
|
206
354
|
clearFailedSessionFingerprint();
|
|
207
355
|
}
|
|
208
356
|
notifySessionChange(documentRef);
|
|
209
357
|
}
|
|
210
|
-
|
|
358
|
+
function startAuthSessionMonitor(documentRef, authSessionService, options) {
|
|
359
|
+
const windowRef = documentRef.defaultView;
|
|
360
|
+
if (!windowRef) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (sessionMonitorCleanup && sessionMonitorOwner === windowRef) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
clearSessionMonitor();
|
|
367
|
+
let disposed = false;
|
|
368
|
+
const runValidation = async () => {
|
|
369
|
+
const hadRefreshCandidate = hasRefreshCandidate(documentRef);
|
|
370
|
+
if (!hadRefreshCandidate) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const isSessionValid = options?.validateSession
|
|
374
|
+
? await options.validateSession()
|
|
375
|
+
: await validateStoredSession(documentRef, authSessionService, { forceRefresh: true });
|
|
376
|
+
if (!disposed && !isSessionValid && hadRefreshCandidate) {
|
|
377
|
+
options?.onSessionInvalid?.();
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
const handleFocus = () => {
|
|
381
|
+
void runValidation();
|
|
382
|
+
};
|
|
383
|
+
const handleVisibilityChange = () => {
|
|
384
|
+
if (documentRef.visibilityState === 'visible') {
|
|
385
|
+
void runValidation();
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
const intervalId = windowRef.setInterval(handleFocus, AUTH_SESSION_MONITOR_INTERVAL_MS);
|
|
389
|
+
windowRef.addEventListener('focus', handleFocus);
|
|
390
|
+
documentRef.addEventListener('visibilitychange', handleVisibilityChange);
|
|
391
|
+
sessionMonitorOwner = windowRef;
|
|
392
|
+
sessionMonitorCleanup = () => {
|
|
393
|
+
disposed = true;
|
|
394
|
+
windowRef.clearInterval(intervalId);
|
|
395
|
+
windowRef.removeEventListener('focus', handleFocus);
|
|
396
|
+
documentRef.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
async function validateStoredSession(documentRef, authSessionService, options) {
|
|
400
|
+
ensureSessionPeerChannel(documentRef);
|
|
211
401
|
if (!options?.forceRefresh && hasValidatedSession(documentRef)) {
|
|
212
402
|
return true;
|
|
213
403
|
}
|
|
214
|
-
if (hasFailedSessionFingerprint(documentRef)) {
|
|
215
|
-
return false;
|
|
216
|
-
}
|
|
217
404
|
if (pendingSessionValidation) {
|
|
218
405
|
return pendingSessionValidation;
|
|
219
406
|
}
|
|
220
407
|
pendingSessionValidation = (async () => {
|
|
221
408
|
try {
|
|
222
|
-
|
|
409
|
+
if (!options?.forceRefresh && !hasStoredSession(documentRef) && (await restoreSessionFromPeer(documentRef))) {
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
if (!options?.forceRefresh && hasValidatedSession(documentRef)) {
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
if (!options?.forceRefresh && !hasRefreshCandidate(documentRef)) {
|
|
416
|
+
clearValidatedSession();
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
if (hasFailedSessionFingerprint(documentRef)) {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
const response = await firstValueFrom(authSessionService.refreshAuthToken());
|
|
223
423
|
persistSession(documentRef, response.data);
|
|
224
424
|
return hasValidatedSession(documentRef);
|
|
225
425
|
}
|
|
226
|
-
catch {
|
|
426
|
+
catch (error) {
|
|
427
|
+
if (!shouldClearSessionAfterRefreshFailure(error)) {
|
|
428
|
+
clearValidatedSession();
|
|
429
|
+
return hasStoredSession(documentRef);
|
|
430
|
+
}
|
|
227
431
|
clearStoredSession(documentRef, { preserveFailedSessionFingerprint: true });
|
|
228
432
|
markFailedSessionFingerprint(documentRef);
|
|
229
433
|
return false;
|
|
@@ -236,13 +440,13 @@ async function validateStoredSession(documentRef, authService, options) {
|
|
|
236
440
|
}
|
|
237
441
|
function injectSessionState() {
|
|
238
442
|
const documentRef = inject(DOCUMENT);
|
|
239
|
-
const
|
|
240
|
-
return validateStoredSession(documentRef,
|
|
443
|
+
const authSessionService = inject(AuthSessionService);
|
|
444
|
+
return validateStoredSession(documentRef, authSessionService);
|
|
241
445
|
}
|
|
242
446
|
function injectSessionRefresh() {
|
|
243
447
|
const documentRef = inject(DOCUMENT);
|
|
244
|
-
const
|
|
245
|
-
return validateStoredSession(documentRef,
|
|
448
|
+
const authSessionService = inject(AuthSessionService);
|
|
449
|
+
return validateStoredSession(documentRef, authSessionService, { forceRefresh: true });
|
|
246
450
|
}
|
|
247
451
|
function injectSessionClear() {
|
|
248
452
|
clearStoredSession(inject(DOCUMENT));
|
|
@@ -252,6 +456,7 @@ const PROTECTED_API_PATH_PREFIX = '/v1/';
|
|
|
252
456
|
const AUTH_PATH_PREFIX = '/v1/auth/';
|
|
253
457
|
const HEALTH_PATH = '/health';
|
|
254
458
|
const EDSIS_SKIP_AUTH = new HttpContextToken(() => false);
|
|
459
|
+
const EDSIS_SKIP_SESSION_REFRESH = new HttpContextToken(() => false);
|
|
255
460
|
const EDSIS_AUTH_RETRY = new HttpContextToken(() => false);
|
|
256
461
|
function normalizeUrl(url) {
|
|
257
462
|
try {
|
|
@@ -306,29 +511,34 @@ function applyBearerToken(request, accessToken, isRetry = false) {
|
|
|
306
511
|
},
|
|
307
512
|
});
|
|
308
513
|
}
|
|
309
|
-
async function resolveAccessToken(documentRef,
|
|
310
|
-
const isSessionValid = await validateStoredSession(documentRef,
|
|
514
|
+
async function resolveAccessToken(documentRef, authSessionService, options) {
|
|
515
|
+
const isSessionValid = await validateStoredSession(documentRef, authSessionService, options);
|
|
311
516
|
return isSessionValid ? readStoredAccessToken(documentRef) : '';
|
|
312
517
|
}
|
|
313
|
-
function handleUnauthorizedError(error, request, next, documentRef,
|
|
314
|
-
if (!(error instanceof HttpErrorResponse) ||
|
|
518
|
+
function handleUnauthorizedError(error, request, next, documentRef, authSessionService) {
|
|
519
|
+
if (!(error instanceof HttpErrorResponse) ||
|
|
520
|
+
error.status !== 401 ||
|
|
521
|
+
request.context.get(EDSIS_AUTH_RETRY) ||
|
|
522
|
+
request.context.get(EDSIS_SKIP_SESSION_REFRESH)) {
|
|
315
523
|
return throwError(() => error);
|
|
316
524
|
}
|
|
317
|
-
return from(resolveAccessToken(documentRef,
|
|
525
|
+
return from(resolveAccessToken(documentRef, authSessionService, { forceRefresh: true })).pipe(switchMap((refreshedAccessToken) => {
|
|
318
526
|
if (!refreshedAccessToken) {
|
|
319
527
|
clearStoredSession(documentRef);
|
|
320
528
|
return throwError(() => error);
|
|
321
529
|
}
|
|
322
530
|
return next(applyBearerToken(request, refreshedAccessToken, true));
|
|
323
|
-
}), catchError(() => {
|
|
324
|
-
|
|
531
|
+
}), catchError((refreshError) => {
|
|
532
|
+
if (shouldClearSessionAfterRefreshFailure(refreshError)) {
|
|
533
|
+
clearStoredSession(documentRef);
|
|
534
|
+
}
|
|
325
535
|
return throwError(() => error);
|
|
326
536
|
}));
|
|
327
537
|
}
|
|
328
538
|
const edsisAuthInterceptor = (request, next) => {
|
|
329
539
|
const documentRef = inject(DOCUMENT);
|
|
330
540
|
const apiConfiguration = inject(ApiConfiguration);
|
|
331
|
-
const
|
|
541
|
+
const authSessionService = inject(AuthSessionService);
|
|
332
542
|
if (request.context.get(EDSIS_SKIP_AUTH)) {
|
|
333
543
|
return next(request);
|
|
334
544
|
}
|
|
@@ -340,9 +550,12 @@ const edsisAuthInterceptor = (request, next) => {
|
|
|
340
550
|
if (isAuthRequest(apiPath) || isHealthRequest(apiPath)) {
|
|
341
551
|
return next(preparedRequest);
|
|
342
552
|
}
|
|
343
|
-
|
|
553
|
+
const skipSessionRefresh = request.context.get(EDSIS_SKIP_SESSION_REFRESH);
|
|
554
|
+
return from(skipSessionRefresh
|
|
555
|
+
? Promise.resolve(readStoredAccessToken(documentRef))
|
|
556
|
+
: resolveAccessToken(documentRef, authSessionService)).pipe(switchMap((accessToken) => {
|
|
344
557
|
const authorizedRequest = accessToken ? applyBearerToken(preparedRequest, accessToken) : preparedRequest;
|
|
345
|
-
return next(authorizedRequest).pipe(catchError((error) => handleUnauthorizedError(error, preparedRequest, next, documentRef,
|
|
558
|
+
return next(authorizedRequest).pipe(catchError((error) => handleUnauthorizedError(error, preparedRequest, next, documentRef, authSessionService)));
|
|
346
559
|
}));
|
|
347
560
|
};
|
|
348
561
|
|
|
@@ -351,6 +564,44 @@ function provideEdsisAuth(config) {
|
|
|
351
564
|
return makeEnvironmentProviders([
|
|
352
565
|
provideApiConfiguration(resolvedConfig.apiUrl),
|
|
353
566
|
provideHttpClient(withFetch(), withInterceptors([edsisAuthInterceptor])),
|
|
567
|
+
provideEnvironmentInitializer(() => {
|
|
568
|
+
const documentRef = inject(DOCUMENT);
|
|
569
|
+
const authSessionService = inject(AuthSessionService);
|
|
570
|
+
const router = inject(Router, { optional: true });
|
|
571
|
+
const userProfileService = inject(UserProfileService);
|
|
572
|
+
startAuthSessionMonitor(documentRef, authSessionService, {
|
|
573
|
+
validateSession: async () => {
|
|
574
|
+
if (!readStoredAccessToken(documentRef)) {
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
try {
|
|
578
|
+
await firstValueFrom(userProfileService.getCurrentUser(undefined, new HttpContext().set(EDSIS_SKIP_SESSION_REFRESH, true)));
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
catch (error) {
|
|
582
|
+
if (error instanceof HttpErrorResponse && error.status === 401) {
|
|
583
|
+
clearStoredSession(documentRef);
|
|
584
|
+
return false;
|
|
585
|
+
}
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
},
|
|
589
|
+
onSessionInvalid: () => {
|
|
590
|
+
if (router) {
|
|
591
|
+
void router.navigateByUrl(resolvedConfig.guestHomePath, { replaceUrl: true });
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
const windowRef = documentRef.defaultView;
|
|
595
|
+
if (!windowRef) {
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
const guestUrl = new URL(resolvedConfig.guestHomePath, windowRef.location.origin).toString();
|
|
599
|
+
if (windowRef.location.href !== guestUrl) {
|
|
600
|
+
windowRef.location.replace(guestUrl);
|
|
601
|
+
}
|
|
602
|
+
},
|
|
603
|
+
});
|
|
604
|
+
}),
|
|
354
605
|
{
|
|
355
606
|
provide: EDSIS_AUTH_CONFIG,
|
|
356
607
|
useValue: resolvedConfig,
|
|
@@ -413,10 +664,16 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.15", ngImpo
|
|
|
413
664
|
}]
|
|
414
665
|
}] });
|
|
415
666
|
|
|
416
|
-
function buildVerifyUrl(config) {
|
|
417
|
-
const baseUrl =
|
|
667
|
+
function buildVerifyUrl(config, runtimeOrigin = readRuntimeOrigin()) {
|
|
668
|
+
const baseUrl = normalizeText(runtimeOrigin) ?? config.appUrl;
|
|
418
669
|
return new URL(config.verifyPath, `${baseUrl}/`).toString();
|
|
419
670
|
}
|
|
671
|
+
function readRuntimeOrigin() {
|
|
672
|
+
if (typeof window === 'undefined') {
|
|
673
|
+
return null;
|
|
674
|
+
}
|
|
675
|
+
return normalizeText(window.location.origin);
|
|
676
|
+
}
|
|
420
677
|
function extractMagicToken(queryToken, fragment) {
|
|
421
678
|
const tokenFromQuery = normalizeText(queryToken);
|
|
422
679
|
if (tokenFromQuery) {
|
|
@@ -471,7 +728,8 @@ function normalizeText(value) {
|
|
|
471
728
|
class AuthLoginPage {
|
|
472
729
|
destroyRef = inject(DestroyRef);
|
|
473
730
|
formBuilder = inject(FormBuilder);
|
|
474
|
-
|
|
731
|
+
authMagicLinkService = inject(AuthMagicLinkService);
|
|
732
|
+
authConfig = inject(EDSIS_AUTH_CONFIG);
|
|
475
733
|
form = this.formBuilder.nonNullable.group({
|
|
476
734
|
email: ['', [Validators.required, Validators.email, Validators.maxLength(255)]],
|
|
477
735
|
});
|
|
@@ -535,10 +793,11 @@ class AuthLoginPage {
|
|
|
535
793
|
this.errorMessage.set(null);
|
|
536
794
|
const email = this.emailControl.getRawValue().trim().toLowerCase();
|
|
537
795
|
this.emailControl.setValue(email);
|
|
538
|
-
this.
|
|
796
|
+
this.authMagicLinkService
|
|
539
797
|
.requestMagicLoginLink({
|
|
540
798
|
body: {
|
|
541
799
|
email,
|
|
800
|
+
frontend_url: buildVerifyUrl(this.authConfig),
|
|
542
801
|
},
|
|
543
802
|
})
|
|
544
803
|
.pipe(takeUntilDestroyed(this.destroyRef), finalize(() => this.isSubmitting.set(false)))
|
|
@@ -901,7 +1160,7 @@ class AuthLogoutPage {
|
|
|
901
1160
|
destroyRef = inject(DestroyRef);
|
|
902
1161
|
documentRef = inject(DOCUMENT);
|
|
903
1162
|
router = inject(Router);
|
|
904
|
-
|
|
1163
|
+
authSessionService = inject(AuthSessionService);
|
|
905
1164
|
authConfig = inject(EDSIS_AUTH_CONFIG);
|
|
906
1165
|
guestHomePath = this.authConfig.guestHomePath;
|
|
907
1166
|
authenticatedHomePath = this.authConfig.authenticatedHomePath;
|
|
@@ -909,7 +1168,7 @@ class AuthLogoutPage {
|
|
|
909
1168
|
message = signal('Mengakhiri sesi...', ...(ngDevMode ? [{ debugName: "message" }] : /* istanbul ignore next */ []));
|
|
910
1169
|
ngOnInit() {
|
|
911
1170
|
clearStoredSession(this.documentRef);
|
|
912
|
-
this.
|
|
1171
|
+
this.authSessionService
|
|
913
1172
|
.logoutAuthSession()
|
|
914
1173
|
.pipe(takeUntilDestroyed(this.destroyRef), finalize(() => {
|
|
915
1174
|
if (this.state() === 'pending') {
|
|
@@ -1066,7 +1325,7 @@ class AuthVerifyPage {
|
|
|
1066
1325
|
route = inject(ActivatedRoute);
|
|
1067
1326
|
router = inject(Router);
|
|
1068
1327
|
location = inject(Location);
|
|
1069
|
-
|
|
1328
|
+
authMagicLinkService = inject(AuthMagicLinkService);
|
|
1070
1329
|
authConfig = inject(EDSIS_AUTH_CONFIG);
|
|
1071
1330
|
queryParamMap = toSignal(this.route.queryParamMap, {
|
|
1072
1331
|
initialValue: this.route.snapshot.queryParamMap,
|
|
@@ -1110,7 +1369,7 @@ class AuthVerifyPage {
|
|
|
1110
1369
|
}
|
|
1111
1370
|
verify(token) {
|
|
1112
1371
|
this.verifySubscription?.unsubscribe();
|
|
1113
|
-
this.verifySubscription = this.
|
|
1372
|
+
this.verifySubscription = this.authMagicLinkService
|
|
1114
1373
|
.verifyMagicLink({
|
|
1115
1374
|
body: {
|
|
1116
1375
|
token,
|
|
@@ -1297,5 +1556,5 @@ const authRoutes = [
|
|
|
1297
1556
|
* Generated bundle index. Do not edit.
|
|
1298
1557
|
*/
|
|
1299
1558
|
|
|
1300
|
-
export { AUTH_SESSION_CHANGE_EVENT, AuthLoginPage, AuthLogoutPage, AuthShellPage, AuthVerifyPage, EDSIS_AUTH_CONFIG, EDSIS_SKIP_AUTH, authRoutes, authenticatedGuard, clearStoredSession, edsisAuthInterceptor, guestOnlyGuard, hasStoredSession, homeRedirectGuard, injectSessionClear, injectSessionRefresh, injectSessionState, persistSession, provideEdsisAuth, readStoredAccessToken, readStoredSession, resolveEdsisAuthConfig, validateStoredSession, ɵresetAuthSessionModuleState };
|
|
1559
|
+
export { AUTH_SESSION_CHANGE_EVENT, AuthLoginPage, AuthLogoutPage, AuthShellPage, AuthVerifyPage, EDSIS_AUTH_CONFIG, EDSIS_SKIP_AUTH, EDSIS_SKIP_SESSION_REFRESH, authRoutes, authenticatedGuard, clearStoredSession, edsisAuthInterceptor, guestOnlyGuard, hasStoredSession, homeRedirectGuard, injectSessionClear, injectSessionRefresh, injectSessionState, persistSession, provideEdsisAuth, readStoredAccessToken, readStoredSession, resolveEdsisAuthConfig, shouldClearSessionAfterRefreshFailure, startAuthSessionMonitor, validateStoredSession, ɵresetAuthSessionModuleState };
|
|
1301
1560
|
//# sourceMappingURL=edsis-angular-module-auth.mjs.map
|