#17 Removed Authentication Service

The auth service is available in the alfresco login component
This commit is contained in:
mauriziovitale84 2016-04-22 17:37:14 +01:00
parent 66f2e380ab
commit be735c6049
2 changed files with 0 additions and 168 deletions

View File

@ -1,88 +0,0 @@
import {it, describe} from 'angular2/testing';
import {provide, Injector} from 'angular2/core';
import {Http, HTTP_PROVIDERS, XHRBackend, Response, ResponseOptions} from 'angular2/http';
import {MockBackend} from 'angular2/http/testing';
import {Authentication} from './authentication';
describe('Authentication', () => {
let injector,
backend,
mockBackend,
httpService,
service;
beforeEach(() => {
injector = Injector.resolveAndCreate([
HTTP_PROVIDERS,
MockBackend,
provide(XHRBackend, {useClass: MockBackend}),
Authentication
]);
let store = {};
spyOn(localStorage, 'getItem').and.callFake(function (key) {
return store[key];
});
spyOn(localStorage, 'setItem').and.callFake(function (key, value) {
return store[key] = value + '';
});
spyOn(localStorage, 'clear').and.callFake(function () {
store = {};
});
spyOn(localStorage, 'removeItem').and.callFake(function (key) {
delete store[key];
});
spyOn(localStorage, 'key').and.callFake(function (i) {
let keys = Object.keys(store);
return keys[i] || null;
});
mockBackend = injector.get(MockBackend);
backend = injector.get(XHRBackend);
httpService = injector.get(Http);
service = injector.get(Authentication);
});
it('should return true and token if the user is logged in', () => {
service.saveJwt('fake-local-token');
expect(service.isLoggedIn()).toBe(true);
expect(localStorage.getItem('token')).toBeDefined();
expect(localStorage.getItem('token')).toEqual('fake-local-token');
});
it('should return false and token undefined if the user is not logged in', () => {
expect(service.isLoggedIn()).toEqual(false);
expect(localStorage.getItem('token')).not.toBeDefined();
});
it('should return true and token on sign in', () => {
backend.connections.subscribe(connection => {
connection.mockRespond(new Response(new ResponseOptions({body: { data: { ticket: 'fake-post-token'}}})));
});
service.token = '';
service.login('POST', 'fakeUser', 'fakePassword')
.subscribe( () => {
expect(service.isLoggedIn()).toBe(true);
expect(service.token).toEqual('fake-post-token');
expect(localStorage.getItem('token')).toBeDefined();
expect(localStorage.getItem('token')).toEqual('fake-post-token');
}
);
});
it('should return false and token undefined on log out', () => {
service.token = 'fake-token';
localStorage.setItem('token', 'fake-token');
service.logout()
.subscribe( () => {
expect(service.isLoggedIn()).toBe(false);
expect(service.token).not.toBeDefined();
expect(localStorage.getItem('token')).not.toBeDefined();
}
);
});
});

View File

@ -1,80 +0,0 @@
import {Injectable} from 'angular2/core';
import {Observable} from 'rxjs/Rx';
import {Http, Headers, URLSearchParams, Response} from 'angular2/http';
declare let xml2json:any;
@Injectable()
export class Authentication {
token:string;
private _host:string = 'http://192.168.99.100:8080';
private _baseUrl:string = this._host + '/alfresco/service/api/';
constructor(public http:Http) {
this.token = localStorage.getItem('token');
}
isLoggedIn() {
return !!localStorage.getItem('token');
}
login(method:string, username:string, password:string) {
if (method === 'GET') {
return this.loginGet(username, password);
} else {
return this.loginPost(username, password);
}
}
loginGet(username:string, password:string) {
const searchParams = new URLSearchParams();
searchParams.set('u', username);
searchParams.set('pw', password);
return this.http.get(this._baseUrl + 'login', {search: searchParams})
.map((res:any) => {
let data = JSON.parse(xml2json(res.text(), ' '));
this.token = data.ticket;
this.saveJwt(this.token);
})
.catch(this.handleError);
}
loginPost(username:string, password:string) {
let credentials = '{ username: ' + username + ', password: ' + password + ' }';
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post(this._baseUrl + 'login', credentials, {
headers: headers
})
.map((res:any) => {
let response = res.json();
this.token = response.data.ticket;
this.saveJwt(this.token);
})
.catch(this.handleError);
}
saveJwt(jwt) {
if (jwt) {
localStorage.setItem('token', jwt);
}
}
logout() {
this.token = undefined;
localStorage.removeItem('token');
return Observable.of(true);
}
private handleError(error:Response) {
// in a real world app, we may send the error to some remote logging infrastructure
// instead of just logging it to the console
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}