diff options
Diffstat (limited to 'client/app/shared')
-rw-r--r-- | client/app/shared/index.ts | 5 | ||||
-rw-r--r-- | client/app/shared/search/index.ts | 3 | ||||
-rw-r--r-- | client/app/shared/search/search-field.type.ts (renamed from client/app/shared/search-field.type.ts) | 0 | ||||
-rw-r--r-- | client/app/shared/search/search.component.html (renamed from client/app/shared/search.component.html) | 0 | ||||
-rw-r--r-- | client/app/shared/search/search.component.ts (renamed from client/app/shared/search.component.ts) | 2 | ||||
-rw-r--r-- | client/app/shared/search/search.model.ts (renamed from client/app/shared/search.model.ts) | 0 | ||||
-rw-r--r-- | client/app/shared/users/auth-status.model.ts | 4 | ||||
-rw-r--r-- | client/app/shared/users/auth.service.ts | 108 | ||||
-rw-r--r-- | client/app/shared/users/index.ts | 4 | ||||
-rw-r--r-- | client/app/shared/users/token.model.ts | 32 | ||||
-rw-r--r-- | client/app/shared/users/user.model.ts | 20 |
11 files changed, 174 insertions, 4 deletions
diff --git a/client/app/shared/index.ts b/client/app/shared/index.ts index a49a4f1a9..ad3ee0098 100644 --- a/client/app/shared/index.ts +++ b/client/app/shared/index.ts | |||
@@ -1,3 +1,2 @@ | |||
1 | export * from './search-field.type'; | 1 | export * from './search/index'; |
2 | export * from './search.component'; | 2 | export * from './users/index' |
3 | export * from './search.model'; | ||
diff --git a/client/app/shared/search/index.ts b/client/app/shared/search/index.ts new file mode 100644 index 000000000..a49a4f1a9 --- /dev/null +++ b/client/app/shared/search/index.ts | |||
@@ -0,0 +1,3 @@ | |||
1 | export * from './search-field.type'; | ||
2 | export * from './search.component'; | ||
3 | export * from './search.model'; | ||
diff --git a/client/app/shared/search-field.type.ts b/client/app/shared/search/search-field.type.ts index 846236290..846236290 100644 --- a/client/app/shared/search-field.type.ts +++ b/client/app/shared/search/search-field.type.ts | |||
diff --git a/client/app/shared/search.component.html b/client/app/shared/search/search.component.html index fb13ac72e..fb13ac72e 100644 --- a/client/app/shared/search.component.html +++ b/client/app/shared/search/search.component.html | |||
diff --git a/client/app/shared/search.component.ts b/client/app/shared/search/search.component.ts index e1e30b9af..d541cd0d6 100644 --- a/client/app/shared/search.component.ts +++ b/client/app/shared/search/search.component.ts | |||
@@ -7,7 +7,7 @@ import { SearchField } from './search-field.type'; | |||
7 | 7 | ||
8 | @Component({ | 8 | @Component({ |
9 | selector: 'my-search', | 9 | selector: 'my-search', |
10 | templateUrl: 'client/app/shared/search.component.html', | 10 | templateUrl: 'client/app/shared/search/search.component.html', |
11 | directives: [ DROPDOWN_DIRECTIVES ] | 11 | directives: [ DROPDOWN_DIRECTIVES ] |
12 | }) | 12 | }) |
13 | 13 | ||
diff --git a/client/app/shared/search.model.ts b/client/app/shared/search/search.model.ts index 932a6566c..932a6566c 100644 --- a/client/app/shared/search.model.ts +++ b/client/app/shared/search/search.model.ts | |||
diff --git a/client/app/shared/users/auth-status.model.ts b/client/app/shared/users/auth-status.model.ts new file mode 100644 index 000000000..f646bd4cf --- /dev/null +++ b/client/app/shared/users/auth-status.model.ts | |||
@@ -0,0 +1,4 @@ | |||
1 | export enum AuthStatus { | ||
2 | LoggedIn, | ||
3 | LoggedOut | ||
4 | } | ||
diff --git a/client/app/shared/users/auth.service.ts b/client/app/shared/users/auth.service.ts new file mode 100644 index 000000000..d63fe38f3 --- /dev/null +++ b/client/app/shared/users/auth.service.ts | |||
@@ -0,0 +1,108 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { Headers, Http, RequestOptions, Response, URLSearchParams } from '@angular/http'; | ||
3 | import { Observable, Subject } from 'rxjs/Rx'; | ||
4 | |||
5 | import { AuthStatus } from './auth-status.model'; | ||
6 | import { User } from './user.model'; | ||
7 | |||
8 | @Injectable() | ||
9 | export class AuthService { | ||
10 | private static BASE_CLIENT_URL = '/api/v1/users/client'; | ||
11 | private static BASE_LOGIN_URL = '/api/v1/users/token'; | ||
12 | |||
13 | loginChangedSource: Observable<AuthStatus>; | ||
14 | |||
15 | private clientId: string; | ||
16 | private clientSecret: string; | ||
17 | private loginChanged: Subject<AuthStatus>; | ||
18 | |||
19 | constructor(private http: Http) { | ||
20 | this.loginChanged = new Subject<AuthStatus>(); | ||
21 | this.loginChangedSource = this.loginChanged.asObservable(); | ||
22 | |||
23 | // Fetch the client_id/client_secret | ||
24 | // FIXME: save in local storage? | ||
25 | this.http.get(AuthService.BASE_CLIENT_URL) | ||
26 | .map(res => res.json()) | ||
27 | .catch(this.handleError) | ||
28 | .subscribe( | ||
29 | result => { | ||
30 | this.clientId = result.client_id; | ||
31 | this.clientSecret = result.client_secret; | ||
32 | console.log('Client credentials loaded.'); | ||
33 | }, | ||
34 | error => { | ||
35 | alert(error); | ||
36 | } | ||
37 | ); | ||
38 | } | ||
39 | |||
40 | getAuthRequestOptions(): RequestOptions { | ||
41 | return new RequestOptions({ headers: this.getRequestHeader() }); | ||
42 | } | ||
43 | |||
44 | getRequestHeader() { | ||
45 | return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); | ||
46 | } | ||
47 | |||
48 | getToken() { | ||
49 | return localStorage.getItem('access_token'); | ||
50 | } | ||
51 | |||
52 | getTokenType() { | ||
53 | return localStorage.getItem('token_type'); | ||
54 | } | ||
55 | |||
56 | getUser(): User { | ||
57 | if (this.isLoggedIn() === false) { | ||
58 | return null; | ||
59 | } | ||
60 | |||
61 | const user = User.load(); | ||
62 | |||
63 | return user; | ||
64 | } | ||
65 | |||
66 | isLoggedIn() { | ||
67 | if (this.getToken()) { | ||
68 | return true; | ||
69 | } else { | ||
70 | return false; | ||
71 | } | ||
72 | } | ||
73 | |||
74 | login(username: string, password: string) { | ||
75 | let body = new URLSearchParams(); | ||
76 | body.set('client_id', this.clientId); | ||
77 | body.set('client_secret', this.clientSecret); | ||
78 | body.set('response_type', 'code'); | ||
79 | body.set('grant_type', 'password'); | ||
80 | body.set('scope', 'upload'); | ||
81 | body.set('username', username); | ||
82 | body.set('password', password); | ||
83 | |||
84 | let headers = new Headers(); | ||
85 | headers.append('Content-Type', 'application/x-www-form-urlencoded'); | ||
86 | |||
87 | let options = { | ||
88 | headers: headers | ||
89 | }; | ||
90 | |||
91 | return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options) | ||
92 | .map(res => res.json()) | ||
93 | .catch(this.handleError); | ||
94 | } | ||
95 | |||
96 | logout() { | ||
97 | // TODO make HTTP request | ||
98 | } | ||
99 | |||
100 | setStatus(status: AuthStatus) { | ||
101 | this.loginChanged.next(status); | ||
102 | } | ||
103 | |||
104 | private handleError (error: Response) { | ||
105 | console.error(error); | ||
106 | return Observable.throw(error.json() || { error: 'Server error' }); | ||
107 | } | ||
108 | } | ||
diff --git a/client/app/shared/users/index.ts b/client/app/shared/users/index.ts new file mode 100644 index 000000000..c6816b3c6 --- /dev/null +++ b/client/app/shared/users/index.ts | |||
@@ -0,0 +1,4 @@ | |||
1 | export * from './auth-status.model'; | ||
2 | export * from './auth.service'; | ||
3 | export * from './token.model'; | ||
4 | export * from './user.model'; | ||
diff --git a/client/app/shared/users/token.model.ts b/client/app/shared/users/token.model.ts new file mode 100644 index 000000000..021c83fad --- /dev/null +++ b/client/app/shared/users/token.model.ts | |||
@@ -0,0 +1,32 @@ | |||
1 | export class Token { | ||
2 | access_token: string; | ||
3 | refresh_token: string; | ||
4 | token_type: string; | ||
5 | |||
6 | static load() { | ||
7 | return new Token({ | ||
8 | access_token: localStorage.getItem('access_token'), | ||
9 | refresh_token: localStorage.getItem('refresh_token'), | ||
10 | token_type: localStorage.getItem('token_type') | ||
11 | }); | ||
12 | } | ||
13 | |||
14 | constructor(hash?: any) { | ||
15 | if (hash) { | ||
16 | this.access_token = hash.access_token; | ||
17 | this.refresh_token = hash.refresh_token; | ||
18 | |||
19 | if (hash.token_type === 'bearer') { | ||
20 | this.token_type = 'Bearer'; | ||
21 | } else { | ||
22 | this.token_type = hash.token_type; | ||
23 | } | ||
24 | } | ||
25 | } | ||
26 | |||
27 | save() { | ||
28 | localStorage.setItem('access_token', this.access_token); | ||
29 | localStorage.setItem('refresh_token', this.refresh_token); | ||
30 | localStorage.setItem('token_type', this.token_type); | ||
31 | } | ||
32 | } | ||
diff --git a/client/app/shared/users/user.model.ts b/client/app/shared/users/user.model.ts new file mode 100644 index 000000000..ca0a5f26c --- /dev/null +++ b/client/app/shared/users/user.model.ts | |||
@@ -0,0 +1,20 @@ | |||
1 | import { Token } from './token.model'; | ||
2 | |||
3 | export class User { | ||
4 | username: string; | ||
5 | token: Token; | ||
6 | |||
7 | static load() { | ||
8 | return new User(localStorage.getItem('username'), Token.load()); | ||
9 | } | ||
10 | |||
11 | constructor(username: string, hash_token: any) { | ||
12 | this.username = username; | ||
13 | this.token = new Token(hash_token); | ||
14 | } | ||
15 | |||
16 | save() { | ||
17 | localStorage.setItem('username', this.username); | ||
18 | this.token.save(); | ||
19 | } | ||
20 | } | ||