Skip to content

Commit cc6b159

Browse files
committed
Update code to use GET request. Cleanup lint errors & add in basic TypeDocs
1 parent 617c7d8 commit cc6b159

7 files changed

Lines changed: 43 additions & 33 deletions

File tree

src/app/core/xsrf/browser-xsrf.service.spec.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { BrowserXSRFService } from './browser-xsrf.service';
21
import { HttpClient } from '@angular/common/http';
3-
import { RESTURLCombiner } from '../url-combiner/rest-url-combiner';
2+
import {
3+
HttpClientTestingModule,
4+
HttpTestingController,
5+
} from '@angular/common/http/testing';
46
import { TestBed } from '@angular/core/testing';
5-
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
7+
8+
import { RESTURLCombiner } from '../url-combiner/rest-url-combiner';
9+
import { BrowserXSRFService } from './browser-xsrf.service';
610

711
describe(`BrowserXSRFService`, () => {
812
let service: BrowserXSRFService;
@@ -14,28 +18,30 @@ describe(`BrowserXSRFService`, () => {
1418
beforeEach(() => {
1519
TestBed.configureTestingModule({
1620
imports: [ HttpClientTestingModule ],
17-
providers: [ BrowserXSRFService ]
21+
providers: [ BrowserXSRFService ],
1822
});
1923
httpClient = TestBed.inject(HttpClient);
2024
httpTestingController = TestBed.inject(HttpTestingController);
2125
service = TestBed.inject(BrowserXSRFService);
2226
});
2327

2428
describe(`initXSRFToken`, () => {
25-
it(`should perform a POST to the csrf endpoint`, () => {
29+
it(`should perform a GET to the csrf endpoint`, (done: DoneFn) => {
2630
service.initXSRFToken(httpClient)();
2731

2832
const req = httpTestingController.expectOne({
2933
url: endpointURL,
30-
method: 'POST'
34+
method: 'GET',
3135
});
3236

3337
req.flush({});
3438
httpTestingController.verify();
39+
expect().nothing();
40+
done();
3541
});
3642

37-
describe(`when the POST succeeds`, () => {
38-
it(`should set tokenInitialized$ to true`, () => {
43+
describe(`when the GET succeeds`, () => {
44+
it(`should set tokenInitialized$ to true`, (done: DoneFn) => {
3945
service.initXSRFToken(httpClient)();
4046

4147
const req = httpTestingController.expectOne(endpointURL);
@@ -44,19 +50,7 @@ describe(`BrowserXSRFService`, () => {
4450
httpTestingController.verify();
4551

4652
expect(service.tokenInitialized$.getValue()).toBeTrue();
47-
});
48-
});
49-
50-
describe(`when the POST fails`, () => {
51-
it(`should set tokenInitialized$ to true`, () => {
52-
service.initXSRFToken(httpClient)();
53-
54-
const req = httpTestingController.expectOne(endpointURL);
55-
56-
req.error(new ErrorEvent('415'));
57-
httpTestingController.verify();
58-
59-
expect(service.tokenInitialized$.getValue()).toBeTrue();
53+
done();
6054
});
6155
});
6256

src/app/core/xsrf/browser-xsrf.service.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
import { take } from 'rxjs/operators';
4+
35
import { RESTURLCombiner } from '../url-combiner/rest-url-combiner';
4-
import { take, catchError } from 'rxjs/operators';
5-
import { of as observableOf } from 'rxjs';
66
import { XSRFService } from './xsrf.service';
77

8+
/**
9+
* Browser (CSR) Service to obtain a new CSRF/XSRF token when needed by our RequestService
10+
* to perform a modify request (e.g. POST/PUT/DELETE).
11+
* NOTE: This is primarily necessary before the *first* modifying request, as the CSRF
12+
* token may not yet be initialized.
13+
*/
814
@Injectable()
915
export class BrowserXSRFService extends XSRFService {
1016
initXSRFToken(httpClient: HttpClient): () => Promise<any> {
11-
return () => new Promise((resolve) => {
12-
httpClient.post(new RESTURLCombiner('/security/csrf').toString(), undefined).pipe(
13-
// errors are to be expected if the token and the cookie don't match, that's what we're
14-
// trying to fix for future requests, so just emit any observable to end up in the
15-
// subscribe
16-
catchError(() => observableOf(null)),
17+
return () => new Promise<void>((resolve) => {
18+
// Force a new token to be created by calling the CSRF endpoint
19+
httpClient.get(new RESTURLCombiner('/security/csrf').toString(), undefined).pipe(
1720
take(1),
1821
).subscribe(() => {
22+
// Once token is returned, set tokenInitialized to true.
1923
this.tokenInitialized$.next(true);
2024
});
2125

src/app/core/xsrf/server-xsrf.service.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { ServerXSRFService } from './server-xsrf.service';
21
import { HttpClient } from '@angular/common/http';
32

3+
import { ServerXSRFService } from './server-xsrf.service';
4+
45
describe(`ServerXSRFService`, () => {
56
let service: ServerXSRFService;
67
let httpClient: HttpClient;

src/app/core/xsrf/server-xsrf.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
34
import { XSRFService } from './xsrf.service';
45

6+
/**
7+
* Server (SSR) Service to obtain a new CSRF/XSRF token. Because SSR only triggers GET
8+
* requests a CSRF token is never needed.
9+
*/
510
@Injectable()
611
export class ServerXSRFService extends XSRFService {
712
initXSRFToken(httpClient: HttpClient): () => Promise<any> {
8-
return () => new Promise((resolve) => {
13+
return () => new Promise<void>((resolve) => {
914
// return immediately, and keep tokenInitialized$ false. The server side can make only GET
1015
// requests, since it can never get a valid XSRF cookie
1116
resolve();

src/app/core/xsrf/xsrf.service.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { XSRFService } from './xsrf.service';
21
import { HttpClient } from '@angular/common/http';
32

3+
import { XSRFService } from './xsrf.service';
4+
45
class XSRFServiceImpl extends XSRFService {
56
initXSRFToken(httpClient: HttpClient): () => Promise<any> {
67
return () => null;

src/app/core/xsrf/xsrf.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { HttpClient } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
33
import { BehaviorSubject } from 'rxjs';
44

5+
/**
6+
* Abstract CSRF/XSRF Service used to track whether a CSRF token has been received
7+
* from the DSpace REST API. Once it is received, the "tokenInitialized$" flag will
8+
* be set to "true".
9+
*/
510
@Injectable()
611
export abstract class XSRFService {
712
public tokenInitialized$: BehaviorSubject<boolean> = new BehaviorSubject(false);

src/modules/app/browser-app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import { MissingTranslationHelper } from '../../app/shared/translate/missing-tra
5959
import { GoogleAnalyticsService } from '../../app/statistics/google-analytics.service';
6060
import { SubmissionService } from '../../app/submission/submission.service';
6161
import { TranslateBrowserLoader } from '../../ngx-translate-loaders/translate-browser.loader';
62-
import { BrowserInitService } from './browser-init.service'
62+
import { BrowserInitService } from './browser-init.service';
6363

6464
export const REQ_KEY = makeStateKey<string>('req');
6565

0 commit comments

Comments
 (0)