Skip to content

Commit 2500195

Browse files
atarix83Andrea Barbasso
authored andcommitted
[DSC-2142] fix creation of short-lived token during SSR by initializing XSRF token
1 parent e1d6713 commit 2500195

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/app/core/auth/server-auth-request.service.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
XSRF_REQUEST_HEADER,
1010
XSRF_RESPONSE_HEADER
1111
} from '../xsrf/xsrf.constants';
12+
import { XSRFService } from '../xsrf/xsrf.service';
13+
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
1214

1315
describe(`ServerAuthRequestService`, () => {
1416
let href: string;
@@ -17,6 +19,7 @@ describe(`ServerAuthRequestService`, () => {
1719
let httpClient: HttpClient;
1820
let httpResponse: HttpResponse<any>;
1921
let halService: HALEndpointService;
22+
let xsrfService: XSRFService;
2023
const mockToken = 'mock-token';
2124

2225
beforeEach(() => {
@@ -37,7 +40,10 @@ describe(`ServerAuthRequestService`, () => {
3740
halService = jasmine.createSpyObj('halService', {
3841
'getRootHref': '/api'
3942
});
40-
service = new ServerAuthRequestService(halService, requestService, null, httpClient);
43+
xsrfService = jasmine.createSpyObj('xsrfService', null, {
44+
'tokenInitialized$': new BehaviorSubject(false),
45+
});
46+
service = new ServerAuthRequestService(halService, requestService, null, httpClient, xsrfService);
4147
});
4248

4349
describe(`createShortLivedTokenRequest`, () => {

src/app/core/auth/server-auth-request.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import {
1414
XSRF_RESPONSE_HEADER,
1515
DSPACE_XSRF_COOKIE
1616
} from '../xsrf/xsrf.constants';
17-
import { map } from 'rxjs/operators';
17+
import { map, tap } from 'rxjs/operators';
1818
import { Observable } from 'rxjs';
19+
import { XSRFService } from '../xsrf/xsrf.service';
1920

2021
/**
2122
* Server side version of the service to send authentication requests
@@ -28,6 +29,7 @@ export class ServerAuthRequestService extends AuthRequestService {
2829
requestService: RequestService,
2930
rdbService: RemoteDataBuildService,
3031
protected httpClient: HttpClient,
32+
protected xsrfService: XSRFService
3133
) {
3234
super(halService, requestService, rdbService);
3335
}
@@ -43,6 +45,7 @@ export class ServerAuthRequestService extends AuthRequestService {
4345
return this.httpClient.get(this.halService.getRootHref(), { observe: 'response' }).pipe(
4446
// retrieve the XSRF token from the response header
4547
map((response: HttpResponse<any>) => response.headers.get(XSRF_RESPONSE_HEADER)),
48+
tap(() => this.xsrfService.tokenInitialized$.next(true)),
4649
// Use that token to create an HttpHeaders object
4750
map((xsrfToken: string) => new HttpHeaders()
4851
.set('Content-Type', 'application/json; charset=utf-8')

0 commit comments

Comments
 (0)