diff --git a/AdminPortal/src/app/app.module.ts b/AdminPortal/src/app/app.module.ts index a2881ba1..7dda9817 100644 --- a/AdminPortal/src/app/app.module.ts +++ b/AdminPortal/src/app/app.module.ts @@ -1,7 +1,7 @@ import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; -import { HttpModule } from '@angular/http'; +import { HttpModule, XSRFStrategy, CookieXSRFStrategy } from '@angular/http'; import { routing } from './app.routing'; @@ -19,6 +19,11 @@ import { AppointmentService } from './appointment.service'; +export function xsrfStrategyFactory() { + // Spring Security's CookieCsrfTokenRepository cookie and header names. + return new CookieXSRFStrategy('XSRF-TOKEN', 'X-XSRF-TOKEN'); +} + @NgModule({ declarations: [ AppComponent, @@ -38,7 +43,8 @@ import { AppointmentService } from './appointment.service'; providers: [ LoginService, UserService, - AppointmentService + AppointmentService, + { provide: XSRFStrategy, useFactory: xsrfStrategyFactory } ], bootstrap: [AppComponent] }) diff --git a/AdminPortal/src/app/appointment.service.spec.ts b/AdminPortal/src/app/appointment.service.spec.ts index 24ed2d35..10be7f15 100644 --- a/AdminPortal/src/app/appointment.service.spec.ts +++ b/AdminPortal/src/app/appointment.service.spec.ts @@ -1,16 +1,37 @@ -/* tslint:disable:no-unused-variable */ - -import { TestBed, async, inject } from '@angular/core/testing'; +import { TestBed, inject } from '@angular/core/testing'; +import { BaseRequestOptions, Http, RequestMethod } from '@angular/http'; +import { MockBackend, MockConnection } from '@angular/http/testing'; import { AppointmentService } from './appointment.service'; describe('Service: Appointment', () => { + let connections: MockConnection[]; + beforeEach(() => { TestBed.configureTestingModule({ - providers: [AppointmentService] + providers: [ + AppointmentService, + MockBackend, + BaseRequestOptions, + { + provide: Http, + useFactory: (b: MockBackend, options: BaseRequestOptions) => new Http(b, options), + deps: [MockBackend, BaseRequestOptions] + } + ] }); + connections = []; + TestBed.get(MockBackend).connections.subscribe((c: MockConnection) => connections.push(c)); }); - it('should ...', inject([AppointmentService], (service: AppointmentService) => { - expect(service).toBeTruthy(); + function expectCredentialedPost(url: string) { + expect(connections.length).toBe(1); + expect(connections[0].request.method).toBe(RequestMethod.Post); + expect(connections[0].request.url).toBe(url); + expect(connections[0].request.withCredentials).toBe(true); + } + + it('confirms an appointment with POST', inject([AppointmentService], (service: AppointmentService) => { + service.confirmAppointment(9).subscribe(); + expectCredentialedPost('http://localhost:8080/api/appointment/9/confirm'); })); }); diff --git a/AdminPortal/src/app/appointment.service.ts b/AdminPortal/src/app/appointment.service.ts index ad506808..2cfaeac8 100644 --- a/AdminPortal/src/app/appointment.service.ts +++ b/AdminPortal/src/app/appointment.service.ts @@ -14,7 +14,7 @@ export class AppointmentService { confirmAppointment(id: number) { let url = "http://localhost:8080/api/appointment/"+id+"/confirm"; - return this.http.get(url, { withCredentials: true }); + return this.http.post(url, null, { withCredentials: true }); } } diff --git a/AdminPortal/src/app/appointment/appointment.component.ts b/AdminPortal/src/app/appointment/appointment.component.ts index 5be37e91..ebbd4ed7 100644 --- a/AdminPortal/src/app/appointment/appointment.component.ts +++ b/AdminPortal/src/app/appointment/appointment.component.ts @@ -25,8 +25,10 @@ export class AppointmentComponent implements OnInit { } confirmAppointment(id: number) { - this.appointmentService.confirmAppointment(id).subscribe(); - location.reload(); + this.appointmentService.confirmAppointment(id).subscribe( + res => location.reload(), + err => console.log(err) + ); } ngOnInit() {} diff --git a/AdminPortal/src/app/login.service.spec.ts b/AdminPortal/src/app/login.service.spec.ts index 876a1be9..ff5343ff 100644 --- a/AdminPortal/src/app/login.service.spec.ts +++ b/AdminPortal/src/app/login.service.spec.ts @@ -1,16 +1,76 @@ -/* tslint:disable:no-unused-variable */ - -import { TestBed, async, inject } from '@angular/core/testing'; +import { TestBed, inject } from '@angular/core/testing'; +import { BaseRequestOptions, Http, Request, RequestMethod, RequestOptions, Response, ResponseOptions } from '@angular/http'; +import { MockBackend, MockConnection } from '@angular/http/testing'; import { LoginService } from './login.service'; +import { xsrfStrategyFactory } from './app.module'; describe('Service: Login', () => { + let backend: MockBackend; + let connections: MockConnection[]; + beforeEach(() => { TestBed.configureTestingModule({ - providers: [LoginService] + providers: [ + LoginService, + MockBackend, + BaseRequestOptions, + { + provide: Http, + useFactory: (b: MockBackend, options: BaseRequestOptions) => new Http(b, options), + deps: [MockBackend, BaseRequestOptions] + } + ] }); + backend = TestBed.get(MockBackend); + connections = []; + backend.connections.subscribe((c: MockConnection) => connections.push(c)); }); - it('should ...', inject([LoginService], (service: LoginService) => { - expect(service).toBeTruthy(); + it('fetches the login page for the CSRF cookie before posting credentials', inject([LoginService], (service: LoginService) => { + let done = false; + service.sendCredential('admin', 'password').subscribe(() => done = true); + + expect(connections.length).toBe(1); + expect(connections[0].request.method).toBe(RequestMethod.Get); + expect(connections[0].request.url).toBe('http://localhost:8080/index'); + expect(connections[0].request.withCredentials).toBe(true); + + connections[0].mockRespond(new Response(new ResponseOptions({ status: 200 }))); + + expect(connections.length).toBe(2); + const post = connections[1].request; + expect(post.method).toBe(RequestMethod.Post); + expect(post.url).toBe('http://localhost:8080/index'); + expect(post.withCredentials).toBe(true); + expect(post.getBody()).toBe('username=admin&password=password'); + expect(post.headers.get('Content-Type')).toBe('application/x-www-form-urlencoded'); + + connections[1].mockRespond(new Response(new ResponseOptions({ status: 200 }))); + expect(done).toBe(true); })); + + it('does not post credentials when the login page request fails', inject([LoginService], (service: LoginService) => { + let error: any; + service.sendCredential('admin', 'password').subscribe(() => fail('should not succeed'), err => error = err); + + connections[0].mockError(new Error('network down')); + + expect(connections.length).toBe(1); + expect(error).toBeDefined(); + })); +}); + +describe('xsrfStrategyFactory', () => { + afterEach(() => { + document.cookie = 'XSRF-TOKEN=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'; + }); + + it('copies the XSRF-TOKEN cookie into the X-XSRF-TOKEN header', () => { + document.cookie = 'XSRF-TOKEN=test-token; path=/'; + const req = new Request(new RequestOptions({ method: RequestMethod.Post, url: 'http://localhost:8080/api/user/u/enable' })); + + xsrfStrategyFactory().configureRequest(req); + + expect(req.headers.get('X-XSRF-TOKEN')).toBe('test-token'); + }); }); diff --git a/AdminPortal/src/app/login.service.ts b/AdminPortal/src/app/login.service.ts index a229f8f0..84cd62ed 100644 --- a/AdminPortal/src/app/login.service.ts +++ b/AdminPortal/src/app/login.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import {Http, Headers} from '@angular/http'; import {Observable} from 'rxjs/Observable'; +import 'rxjs/add/operator/mergeMap'; @Injectable() export class LoginService { @@ -15,7 +16,10 @@ export class LoginService { 'Content-Type': 'application/x-www-form-urlencoded' // 'Access-Control-Allow-Credentials' : true }); - return this.http.post(url, params, {headers: headers, withCredentials : true}); + // GET the login page first so the backend sets the XSRF-TOKEN cookie + // that Http sends back as the X-XSRF-TOKEN header on the login POST. + return this.http.get(url, { withCredentials: true }) + .mergeMap(() => this.http.post(url, params, {headers: headers, withCredentials : true})); } logout() { diff --git a/AdminPortal/src/app/navbar/navbar.component.ts b/AdminPortal/src/app/navbar/navbar.component.ts index 019989bd..e551a086 100644 --- a/AdminPortal/src/app/navbar/navbar.component.ts +++ b/AdminPortal/src/app/navbar/navbar.component.ts @@ -23,11 +23,10 @@ export class NavbarComponent implements OnInit { this.loginService.logout().subscribe( res => { localStorage.setItem('PortalAdminHasLoggedIn', ''); + this.router.navigate(['/login']).then(() => location.reload()); }, err => console.log(err) ); - location.reload(); - this.router.navigate(['/login']); } getDisplay() { diff --git a/AdminPortal/src/app/user-account/user-account.component.ts b/AdminPortal/src/app/user-account/user-account.component.ts index 872546c7..4fc0189d 100644 --- a/AdminPortal/src/app/user-account/user-account.component.ts +++ b/AdminPortal/src/app/user-account/user-account.component.ts @@ -33,13 +33,17 @@ export class UserAccountComponent implements OnInit { } enableUser(username: string) { - this.userService.enableUser(username).subscribe(); - location.reload(); + this.userService.enableUser(username).subscribe( + res => location.reload(), + err => console.log(err) + ); } disableUser(username: string) { - this.userService.disableUser(username).subscribe(); - location.reload(); + this.userService.disableUser(username).subscribe( + res => location.reload(), + err => console.log(err) + ); } diff --git a/AdminPortal/src/app/user.service.spec.ts b/AdminPortal/src/app/user.service.spec.ts index 79eb5480..6fbf77dc 100644 --- a/AdminPortal/src/app/user.service.spec.ts +++ b/AdminPortal/src/app/user.service.spec.ts @@ -1,16 +1,42 @@ -/* tslint:disable:no-unused-variable */ - -import { TestBed, async, inject } from '@angular/core/testing'; +import { TestBed, inject } from '@angular/core/testing'; +import { BaseRequestOptions, Http, RequestMethod } from '@angular/http'; +import { MockBackend, MockConnection } from '@angular/http/testing'; import { UserService } from './user.service'; describe('Service: User', () => { + let connections: MockConnection[]; + beforeEach(() => { TestBed.configureTestingModule({ - providers: [UserService] + providers: [ + UserService, + MockBackend, + BaseRequestOptions, + { + provide: Http, + useFactory: (b: MockBackend, options: BaseRequestOptions) => new Http(b, options), + deps: [MockBackend, BaseRequestOptions] + } + ] }); + connections = []; + TestBed.get(MockBackend).connections.subscribe((c: MockConnection) => connections.push(c)); }); - it('should ...', inject([UserService], (service: UserService) => { - expect(service).toBeTruthy(); + function expectCredentialedPost(url: string) { + expect(connections.length).toBe(1); + expect(connections[0].request.method).toBe(RequestMethod.Post); + expect(connections[0].request.url).toBe(url); + expect(connections[0].request.withCredentials).toBe(true); + } + + it('enables a user with POST', inject([UserService], (service: UserService) => { + service.enableUser('alice').subscribe(); + expectCredentialedPost('http://localhost:8080/api/user/alice/enable'); + })); + + it('disables a user with POST', inject([UserService], (service: UserService) => { + service.disableUser('alice').subscribe(); + expectCredentialedPost('http://localhost:8080/api/user/alice/disable'); })); }); diff --git a/AdminPortal/src/app/user.service.ts b/AdminPortal/src/app/user.service.ts index bde10a38..424ec2a1 100644 --- a/AdminPortal/src/app/user.service.ts +++ b/AdminPortal/src/app/user.service.ts @@ -24,12 +24,12 @@ export class UserService { enableUser (username: string) { let url = "http://localhost:8080/api/user/"+username+"/enable"; - return this.http.get(url, { withCredentials: true }); + return this.http.post(url, null, { withCredentials: true }); } disableUser (username: string) { let url = "http://localhost:8080/api/user/"+username+"/disable"; - return this.http.get(url, { withCredentials: true }); + return this.http.post(url, null, { withCredentials: true }); } }