Fix OIDC auto-login not triggering in new browser tabs
This commit is contained in:
committed by
Aditya Chandel
parent
e41ecb49d0
commit
22b2c013bd
@@ -1,19 +1,12 @@
|
|||||||
import {Injectable} from '@angular/core';
|
import {Injectable} from '@angular/core';
|
||||||
import {BehaviorSubject} from 'rxjs';
|
import {BehaviorSubject} from 'rxjs';
|
||||||
|
|
||||||
@Injectable({ providedIn: 'root' })
|
@Injectable({providedIn: 'root'})
|
||||||
export class AuthInitializationService {
|
export class AuthInitializationService {
|
||||||
private initialized = new BehaviorSubject<boolean>(false);
|
private initialized = new BehaviorSubject<boolean>(false);
|
||||||
initialized$ = this.initialized.asObservable();
|
initialized$ = this.initialized.asObservable();
|
||||||
|
|
||||||
private oidcFailed = new BehaviorSubject<boolean>(false);
|
|
||||||
oidcFailed$ = this.oidcFailed.asObservable();
|
|
||||||
|
|
||||||
markAsInitialized() {
|
markAsInitialized() {
|
||||||
this.initialized.next(true);
|
this.initialized.next(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
setOidcFailed(failed: boolean) {
|
|
||||||
this.oidcFailed.next(failed);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,59 +16,52 @@ export function initializeAuthFactory() {
|
|||||||
|
|
||||||
return new Promise<void>((resolve) => {
|
return new Promise<void>((resolve) => {
|
||||||
const sub = appSettingsService.appSettings$.subscribe(settings => {
|
const sub = appSettingsService.appSettings$.subscribe(settings => {
|
||||||
if (!settings) return;
|
if (settings) {
|
||||||
|
if (settings.oidcEnabled && settings.oidcProviderDetails) {
|
||||||
|
const details = settings.oidcProviderDetails;
|
||||||
|
|
||||||
sub.unsubscribe();
|
oauthService.configure({
|
||||||
|
issuer: details.issuerUri,
|
||||||
if (settings.oidcEnabled && settings.oidcProviderDetails) {
|
clientId: details.clientId,
|
||||||
const details = settings.oidcProviderDetails;
|
scope: 'openid profile email offline_access',
|
||||||
|
redirectUri: window.location.origin + '/oauth2-callback',
|
||||||
oauthService.configure({
|
responseType: 'code',
|
||||||
issuer: details.issuerUri,
|
showDebugInformation: false,
|
||||||
clientId: details.clientId,
|
requireHttps: false,
|
||||||
scope: 'openid profile email offline_access',
|
strictDiscoveryDocumentValidation: false,
|
||||||
redirectUri: window.location.origin + '/oauth2-callback',
|
|
||||||
responseType: 'code',
|
|
||||||
showDebugInformation: false,
|
|
||||||
requireHttps: false,
|
|
||||||
strictDiscoveryDocumentValidation: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
oauthService.loadDiscoveryDocumentAndTryLogin()
|
|
||||||
.then(() => {
|
|
||||||
if (oauthService.hasValidAccessToken()) {
|
|
||||||
console.log('[OIDC] Valid access token found');
|
|
||||||
oauthService.setupAutomaticSilentRefresh();
|
|
||||||
websocketInitializer(authService);
|
|
||||||
} else {
|
|
||||||
console.warn('[OIDC] No valid access token. Will proceed to app and show login page.');
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error('[OIDC] Failed to load discovery document or login:', err);
|
|
||||||
authInitService.setOidcFailed(true);
|
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
authInitService.markAsInitialized();
|
|
||||||
resolve();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
} else if (settings.remoteAuthEnabled) {
|
oauthService.loadDiscoveryDocumentAndTryLogin()
|
||||||
authService.remoteLogin().subscribe({
|
.then(() => {
|
||||||
next: () => {
|
console.log('[OIDC] Discovery document loaded and login attempted');
|
||||||
authInitService.markAsInitialized();
|
if (oauthService.hasValidAccessToken()) {
|
||||||
resolve();
|
authService.tokenSubject.next(oauthService.getAccessToken())
|
||||||
},
|
console.log('[OIDC] Valid access token found after tryLogin');
|
||||||
error: err => {
|
router.navigate(['/dashboard']);
|
||||||
console.error('[Remote Login] failed:', err);
|
oauthService.setupAutomaticSilentRefresh();
|
||||||
authInitService.markAsInitialized();
|
websocketInitializer(authService);
|
||||||
resolve();
|
authInitService.markAsInitialized();
|
||||||
}
|
resolve();
|
||||||
});
|
} else {
|
||||||
|
console.log('[OIDC] No valid access token found, attempting silent login with prompt=none');
|
||||||
} else {
|
oauthService.initImplicitFlow();
|
||||||
authInitService.markAsInitialized();
|
resolve();
|
||||||
resolve();
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
authInitService.markAsInitialized();
|
||||||
|
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. ' +
|
||||||
|
'Falling back to local login. Details:', err
|
||||||
|
);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
authInitService.markAsInitialized();
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
sub.unsubscribe();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -20,11 +20,9 @@ export class AuthService {
|
|||||||
private oAuthService = inject(OAuthService);
|
private oAuthService = inject(OAuthService);
|
||||||
private router = inject(Router);
|
private router = inject(Router);
|
||||||
|
|
||||||
private tokenSubject = new BehaviorSubject<string | null>(this.getOidcAccessToken() || this.getInternalAccessToken());
|
public tokenSubject = new BehaviorSubject<string | null>(this.getOidcAccessToken() || this.getInternalAccessToken());
|
||||||
public token$ = this.tokenSubject.asObservable();
|
public token$ = this.tokenSubject.asObservable();
|
||||||
|
|
||||||
private logoutSubject = new Subject<void>();
|
|
||||||
public logout$ = this.logoutSubject.asObservable();
|
|
||||||
|
|
||||||
internalLogin(credentials: { username: string; password: string }): Observable<{ accessToken: string; refreshToken: string, isDefaultPassword: string }> {
|
internalLogin(credentials: { username: string; password: string }): Observable<{ accessToken: string; refreshToken: string, isDefaultPassword: string }> {
|
||||||
return this.http.post<{ accessToken: string; refreshToken: string, isDefaultPassword: string }>(`${this.apiUrl}/login`, credentials).pipe(
|
return this.http.post<{ accessToken: string; refreshToken: string, isDefaultPassword: string }>(`${this.apiUrl}/login`, credentials).pipe(
|
||||||
@@ -48,17 +46,6 @@ export class AuthService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteLogin(): Observable<{ accessToken: string; refreshToken: string, isDefaultPassword: string }> {
|
|
||||||
return this.http.get<{ accessToken: string; refreshToken: string, isDefaultPassword: string }>(`${this.apiUrl}/remote`).pipe(
|
|
||||||
tap((response) => {
|
|
||||||
if (response.accessToken && response.refreshToken) {
|
|
||||||
this.saveInternalTokens(response.accessToken, response.refreshToken);
|
|
||||||
this.initializeWebSocketConnection();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
saveInternalTokens(accessToken: string, refreshToken: string): void {
|
saveInternalTokens(accessToken: string, refreshToken: string): void {
|
||||||
localStorage.setItem('accessToken_Internal', accessToken);
|
localStorage.setItem('accessToken_Internal', accessToken);
|
||||||
localStorage.setItem('refreshToken_Internal', refreshToken);
|
localStorage.setItem('refreshToken_Internal', refreshToken);
|
||||||
@@ -81,13 +68,8 @@ export class AuthService {
|
|||||||
localStorage.removeItem('accessToken_Internal');
|
localStorage.removeItem('accessToken_Internal');
|
||||||
localStorage.removeItem('refreshToken_Internal');
|
localStorage.removeItem('refreshToken_Internal');
|
||||||
this.tokenSubject.next(null);
|
this.tokenSubject.next(null);
|
||||||
this.logoutSubject.next();
|
|
||||||
this.getRxStompService().deactivate();
|
this.getRxStompService().deactivate();
|
||||||
if (this.oAuthService.clientId) {
|
this.router.navigate(['/login']);
|
||||||
this.oAuthService.logOut();
|
|
||||||
} else {
|
|
||||||
this.router.navigate(['/login']);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getRxStompService(): RxStompService {
|
getRxStompService(): RxStompService {
|
||||||
|
|||||||
@@ -1,26 +1,28 @@
|
|||||||
import { Injectable, inject } from '@angular/core';
|
import {Injectable, inject} from '@angular/core';
|
||||||
import { AuthService } from './auth.service';
|
import {AuthService} from './auth.service';
|
||||||
import { UserService } from '../../settings/user-management/user.service';
|
import {UserService} from '../../settings/user-management/user.service';
|
||||||
import { filter, catchError } from 'rxjs/operators';
|
import {filter, catchError} from 'rxjs/operators';
|
||||||
import { of } from 'rxjs';
|
import {of} from 'rxjs';
|
||||||
|
import {OAuthService} from 'angular-oauth2-oidc';
|
||||||
|
|
||||||
@Injectable({ providedIn: 'root' })
|
@Injectable({providedIn: 'root'})
|
||||||
export class StartupService {
|
export class StartupService {
|
||||||
private auth = inject(AuthService);
|
private authService = inject(AuthService);
|
||||||
private userSvc = inject(UserService);
|
private userService = inject(UserService);
|
||||||
|
|
||||||
load(): Promise<void> {
|
load(): Promise<void> {
|
||||||
this.auth.token$
|
this.authService.token$
|
||||||
.pipe(filter(t => !!t))
|
.pipe(filter(t => !!t))
|
||||||
.subscribe(() => {
|
.subscribe(() => {
|
||||||
this.userSvc.getMyself()
|
this.userService.getMyself()
|
||||||
.pipe(catchError(() => of(null)))
|
.pipe(catchError(() => of(null)))
|
||||||
.subscribe(user => {
|
.subscribe(user => {
|
||||||
if (user) {
|
if (user) {
|
||||||
this.userSvc.setInitialUser(user);
|
this.userService.setInitialUser(user);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user