Enhance OIDC flow for better error handling and misconfiguration recovery

This commit is contained in:
aditya.chandel
2025-08-15 15:44:24 -06:00
committed by Aditya Chandel
parent 628a6c7bcf
commit 44541849ff
7 changed files with 225 additions and 42 deletions
@@ -1,7 +1,7 @@
<div class="flex items-center justify-center min-h-screen min-w-[100vw] overflow-hidden"
style="background: linear-gradient(60deg, var(--primary-color) 0%, #1e3a8a 100%);">
<div class="flex flex-col items-center justify-center -mt-48">
<div style="border-radius: 56px; padding: 0.3rem; background: linear-gradient(180deg, var(--primary-color) 10%, rgba(33, 150, 243, 0) 30%)">
<div class="flex flex-col items-center justify-center -mt-48 w-[90%] sm:w-auto sm:min-w-[400px] md:min-w-[500px]">
<div class="w-full" style="border-radius: 56px; padding: 0.3rem; background: linear-gradient(180deg, var(--primary-color) 10%, rgba(33, 150, 243, 0) 30%)">
<div class="w-full bg-surface-900 pt-8 pb-12 px-8 sm:px-8" style="border-radius: 53px">
<div class="text-center mb-8">
<svg class="mb-8 w-14 h-14 mx-auto" viewBox="0 0 126 126" fill="var(--primary-color)" xmlns="http://www.w3.org/2000/svg">
@@ -15,17 +15,48 @@
<span class="font-medium">Sign in to continue</span>
</div>
<form class="flex flex-col gap-4" #loginForm="ngForm" (ngSubmit)="login()">
@if (showOidcBypassInfo) {
<div class="mb-4 p-3 rounded-md bg-yellow-900/30 border border-yellow-600/50 w-full">
<div class="flex flex-col items-center">
<div class="flex items-center gap-2 text-yellow-200 text-sm mb-2">
<i class="pi pi-exclamation-triangle"></i>
<span class="font-medium">OIDC Authentication Issues Detected</span>
</div>
<p class="text-xs text-yellow-300 mb-3 text-center">
{{ oidcBypassMessage }}
</p>
<div class="flex gap-4">
<p-button
size="small"
severity="warn"
label="Retry OIDC"
icon="pi pi-refresh"
(click)="retryOidc()"
/>
<p-button
size="small"
severity="success"
label="Use Local Login"
icon="pi pi-times"
(click)="dismissOidcWarning()"
/>
</div>
</div>
</div>
}
<form class="flex flex-col gap-4 w-full" #loginForm="ngForm" (ngSubmit)="login()">
<div class="flex flex-col gap-2">
<label for="username" class="text-lg font-medium">Username</label>
<input
fluid
pInputText
id="username"
name="username"
required
[(ngModel)]="username"
placeholder="Enter your username"
class="w-full md:w-[27rem]"
class="w-full"
/>
</div>
@@ -39,24 +70,25 @@
[feedback]="false"
placeholder="Enter your password"
[toggleMask]="true"
styleClass="mb-4"
class="mb-4"
[fluid]="true"
></p-password>
</div>
<p-button
fluid
type="submit"
label="Login"
icon="pi pi-sign-in"
[disabled]="!loginForm.valid || !username || !password"
styleClass="w-full"
></p-button>
class="w-full">
</p-button>
@if (errorMessage) {
<p-message severity="error" text="{{ errorMessage }}"></p-message>
<p-message severity="error">{{ errorMessage }}</p-message>
}
@if (oidcEnabled) {
@if (oidcEnabled && !isOidcBypassed) {
<div class="flex items-center gap-2 my-2">
<hr class="flex-grow" style="border: none; border-top: 1px solid var(--border-color);"/>
<span class="text-gray-300">or</span>
@@ -66,11 +98,31 @@
<p-button
fluid
severity="info"
label="Login with {{ oidcName }}"
icon="pi pi-id-card"
[label]="isOidcLoginInProgress ? 'Redirecting to ' + oidcName + '...' : 'Login with ' + oidcName"
[icon]="isOidcLoginInProgress ? 'pi pi-spin pi-spinner' : 'pi pi-id-card'"
[disabled]="isOidcLoginInProgress"
(click)="loginWithOidc()"
styleClass="w-full bg-gray-800 text-white hover:bg-gray-700"
/>
class="w-full bg-gray-800 text-white hover:bg-gray-700"/>
<div class="text-center">
<button
type="button"
class="text-xs text-gray-400 hover:text-gray-300 underline"
(click)="bypassOidc()">
Having trouble with {{ oidcName }}? Use local login only
</button>
</div>
}
@if (oidcEnabled && isOidcBypassed) {
<div class="text-center mt-4">
<button
type="button"
class="text-xs text-blue-400 hover:text-blue-300 underline"
(click)="enableOidc()">
Re-enable {{ oidcName }} login
</button>
</div>
}
</form>
</div>
@@ -1,4 +1,4 @@
import {Component, inject, OnInit} from '@angular/core';
import {Component, inject, OnInit, OnDestroy} from '@angular/core';
import {AuthService} from '../../service/auth.service';
import {Router} from '@angular/router';
import {FormsModule} from '@angular/forms';
@@ -7,11 +7,10 @@ import {Button} from 'primeng/button';
import {Message} from 'primeng/message';
import {InputText} from 'primeng/inputtext';
import {OAuthService} from 'angular-oauth2-oidc';
import {AppSettingsService} from '../../service/app-settings.service';
import {AppSettings} from '../../model/app-settings.model';
import {Observable} from 'rxjs';
import {filter, take} from 'rxjs/operators';
import {Observable, Subject} from 'rxjs';
import {filter, take, takeUntil} from 'rxjs/operators';
import {PublicAppSettings, PublicAppSettingService} from '../../../public-app-settings.service';
import {getOidcErrorCount, isOidcBypassed, resetOidcBypass} from '../../../auth-initializer';
@Component({
selector: 'app-login',
@@ -25,12 +24,16 @@ import {PublicAppSettings, PublicAppSettingService} from '../../../public-app-se
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
export class LoginComponent implements OnInit, OnDestroy {
username = '';
password = '';
errorMessage = '';
oidcEnabled = false;
oidcName = 'OIDC';
isOidcBypassed = false;
showOidcBypassInfo = false;
oidcBypassMessage = '';
isOidcLoginInProgress = false;
private authService = inject(AuthService);
private oAuthService = inject(OAuthService);
@@ -39,6 +42,8 @@ export class LoginComponent implements OnInit {
publicAppSettings$: Observable<PublicAppSettings | null> = this.appSettingsService.publicAppSettings$;
private destroy$ = new Subject<void>();
ngOnInit(): void {
this.publicAppSettings$
.pipe(
@@ -48,9 +53,27 @@ export class LoginComponent implements OnInit {
.subscribe(publicSettings => {
this.oidcEnabled = publicSettings!.oidcEnabled;
this.oidcName = publicSettings!.oidcProviderDetails?.providerName || 'OIDC';
this.checkOidcBypassStatus();
});
}
private checkOidcBypassStatus(): void {
this.isOidcBypassed = isOidcBypassed();
const errorCount = getOidcErrorCount();
if (this.oidcEnabled && (this.isOidcBypassed || errorCount > 0)) {
this.showOidcBypassInfo = true;
if (this.isOidcBypassed && errorCount >= 3) {
this.oidcBypassMessage = `${this.oidcName} authentication has been automatically disabled after ${errorCount} consecutive failures (including timeouts). You can retry or continue with local login.`;
} else if (this.isOidcBypassed) {
this.oidcBypassMessage = `${this.oidcName} authentication has been manually disabled. You can re-enable it or continue with local login.`;
} else if (errorCount > 0) {
this.oidcBypassMessage = `${this.oidcName} authentication encountered ${errorCount} error(s), possibly due to timeouts or server issues.`;
}
}
}
login(): void {
this.authService.internalLogin({username: this.username, password: this.password}).subscribe({
next: (response) => {
@@ -71,6 +94,53 @@ export class LoginComponent implements OnInit {
}
loginWithOidc(): void {
this.oAuthService.initCodeFlow();
if (this.isOidcLoginInProgress) {
return;
}
this.isOidcLoginInProgress = true;
this.errorMessage = '';
try {
setTimeout(() => {
this.isOidcLoginInProgress = false;
}, 5000);
this.oAuthService.initCodeFlow();
} catch (error) {
console.error('OIDC login initiation failed:', error);
this.errorMessage = 'Failed to initiate OIDC login. Please try again or use local login.';
this.isOidcLoginInProgress = false;
}
}
bypassOidc(): void {
localStorage.setItem('booklore-oidc-bypass', 'true');
this.isOidcBypassed = true;
this.showOidcBypassInfo = false;
}
enableOidc(): void {
resetOidcBypass();
this.isOidcBypassed = false;
this.showOidcBypassInfo = false;
this.isOidcLoginInProgress = false;
window.location.reload();
}
retryOidc(): void {
resetOidcBypass();
this.isOidcBypassed = false;
this.showOidcBypassInfo = false;
this.isOidcLoginInProgress = false;
window.location.reload();
}
dismissOidcWarning(): void {
this.showOidcBypassInfo = false;
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}