Enhance OIDC flow for better error handling and misconfiguration recovery
This commit is contained in:
committed by
Aditya Chandel
parent
628a6c7bcf
commit
44541849ff
@@ -4,6 +4,20 @@ import {AuthService, websocketInitializer} from './core/service/auth.service';
|
||||
import {AuthInitializationService} from './auth-initialization-service';
|
||||
import {PublicAppSettingService} from './public-app-settings.service';
|
||||
|
||||
const OIDC_BYPASS_KEY = 'booklore-oidc-bypass';
|
||||
const OIDC_ERROR_COUNT_KEY = 'booklore-oidc-error-count';
|
||||
const MAX_OIDC_RETRIES = 3;
|
||||
const OIDC_TIMEOUT_MS = 5000;
|
||||
|
||||
function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
|
||||
return Promise.race([
|
||||
promise,
|
||||
new Promise<T>((_, reject) =>
|
||||
setTimeout(() => reject(new Error(`Operation timed out after ${timeoutMs}ms`)), timeoutMs)
|
||||
)
|
||||
]);
|
||||
}
|
||||
|
||||
export function initializeAuthFactory() {
|
||||
return () => {
|
||||
const oauthService = inject(OAuthService);
|
||||
@@ -14,7 +28,10 @@ export function initializeAuthFactory() {
|
||||
return new Promise<void>((resolve) => {
|
||||
const sub = publicAppSettingService.publicAppSettings$.subscribe(publicSettings => {
|
||||
if (publicSettings) {
|
||||
if (publicSettings.oidcEnabled && publicSettings.oidcProviderDetails) {
|
||||
const oidcBypassed = localStorage.getItem(OIDC_BYPASS_KEY) === 'true';
|
||||
const errorCount = parseInt(localStorage.getItem(OIDC_ERROR_COUNT_KEY) || '0', 10);
|
||||
|
||||
if (publicSettings.oidcEnabled && publicSettings.oidcProviderDetails && !oidcBypassed && errorCount < MAX_OIDC_RETRIES) {
|
||||
const details = publicSettings.oidcProviderDetails;
|
||||
|
||||
oauthService.configure({
|
||||
@@ -28,9 +45,13 @@ export function initializeAuthFactory() {
|
||||
strictDiscoveryDocumentValidation: false,
|
||||
});
|
||||
|
||||
oauthService.loadDiscoveryDocumentAndTryLogin()
|
||||
console.log(`[OIDC] Attempting discovery and login (attempt ${errorCount + 1}/${MAX_OIDC_RETRIES})`);
|
||||
|
||||
withTimeout(oauthService.loadDiscoveryDocumentAndTryLogin(), OIDC_TIMEOUT_MS)
|
||||
.then(() => {
|
||||
console.log('[OIDC] Discovery document loaded and login attempted');
|
||||
localStorage.removeItem(OIDC_ERROR_COUNT_KEY);
|
||||
|
||||
if (oauthService.hasValidAccessToken()) {
|
||||
authService.tokenSubject.next(oauthService.getAccessToken())
|
||||
console.log('[OIDC] Valid access token found after tryLogin');
|
||||
@@ -45,15 +66,35 @@ export function initializeAuthFactory() {
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
const newErrorCount = errorCount + 1;
|
||||
localStorage.setItem(OIDC_ERROR_COUNT_KEY, newErrorCount.toString());
|
||||
|
||||
authInitService.markAsInitialized();
|
||||
|
||||
const isTimeout = err.message.includes('timed out');
|
||||
const errorType = isTimeout ? 'timeout' : 'network/configuration';
|
||||
|
||||
console.error(
|
||||
'OIDC initialization failed: Unable to complete OpenID Connect discovery or login. ' +
|
||||
'This may be due to an incorrect issuer URL, client ID, or network issue. ' +
|
||||
`OIDC initialization failed (${errorType}, attempt ${newErrorCount}/${MAX_OIDC_RETRIES}): ` +
|
||||
'Unable to complete OpenID Connect discovery or login. ' +
|
||||
`${isTimeout ? 'The request timed out after ' + OIDC_TIMEOUT_MS + 'ms. ' : ''}` +
|
||||
'This may be due to an incorrect issuer URL, client ID, network issue, or slow server response. ' +
|
||||
'Falling back to local login. Details:', err
|
||||
);
|
||||
|
||||
if (newErrorCount >= MAX_OIDC_RETRIES) {
|
||||
console.warn(`[OIDC] Maximum retry attempts (${MAX_OIDC_RETRIES}) exceeded. OIDC will be automatically bypassed until manually re-enabled.`);
|
||||
localStorage.setItem(OIDC_BYPASS_KEY, 'true');
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
if (oidcBypassed) {
|
||||
console.log('[OIDC] OIDC is manually bypassed, using local authentication only');
|
||||
} else if (errorCount >= MAX_OIDC_RETRIES) {
|
||||
console.log(`[OIDC] OIDC automatically bypassed due to ${errorCount} consecutive errors`);
|
||||
}
|
||||
authInitService.markAsInitialized();
|
||||
resolve();
|
||||
}
|
||||
@@ -63,3 +104,16 @@ export function initializeAuthFactory() {
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function resetOidcBypass(): void {
|
||||
localStorage.removeItem(OIDC_BYPASS_KEY);
|
||||
localStorage.removeItem(OIDC_ERROR_COUNT_KEY);
|
||||
}
|
||||
|
||||
export function isOidcBypassed(): boolean {
|
||||
return localStorage.getItem(OIDC_BYPASS_KEY) === 'true';
|
||||
}
|
||||
|
||||
export function getOidcErrorCount(): number {
|
||||
return parseInt(localStorage.getItem(OIDC_ERROR_COUNT_KEY) || '0', 10);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user