mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
[DURACOM-309] add new orejime cookie for correlation id
This commit is contained in:

committed by
FrancescoMolinaro

parent
0180b633e2
commit
9d3b298f1a
@@ -10,12 +10,15 @@ import {
|
|||||||
import { TestBed } from '@angular/core/testing';
|
import { TestBed } from '@angular/core/testing';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { StoreModule } from '@ngrx/store';
|
import { StoreModule } from '@ngrx/store';
|
||||||
|
import { of } from 'rxjs';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
appReducers,
|
appReducers,
|
||||||
storeModuleConfig,
|
storeModuleConfig,
|
||||||
} from '../../app.reducer';
|
} from '../../app.reducer';
|
||||||
import { CorrelationIdService } from '../../correlation-id/correlation-id.service';
|
import { CorrelationIdService } from '../../correlation-id/correlation-id.service';
|
||||||
|
import { OrejimeService } from '../../shared/cookies/orejime.service';
|
||||||
|
import { CORRELATION_ID_OREJIME_KEY } from '../../shared/cookies/orejime-configuration';
|
||||||
import { CookieServiceMock } from '../../shared/mocks/cookie.service.mock';
|
import { CookieServiceMock } from '../../shared/mocks/cookie.service.mock';
|
||||||
import { RouterStub } from '../../shared/testing/router.stub';
|
import { RouterStub } from '../../shared/testing/router.stub';
|
||||||
import { RestRequestMethod } from '../data/rest-request-method';
|
import { RestRequestMethod } from '../data/rest-request-method';
|
||||||
@@ -40,6 +43,8 @@ describe('LogInterceptor', () => {
|
|||||||
const mockStatusCode = 200;
|
const mockStatusCode = 200;
|
||||||
const mockStatusText = 'SUCCESS';
|
const mockStatusText = 'SUCCESS';
|
||||||
|
|
||||||
|
const mockOrejimeService = jasmine.createSpyObj('OrejimeService', ['getSavedPreferences']);
|
||||||
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
@@ -56,6 +61,7 @@ describe('LogInterceptor', () => {
|
|||||||
{ provide: Router, useValue: router },
|
{ provide: Router, useValue: router },
|
||||||
{ provide: CorrelationIdService, useClass: CorrelationIdService },
|
{ provide: CorrelationIdService, useClass: CorrelationIdService },
|
||||||
{ provide: UUIDService, useClass: UUIDService },
|
{ provide: UUIDService, useClass: UUIDService },
|
||||||
|
{ provide: OrejimeService, useValue: mockOrejimeService },
|
||||||
provideHttpClient(withInterceptorsFromDi()),
|
provideHttpClient(withInterceptorsFromDi()),
|
||||||
provideHttpClientTesting(),
|
provideHttpClientTesting(),
|
||||||
],
|
],
|
||||||
@@ -71,7 +77,9 @@ describe('LogInterceptor', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('headers should be set', (done) => {
|
it('headers should be set when cookie is accepted', (done) => {
|
||||||
|
mockOrejimeService.getSavedPreferences.and.returnValue(of({ [CORRELATION_ID_OREJIME_KEY]: true }));
|
||||||
|
|
||||||
service.request(RestRequestMethod.POST, 'server/api/core/items', 'test', { withCredentials: false }).subscribe((response) => {
|
service.request(RestRequestMethod.POST, 'server/api/core/items', 'test', { withCredentials: false }).subscribe((response) => {
|
||||||
expect(response).toBeTruthy();
|
expect(response).toBeTruthy();
|
||||||
done();
|
done();
|
||||||
@@ -83,7 +91,23 @@ describe('LogInterceptor', () => {
|
|||||||
expect(httpRequest.request.headers.has('X-REFERRER')).toBeTrue();
|
expect(httpRequest.request.headers.has('X-REFERRER')).toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('headers should have the right values', (done) => {
|
it('headers should not be set when cookie is declined', (done) => {
|
||||||
|
mockOrejimeService.getSavedPreferences.and.returnValue(of({ [CORRELATION_ID_OREJIME_KEY]: false }));
|
||||||
|
|
||||||
|
service.request(RestRequestMethod.POST, 'server/api/core/items', 'test', { withCredentials: false }).subscribe((response) => {
|
||||||
|
expect(response).toBeTruthy();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
const httpRequest = httpMock.expectOne('server/api/core/items');
|
||||||
|
httpRequest.flush(mockPayload, { status: mockStatusCode, statusText: mockStatusText });
|
||||||
|
expect(httpRequest.request.headers.has('X-CORRELATION-ID')).toBeFalse();
|
||||||
|
expect(httpRequest.request.headers.has('X-REFERRER')).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('headers should have the right values when cookie is accepted', (done) => {
|
||||||
|
mockOrejimeService.getSavedPreferences.and.returnValue(of({ [CORRELATION_ID_OREJIME_KEY]: true }));
|
||||||
|
|
||||||
service.request(RestRequestMethod.POST, 'server/api/core/items', 'test', { withCredentials: false }).subscribe((response) => {
|
service.request(RestRequestMethod.POST, 'server/api/core/items', 'test', { withCredentials: false }).subscribe((response) => {
|
||||||
expect(response).toBeTruthy();
|
expect(response).toBeTruthy();
|
||||||
done();
|
done();
|
||||||
|
@@ -7,9 +7,15 @@ import {
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
import { switchMap } from 'rxjs/operators';
|
||||||
|
|
||||||
import { CorrelationIdService } from '../../correlation-id/correlation-id.service';
|
import { CorrelationIdService } from '../../correlation-id/correlation-id.service';
|
||||||
import { hasValue } from '../../shared/empty.util';
|
import { OrejimeService } from '../../shared/cookies/orejime.service';
|
||||||
|
import { CORRELATION_ID_OREJIME_KEY } from '../../shared/cookies/orejime-configuration';
|
||||||
|
import {
|
||||||
|
hasValue,
|
||||||
|
isEmpty,
|
||||||
|
} from '../../shared/empty.util';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log Interceptor intercepting Http Requests & Responses to
|
* Log Interceptor intercepting Http Requests & Responses to
|
||||||
@@ -19,22 +25,37 @@ import { hasValue } from '../../shared/empty.util';
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class LogInterceptor implements HttpInterceptor {
|
export class LogInterceptor implements HttpInterceptor {
|
||||||
|
|
||||||
constructor(private cidService: CorrelationIdService, private router: Router) {}
|
constructor(
|
||||||
|
private cidService: CorrelationIdService,
|
||||||
|
private router: Router,
|
||||||
|
private orejimeService: OrejimeService,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||||
|
return this.orejimeService.getSavedPreferences().pipe(
|
||||||
|
switchMap(preferences => {
|
||||||
|
// Check if the user has declined correlation id tracking
|
||||||
|
const correlationDeclined =
|
||||||
|
isEmpty(preferences) ||
|
||||||
|
isEmpty(preferences[CORRELATION_ID_OREJIME_KEY]) ||
|
||||||
|
!preferences[CORRELATION_ID_OREJIME_KEY];
|
||||||
|
|
||||||
// Get the correlation id for the user from the store
|
// Add headers from the intercepted request
|
||||||
const correlationId = this.cidService.getCorrelationId();
|
let headers = request.headers;
|
||||||
|
if (!correlationDeclined) {
|
||||||
|
// Get the correlation id for the user from the store
|
||||||
|
const correlationId = this.cidService.getCorrelationId();
|
||||||
|
if (hasValue(correlationId)) {
|
||||||
|
headers = headers.append('X-CORRELATION-ID', correlationId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
headers = headers.append('X-REFERRER', this.router.url);
|
||||||
|
|
||||||
// Add headers from the intercepted request
|
// Add new headers to the intercepted request
|
||||||
let headers = request.headers;
|
request = request.clone({ withCredentials: true, headers: headers });
|
||||||
if (hasValue(correlationId)) {
|
return next.handle(request);
|
||||||
headers = headers.append('X-CORRELATION-ID', correlationId);
|
}),
|
||||||
}
|
);
|
||||||
headers = headers.append('X-REFERRER', this.router.url);
|
|
||||||
|
|
||||||
// Add new headers to the intercepted request
|
|
||||||
request = request.clone({ withCredentials: true, headers: headers });
|
|
||||||
return next.handle(request);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -12,6 +12,8 @@ import { isEmpty } from '../shared/empty.util';
|
|||||||
import { SetCorrelationIdAction } from './correlation-id.actions';
|
import { SetCorrelationIdAction } from './correlation-id.actions';
|
||||||
import { correlationIdSelector } from './correlation-id.selector';
|
import { correlationIdSelector } from './correlation-id.selector';
|
||||||
|
|
||||||
|
export const CORRELATION_ID_COOKIE = 'dsCorrelationId';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service to manage the correlation id, an id used to give context to server side logs
|
* Service to manage the correlation id, an id used to give context to server side logs
|
||||||
*/
|
*/
|
||||||
|
@@ -9,6 +9,7 @@ import {
|
|||||||
} from '../../core/google-recaptcha/google-recaptcha.service';
|
} from '../../core/google-recaptcha/google-recaptcha.service';
|
||||||
import { LANG_COOKIE } from '../../core/locale/locale.service';
|
import { LANG_COOKIE } from '../../core/locale/locale.service';
|
||||||
import { NativeWindowRef } from '../../core/services/window.service';
|
import { NativeWindowRef } from '../../core/services/window.service';
|
||||||
|
import { CORRELATION_ID_COOKIE } from '../../correlation-id/correlation-id.service';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cookie for has_agreed_end_user
|
* Cookie for has_agreed_end_user
|
||||||
@@ -23,6 +24,8 @@ export const MATOMO_OREJIME_KEY = 'matomo';
|
|||||||
|
|
||||||
export const MATOMO_COOKIE = 'dsMatomo';
|
export const MATOMO_COOKIE = 'dsMatomo';
|
||||||
|
|
||||||
|
export const CORRELATION_ID_OREJIME_KEY = 'correlation-id';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Orejime configuration
|
* Orejime configuration
|
||||||
* For more information see https://github.com/empreinte-digitale/orejime
|
* For more information see https://github.com/empreinte-digitale/orejime
|
||||||
@@ -141,6 +144,14 @@ export function getOrejimeConfiguration(_window: NativeWindowRef): any {
|
|||||||
HAS_AGREED_END_USER,
|
HAS_AGREED_END_USER,
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: CORRELATION_ID_OREJIME_KEY,
|
||||||
|
purposes: ['statistical'],
|
||||||
|
required: false,
|
||||||
|
cookies: [
|
||||||
|
CORRELATION_ID_COOKIE,
|
||||||
|
],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: MATOMO_OREJIME_KEY,
|
name: MATOMO_OREJIME_KEY,
|
||||||
purposes: ['statistical'],
|
purposes: ['statistical'],
|
||||||
|
@@ -1637,6 +1637,10 @@
|
|||||||
|
|
||||||
"cookies.consent.app.description.authentication": "Required for signing you in",
|
"cookies.consent.app.description.authentication": "Required for signing you in",
|
||||||
|
|
||||||
|
"cookies.consent.app.title.correlation-id": "Correlation ID",
|
||||||
|
|
||||||
|
"cookies.consent.app.description.correlation-id": "Allow us to track your session for debugging purposes",
|
||||||
|
|
||||||
"cookies.consent.app.title.preferences": "Preferences",
|
"cookies.consent.app.title.preferences": "Preferences",
|
||||||
|
|
||||||
"cookies.consent.app.description.preferences": "Required for saving your preferences",
|
"cookies.consent.app.description.preferences": "Required for saving your preferences",
|
||||||
|
@@ -2666,6 +2666,12 @@
|
|||||||
// "cookies.consent.app.description.authentication": "Required for signing you in",
|
// "cookies.consent.app.description.authentication": "Required for signing you in",
|
||||||
"cookies.consent.app.description.authentication": "Necessario per l'accesso",
|
"cookies.consent.app.description.authentication": "Necessario per l'accesso",
|
||||||
|
|
||||||
|
// "cookies.consent.app.title.correlation-id": "Correlation ID",
|
||||||
|
"cookies.consent.app.title.correlation-id": "ID di correlazione",
|
||||||
|
|
||||||
|
// "cookies.consent.app.description.correlation-id": "Allow us to track your session for debugging purposes",
|
||||||
|
"cookies.consent.app.description.correlation-id": "Consentici di tracciare la tua sessione per scopi di debug",
|
||||||
|
|
||||||
// "cookies.consent.app.title.preferences": "Preferences",
|
// "cookies.consent.app.title.preferences": "Preferences",
|
||||||
"cookies.consent.app.title.preferences": "Preferenze",
|
"cookies.consent.app.title.preferences": "Preferenze",
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user