Skip to content

Commit

Permalink
Cherry picked commit from oidc and run fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
VitoAlbano committed Nov 12, 2024
1 parent 442dcb0 commit f532ae5
Show file tree
Hide file tree
Showing 41 changed files with 354 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Component, TemplateRef, ViewChild } from '@angular/core';
selector: 'adf-breadcrumb-item',
template: `
<ng-template #breadcrumbItemTemplate>
<ng-content></ng-content>
<ng-content />
</ng-template>
`
})
Expand Down
13 changes: 13 additions & 0 deletions lib/core/src/lib/auth/oidc/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,21 @@ import { Observable } from 'rxjs';
export abstract class AuthService {
abstract onLogin: Observable<any>;

/**
* An observable that emits a value when a logout event occurs.
* Implement this observable to handle any necessary cleanup or state updates
* when a user logs out of the application.
*/
abstract onLogout$: Observable<void>;

abstract onTokenReceived: Observable<any>;

/**
* An abstract observable that emits a boolean value indicating whether the discovery document
* has been successfully loaded.
*/
abstract isDiscoveryDocumentLoaded$: Observable<boolean>;

/** Subscribe to whether the user has valid Id/Access tokens. */
abstract authenticated$: Observable<boolean>;

Expand Down
2 changes: 0 additions & 2 deletions lib/core/src/lib/auth/oidc/oidc-authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export class OidcAuthenticationService extends BaseAuthenticationService {
* This observable combines the authentication status and the discovery document load status
* to decide if an SSO login is necessary. It emits `true` if the user is not authenticated
* and the discovery document is loaded, otherwise it emits `false`.
*
* @type {Observable<boolean>}
*/
shouldPerformSsoLogin$: Observable<boolean> = combineLatest([this.auth.authenticated$, this.auth.isDiscoveryDocumentLoaded$]).pipe(
map(([authenticated, isDiscoveryDocumentLoaded]) => !authenticated && isDiscoveryDocumentLoaded)
Expand Down
120 changes: 86 additions & 34 deletions lib/core/src/lib/auth/oidc/redirect-auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@
*/

import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { OAuthService, OAuthEvent, OAuthStorage, AUTH_CONFIG, TokenResponse, AuthConfig, OAuthLogger, OAuthErrorEvent, OAuthSuccessEvent, OAuthInfoEvent } from 'angular-oauth2-oidc';
import {
OAuthService,
OAuthEvent,
OAuthStorage,
AUTH_CONFIG,
TokenResponse,
AuthConfig,
OAuthLogger,
OAuthErrorEvent,
OAuthSuccessEvent,
OAuthInfoEvent
} from 'angular-oauth2-oidc';
import { of, Subject, timeout } from 'rxjs';
import { RedirectAuthService } from './redirect-auth.service';
import { AUTH_MODULE_CONFIG } from './auth-config';
Expand All @@ -43,18 +54,22 @@ describe('RedirectAuthService', () => {
retryLoginServiceSpy = jasmine.createSpyObj('RetryLoginService', ['tryToLoginTimes']);
timeSyncServiceSpy = jasmine.createSpyObj('TimeSyncService', ['checkTimeSync']);
oauthLoggerSpy = jasmine.createSpyObj('OAuthLogger', ['error', 'info', 'warn']);
oauthServiceSpy = jasmine.createSpyObj('OAuthService', [
'clearHashAfterLogin',
'configure',
'logOut',
'hasValidAccessToken',
'hasValidIdToken',
'setupAutomaticSilentRefresh',
'silentRefresh',
'refreshToken',
'getIdentityClaims',
'getAccessToken'
], { clockSkewInSec: 120, events: oauthEvents$, tokenValidationHandler: {} });
oauthServiceSpy = jasmine.createSpyObj(
'OAuthService',
[
'clearHashAfterLogin',
'configure',
'logOut',
'hasValidAccessToken',
'hasValidIdToken',
'setupAutomaticSilentRefresh',
'silentRefresh',
'refreshToken',
'getIdentityClaims',
'getAccessToken'
],
{ clockSkewInSec: 120, events: oauthEvents$, tokenValidationHandler: {} }
);
authConfigSpy = jasmine.createSpyObj('AuthConfig', ['sessionChecksEnabled']);

TestBed.configureTestingModule({
Expand Down Expand Up @@ -202,8 +217,14 @@ describe('RedirectAuthService', () => {
});

it('should logout user if token has expired due to local machine clock being out of sync', () => {
const mockTimeSync: TimeSync = { outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' };
const expectedError = new Error(`Token has expired due to local machine clock ${mockTimeSync.localDateTimeISO} being out of sync with server time ${mockTimeSync.serverDateTimeISO}`);
const mockTimeSync: TimeSync = {
outOfSync: true,
localDateTimeISO: '2024-10-10T22:00:18.621Z',
serverDateTimeISO: '2024-10-10T22:10:53.000Z'
};
const expectedError = new Error(
`Token has expired due to local machine clock ${mockTimeSync.localDateTimeISO} being out of sync with server time ${mockTimeSync.serverDateTimeISO}`
);

timeSyncServiceSpy.checkTimeSync.and.returnValue(of(mockTimeSync));

Expand Down Expand Up @@ -375,7 +396,7 @@ describe('RedirectAuthService', () => {

expect(oauthServiceSpy.logOut).not.toHaveBeenCalled();
expect(oauthLoggerSpy.error).not.toHaveBeenCalled();
expect(await firstEventOccurPromise).toEqual(expectedFakeErrorEvent);;
expect(await firstEventOccurPromise).toEqual(expectedFakeErrorEvent);

try {
tick(1000);
Expand All @@ -387,7 +408,6 @@ describe('RedirectAuthService', () => {
}));

it('should logout user if the second time the refresh token failed', fakeAsync(async () => {

const expectedErrorCausedBySecondTokenRefreshError = new OAuthErrorEvent('token_refresh_error', { reason: 'second token refresh error' }, {});

oauthEvents$.next(new OAuthErrorEvent('token_refresh_error', { reason: 'error' }, {}));
Expand All @@ -398,8 +418,12 @@ describe('RedirectAuthService', () => {
}));

it('should logout user if token_refresh_error is emitted because of clock out of sync', () => {
const expectedErrorMessage = new Error('OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z');
timeSyncServiceSpy.checkTimeSync.and.returnValue(of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync));
const expectedErrorMessage = new Error(
'OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z'
);
timeSyncServiceSpy.checkTimeSync.and.returnValue(
of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync)
);

oauthEvents$.next(new OAuthErrorEvent('token_refresh_error', { reason: 'error' }, {}));

Expand All @@ -408,8 +432,12 @@ describe('RedirectAuthService', () => {
});

it('should logout user if discovery_document_load_error is emitted because of clock out of sync', () => {
const expectedErrorMessage = new Error('OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z');
timeSyncServiceSpy.checkTimeSync.and.returnValue(of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync));
const expectedErrorMessage = new Error(
'OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z'
);
timeSyncServiceSpy.checkTimeSync.and.returnValue(
of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync)
);

oauthEvents$.next(new OAuthErrorEvent('discovery_document_load_error', { reason: 'error' }, {}));

Expand All @@ -418,8 +446,12 @@ describe('RedirectAuthService', () => {
});

it('should logout user if code_error is emitted because of clock out of sync', () => {
const expectedErrorMessage = new Error('OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z');
timeSyncServiceSpy.checkTimeSync.and.returnValue(of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync));
const expectedErrorMessage = new Error(
'OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z'
);
timeSyncServiceSpy.checkTimeSync.and.returnValue(
of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync)
);

oauthEvents$.next(new OAuthErrorEvent('code_error', { reason: 'error' }, {}));

Expand All @@ -428,8 +460,12 @@ describe('RedirectAuthService', () => {
});

it('should logout user if discovery_document_validation_error is emitted because of clock out of sync', () => {
const expectedErrorMessage = new Error('OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z');
timeSyncServiceSpy.checkTimeSync.and.returnValue(of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync));
const expectedErrorMessage = new Error(
'OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z'
);
timeSyncServiceSpy.checkTimeSync.and.returnValue(
of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync)
);

oauthEvents$.next(new OAuthErrorEvent('discovery_document_validation_error', { reason: 'error' }, {}));

Expand All @@ -438,8 +474,12 @@ describe('RedirectAuthService', () => {
});

it('should logout user if jwks_load_error is emitted because of clock out of sync', () => {
const expectedErrorMessage = new Error('OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z');
timeSyncServiceSpy.checkTimeSync.and.returnValue(of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync));
const expectedErrorMessage = new Error(
'OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z'
);
timeSyncServiceSpy.checkTimeSync.and.returnValue(
of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync)
);

oauthEvents$.next(new OAuthErrorEvent('jwks_load_error', { reason: 'error' }, {}));

Expand All @@ -448,8 +488,12 @@ describe('RedirectAuthService', () => {
});

it('should logout user if silent_refresh_error is emitted because of clock out of sync', () => {
const expectedErrorMessage = new Error('OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z');
timeSyncServiceSpy.checkTimeSync.and.returnValue(of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync));
const expectedErrorMessage = new Error(
'OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z'
);
timeSyncServiceSpy.checkTimeSync.and.returnValue(
of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync)
);

oauthEvents$.next(new OAuthErrorEvent('silent_refresh_error', { reason: 'error' }, {}));

Expand All @@ -458,8 +502,12 @@ describe('RedirectAuthService', () => {
});

it('should logout user if user_profile_load_error is emitted because of clock out of sync', () => {
const expectedErrorMessage = new Error('OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z');
timeSyncServiceSpy.checkTimeSync.and.returnValue(of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync));
const expectedErrorMessage = new Error(
'OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z'
);
timeSyncServiceSpy.checkTimeSync.and.returnValue(
of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync)
);

oauthEvents$.next(new OAuthErrorEvent('user_profile_load_error', { reason: 'error' }, {}));

Expand All @@ -468,8 +516,12 @@ describe('RedirectAuthService', () => {
});

it('should logout user if token_error is emitted because of clock out of sync', () => {
const expectedErrorMessage = new Error('OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z');
timeSyncServiceSpy.checkTimeSync.and.returnValue(of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync));
const expectedErrorMessage = new Error(
'OAuth error occurred due to local machine clock 2024-10-10T22:00:18.621Z being out of sync with server time 2024-10-10T22:10:53.000Z'
);
timeSyncServiceSpy.checkTimeSync.and.returnValue(
of({ outOfSync: true, localDateTimeISO: '2024-10-10T22:00:18.621Z', serverDateTimeISO: '2024-10-10T22:10:53.000Z' } as TimeSync)
);

oauthEvents$.next(new OAuthErrorEvent('token_error', { reason: 'error' }, {}));

Expand All @@ -479,7 +531,7 @@ describe('RedirectAuthService', () => {

it('should onLogout$ be emitted when logout event occur', () => {
let expectedLogoutIsEmitted = false;
service.onLogout$.subscribe(() => expectedLogoutIsEmitted = true);
service.onLogout$.subscribe(() => (expectedLogoutIsEmitted = true));

oauthEvents$.next(new OAuthInfoEvent('logout'));

Expand Down
Loading

0 comments on commit f532ae5

Please sign in to comment.