From bd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 20 Jul 2016 16:24:18 +0200 Subject: Client: Add authHttp service that authentificates the http request and optionally refresh the access token if needed --- client/src/app/shared/users/auth-status.model.ts | 4 - client/src/app/shared/users/auth.service.ts | 113 ----------------------- client/src/app/shared/users/index.ts | 4 - client/src/app/shared/users/token.model.ts | 32 ------- client/src/app/shared/users/user.model.ts | 20 ---- 5 files changed, 173 deletions(-) delete mode 100644 client/src/app/shared/users/auth-status.model.ts delete mode 100644 client/src/app/shared/users/auth.service.ts delete mode 100644 client/src/app/shared/users/index.ts delete mode 100644 client/src/app/shared/users/token.model.ts delete mode 100644 client/src/app/shared/users/user.model.ts (limited to 'client/src/app/shared/users') diff --git a/client/src/app/shared/users/auth-status.model.ts b/client/src/app/shared/users/auth-status.model.ts deleted file mode 100644 index f646bd4cf..000000000 --- a/client/src/app/shared/users/auth-status.model.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum AuthStatus { - LoggedIn, - LoggedOut -} diff --git a/client/src/app/shared/users/auth.service.ts b/client/src/app/shared/users/auth.service.ts deleted file mode 100644 index 1c822c1e1..000000000 --- a/client/src/app/shared/users/auth.service.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { Injectable } from '@angular/core'; -import { Headers, Http, RequestOptions, Response, URLSearchParams } from '@angular/http'; -import { Observable } from 'rxjs/Observable'; -import { Subject } from 'rxjs/Subject'; - -import { AuthStatus } from './auth-status.model'; -import { User } from './user.model'; - -@Injectable() -export class AuthService { - private static BASE_CLIENT_URL = '/api/v1/users/client'; - private static BASE_LOGIN_URL = '/api/v1/users/token'; - - loginChangedSource: Observable; - - private clientId: string; - private clientSecret: string; - private loginChanged: Subject; - - constructor(private http: Http) { - this.loginChanged = new Subject(); - this.loginChangedSource = this.loginChanged.asObservable(); - - // Fetch the client_id/client_secret - // FIXME: save in local storage? - this.http.get(AuthService.BASE_CLIENT_URL) - .map(res => res.json()) - .catch(this.handleError) - .subscribe( - result => { - this.clientId = result.client_id; - this.clientSecret = result.client_secret; - console.log('Client credentials loaded.'); - }, - error => { - alert(error); - } - ); - } - - getAuthRequestOptions(): RequestOptions { - return new RequestOptions({ headers: this.getRequestHeader() }); - } - - getRequestHeader() { - return new Headers({ 'Authorization': this.getRequestHeaderValue() }); - } - - getRequestHeaderValue() { - return `${this.getTokenType()} ${this.getToken()}`; - } - - getToken() { - return localStorage.getItem('access_token'); - } - - getTokenType() { - return localStorage.getItem('token_type'); - } - - getUser(): User { - if (this.isLoggedIn() === false) { - return null; - } - - const user = User.load(); - - return user; - } - - isLoggedIn() { - if (this.getToken()) { - return true; - } else { - return false; - } - } - - login(username: string, password: string) { - let body = new URLSearchParams(); - body.set('client_id', this.clientId); - body.set('client_secret', this.clientSecret); - body.set('response_type', 'code'); - body.set('grant_type', 'password'); - body.set('scope', 'upload'); - body.set('username', username); - body.set('password', password); - - let headers = new Headers(); - headers.append('Content-Type', 'application/x-www-form-urlencoded'); - - let options = { - headers: headers - }; - - return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options) - .map(res => res.json()) - .catch(this.handleError); - } - - logout() { - // TODO make HTTP request - } - - setStatus(status: AuthStatus) { - this.loginChanged.next(status); - } - - private handleError (error: Response) { - console.error(error); - return Observable.throw(error.json() || { error: 'Server error' }); - } -} diff --git a/client/src/app/shared/users/index.ts b/client/src/app/shared/users/index.ts deleted file mode 100644 index c6816b3c6..000000000 --- a/client/src/app/shared/users/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './auth-status.model'; -export * from './auth.service'; -export * from './token.model'; -export * from './user.model'; diff --git a/client/src/app/shared/users/token.model.ts b/client/src/app/shared/users/token.model.ts deleted file mode 100644 index 021c83fad..000000000 --- a/client/src/app/shared/users/token.model.ts +++ /dev/null @@ -1,32 +0,0 @@ -export class Token { - access_token: string; - refresh_token: string; - token_type: string; - - static load() { - return new Token({ - access_token: localStorage.getItem('access_token'), - refresh_token: localStorage.getItem('refresh_token'), - token_type: localStorage.getItem('token_type') - }); - } - - constructor(hash?: any) { - if (hash) { - this.access_token = hash.access_token; - this.refresh_token = hash.refresh_token; - - if (hash.token_type === 'bearer') { - this.token_type = 'Bearer'; - } else { - this.token_type = hash.token_type; - } - } - } - - save() { - localStorage.setItem('access_token', this.access_token); - localStorage.setItem('refresh_token', this.refresh_token); - localStorage.setItem('token_type', this.token_type); - } -} diff --git a/client/src/app/shared/users/user.model.ts b/client/src/app/shared/users/user.model.ts deleted file mode 100644 index ca0a5f26c..000000000 --- a/client/src/app/shared/users/user.model.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Token } from './token.model'; - -export class User { - username: string; - token: Token; - - static load() { - return new User(localStorage.getItem('username'), Token.load()); - } - - constructor(username: string, hash_token: any) { - this.username = username; - this.token = new Token(hash_token); - } - - save() { - localStorage.setItem('username', this.username); - this.token.save(); - } -} -- cgit v1.2.3