Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exui-2655-fortify scan issue #1323

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { LoggerModule, NGXLogger, NgxLoggerLevel } from 'ngx-logger';
import { environment } from '../environments/environment';
import { EnvironmentConfig } from '../models/environmentConfig.model';
import { DefaultErrorHandler } from '../shared/errorHandler/defaultErrorHandler';
import { CryptoWrapper } from '../shared/services/cryptoWrapper';
import { JwtDecodeWrapper } from '../shared/services/jwtDecodeWrapper';
import { LoggerService } from '../shared/services/logger.service';
import { UserService } from '../user-profile/services/user.service';
Expand Down Expand Up @@ -105,7 +104,7 @@ export function launchDarklyClientIdFactory(envConfig: EnvironmentConfig): strin
FeatureToggleGuard,
{ provide: RouterStateSerializer, useClass: CustomSerializer },
UserService, { provide: ErrorHandler, useClass: DefaultErrorHandler },
CryptoWrapper, JwtDecodeWrapper, LoggerService, JurisdictionService,
JwtDecodeWrapper, LoggerService, JurisdictionService,
{ provide: FeatureToggleService, useClass: LaunchDarklyService },
{ provide: APP_INITIALIZER, useFactory: initApplication, deps: [Store, EnvironmentService], multi: true }
],
Expand Down
3 changes: 1 addition & 2 deletions src/organisation/organisation.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { RxReactiveFormsModule } from '@rxweb/reactive-form-validators';
import { LoggerModule, NgxLoggerLevel } from 'ngx-logger';
import { DefaultErrorHandler } from '../shared/errorHandler/defaultErrorHandler';
import { AbstractAppInsights, AppInsightsWrapper } from '../shared/services/appInsightsWrapper';
import { CryptoWrapper } from '../shared/services/cryptoWrapper';
import { JwtDecodeWrapper } from '../shared/services/jwtDecodeWrapper';
import { LoggerService } from '../shared/services/logger.service';
import { MonitoringService } from '../shared/services/monitoring.service';
Expand All @@ -47,7 +46,7 @@ import { effects, reducers } from './store';
declarations: [...fromContainers.containers, ...fromComponent.components],
providers: [...fromServices.services, OrganisationGuard,
{ provide: AbstractAppInsights, useClass: AppInsightsWrapper },
CryptoWrapper, JwtDecodeWrapper, MonitoringService, LoggerService,
JwtDecodeWrapper, MonitoringService, LoggerService,
{ provide: ErrorHandler, useClass: DefaultErrorHandler }]
})

Expand Down
14 changes: 0 additions & 14 deletions src/shared/services/CryptoWrapper.spec.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/shared/services/cryptoWrapper.ts

This file was deleted.

46 changes: 29 additions & 17 deletions src/shared/services/logger.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,73 @@
import { LoggerService } from './logger.service';
import { UserInterface } from 'src/user-profile/models/user.model';

const userDetails: UserInterface = {
email: '[email protected]',
orgId: '12345',
roles: ['pui-case-manager', 'pui-user-manager', 'pui-finance-manager', 'pui-organisation-manager'],
userId: '1',
sessionTimeout: {
idleModalDisplayTime: 10,
totalIdleTime: 50
}
};

describe('Logger service', () => {
const mockedMonitoringService = jasmine.createSpyObj('mockedMonitoringService', ['logEvent', 'logException']);
const mockedNgxLogger = jasmine.createSpyObj('mockedNgxLogger', ['trace', 'debug', 'info',
'log', 'warn', 'error', 'fatal']);
const mockedCookieService = jasmine.createSpyObj('mockedCookieService', ['get']);
const mockedCryptoWrapper = jasmine.createSpyObj('mockedCryptoWrapper', ['encrypt', 'decrypt']);
const mockJwtDecodeWrapper = jasmine.createSpyObj('mockJwtDecodeWrapper', ['decode']);
const mockedSessionStorageService = jasmine.createSpyObj('mockedSessionStorageService', ['getItem']);

it('should be Truthy', () => {
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedCookieService,
mockedCryptoWrapper, mockJwtDecodeWrapper);
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedSessionStorageService);
expect(service).toBeTruthy();
});

it('should be able to call info', () => {
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedCookieService,
mockedCryptoWrapper, mockJwtDecodeWrapper);
mockedSessionStorageService.getItem.and.returnValue(JSON.stringify(userDetails));
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedSessionStorageService);
service.info('message');
expect(mockedMonitoringService.logEvent).toHaveBeenCalled();
expect(mockedNgxLogger.info).toHaveBeenCalled();
});

it('should be able to call warn', () => {
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedCookieService,
mockedCryptoWrapper, mockJwtDecodeWrapper);
mockedSessionStorageService.getItem.and.returnValue(JSON.stringify(userDetails));
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedSessionStorageService);
service.warn('message');
expect(mockedMonitoringService.logEvent).toHaveBeenCalled();
expect(mockedNgxLogger.warn).toHaveBeenCalled();
});

it('should be able to call error', () => {
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedCookieService,
mockedCryptoWrapper, mockJwtDecodeWrapper);
mockedSessionStorageService.getItem.and.returnValue(JSON.stringify(userDetails));
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedSessionStorageService);
service.error('message');
expect(mockedMonitoringService.logException).toHaveBeenCalled();
expect(mockedNgxLogger.error).toHaveBeenCalled();
expect(mockedSessionStorageService.getItem).toHaveBeenCalled();
});

it('should be able to call fatal', () => {
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedCookieService,
mockedCryptoWrapper, mockJwtDecodeWrapper);
mockedSessionStorageService.getItem.and.returnValue(JSON.stringify(userDetails));
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedSessionStorageService);
service.fatal('message');
expect(mockedMonitoringService.logException).toHaveBeenCalled();
expect(mockedNgxLogger.fatal).toHaveBeenCalled();
expect(mockedSessionStorageService.getItem).toHaveBeenCalled();
});

it('should be able to call debug', () => {
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedCookieService,
mockedCryptoWrapper, mockJwtDecodeWrapper);
mockedSessionStorageService.getItem.and.returnValue(JSON.stringify(userDetails));
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedSessionStorageService);
service.debug('message');
expect(mockedMonitoringService.logEvent).toHaveBeenCalled();
expect(mockedSessionStorageService.getItem).toHaveBeenCalled();
});

it('should be able to call trace', () => {
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedCookieService,
mockedCryptoWrapper, mockJwtDecodeWrapper);
mockedSessionStorageService.getItem.and.returnValue(JSON.stringify(userDetails));
const service = new LoggerService(mockedMonitoringService, mockedNgxLogger, mockedSessionStorageService);
service.trace('message');
expect(mockedMonitoringService.logEvent).toHaveBeenCalled();
expect(mockedNgxLogger.trace).toHaveBeenCalled();
Expand Down
21 changes: 8 additions & 13 deletions src/shared/services/logger.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Injectable } from '@angular/core';
import { CookieService } from 'ngx-cookie';
import { NGXLogger } from 'ngx-logger';
import { CryptoWrapper } from './cryptoWrapper';
import { JwtDecodeWrapper } from './jwtDecodeWrapper';
import { MonitoringService } from './monitoring.service';
import { environment } from 'src/environments/environment';
import { SessionStorageService } from '../services/session-storage.service';
import { UserInterface } from 'src/user-profile/models/user.model';

export interface ILoggerService {
trace(message: any, ...additional: any[]): void;
Expand All @@ -23,9 +22,7 @@ export class LoggerService implements ILoggerService {
public COOKIE_KEYS;
constructor(private readonly monitoringService: MonitoringService,
private readonly ngxLogger: NGXLogger,
private readonly cookieService: CookieService,
private readonly cryptoWrapper: CryptoWrapper,
private readonly jwtDecodeWrapper: JwtDecodeWrapper) {
private readonly sessionStorageService: SessionStorageService) {
this.COOKIE_KEYS = {
TOKEN: environment.cookies.token,
USER: environment.cookies.userId
Expand Down Expand Up @@ -84,13 +81,11 @@ export class LoggerService implements ILoggerService {
}

public getMessage(message: any): string {
const jwt = this.cookieService.get(this.COOKIE_KEYS.TOKEN);
if (jwt) {
const jwtData = this.jwtDecodeWrapper.decode(jwt);
if (jwtData) {
const userIdEncrypted = this.cryptoWrapper.encrypt(jwtData.sub);
return `User - ${userIdEncrypted.toString()}, Message - ${message}, Timestamp - ${Date.now()}`;
}
const userInfoStr = this.sessionStorageService.getItem('userDetails');
const userInfo: UserInterface = JSON.parse(userInfoStr);
if (userInfo?.userId) {
const userId = userInfo.userId;
return `User - ${userId.toString()}, Message - ${message}, Timestamp - ${Date.now()}`;
}
return `Message - ${message}, Timestamp - ${Date.now()}`;
}
Expand Down
5 changes: 4 additions & 1 deletion src/user-profile/store/effects/user-profile.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UserInterface } from '../../models/user.model';
import { UserService } from '../../services/user.service';
import * as authActions from '../actions';
import { AuthActionTypes } from '../actions/';
import { SessionStorageService } from '../../../shared/services/session-storage.service';

@Injectable()
export class UserProfileEffects {
Expand All @@ -19,7 +20,8 @@ export class UserProfileEffects {
private readonly userService: UserService,
private readonly loggerService: LoggerService,
private readonly authService: UserService,
private readonly acceptTcService: AcceptTcService
private readonly acceptTcService: AcceptTcService,
private readonly sessionStorageService: SessionStorageService
) {}

public getUser$ = createEffect(() => this.actions$.pipe(
Expand All @@ -28,6 +30,7 @@ export class UserProfileEffects {
return this.userService.getUserDetails()
.pipe(
map((userDetails: UserInterface) => {
this.sessionStorageService.setItem('userDetails', JSON.stringify(userDetails));
return new authActions.GetUserDetailsSuccess(userDetails);
}),
catchError((error: HttpErrorResponse) => {
Expand Down
7 changes: 7 additions & 0 deletions src/user-profile/store/effects/user.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
LoadHasAcceptedTCFail, LoadHasAcceptedTCSuccess
} from '../actions';
import * as fromUserEffects from './user-profile.effects';
import { SessionStorageService } from '../../../shared/services/session-storage.service';

describe('User Profile Effects', () => {
let actions$;
Expand All @@ -30,6 +31,7 @@ describe('User Profile Effects', () => {
]);

const mockedLoggerService = jasmine.createSpyObj('mockedLoggerService', ['trace', 'info', 'debug', 'log', 'warn', 'error', 'fatal']);
const mockedSessionStorageService = jasmine.createSpyObj('mockedSessionStorageService', ['setItem']);

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -47,6 +49,10 @@ describe('User Profile Effects', () => {
provide: LoggerService,
useValue: mockedLoggerService
},
{
provide: SessionStorageService,
useValue: mockedSessionStorageService
},
fromUserEffects.UserProfileEffects,
provideMockActions(() => actions$)
]
Expand Down Expand Up @@ -78,6 +84,7 @@ describe('User Profile Effects', () => {
actions$ = hot('-a', { a: action });
const expected = cold('-b', { b: completion });
expect(effects.getUser$).toBeObservable(expected);
expect(mockedSessionStorageService.setItem).toHaveBeenCalledWith('userDetails', JSON.stringify(returnValue));
}));
});

Expand Down