Skip to content
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
10 changes: 8 additions & 2 deletions AdminPortal/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -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';


Expand All @@ -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,
Expand All @@ -38,7 +43,8 @@ import { AppointmentService } from './appointment.service';
providers: [
LoginService,
UserService,
AppointmentService
AppointmentService,
{ provide: XSRFStrategy, useFactory: xsrfStrategyFactory }
],
bootstrap: [AppComponent]
})
Expand Down
33 changes: 27 additions & 6 deletions AdminPortal/src/app/appointment.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
}));
});
2 changes: 1 addition & 1 deletion AdminPortal/src/app/appointment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}

}
6 changes: 4 additions & 2 deletions AdminPortal/src/app/appointment/appointment.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
72 changes: 66 additions & 6 deletions AdminPortal/src/app/login.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
6 changes: 5 additions & 1 deletion AdminPortal/src/app/login.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions AdminPortal/src/app/navbar/navbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 8 additions & 4 deletions AdminPortal/src/app/user-account/user-account.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}


Expand Down
38 changes: 32 additions & 6 deletions AdminPortal/src/app/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
}));
});
4 changes: 2 additions & 2 deletions AdminPortal/src/app/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}

}