diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-06-01 20:36:27 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-06-01 20:36:27 +0200 |
commit | a840d396093ef968f9512862197ac166a1ff9921 (patch) | |
tree | f7e4c93ee3ae8a44bd14fd1f7c74234eef7469ef /client/app/users | |
parent | 575fdcece562b914149f89f5a5b96ab206648f09 (diff) | |
download | PeerTube-a840d396093ef968f9512862197ac166a1ff9921.tar.gz PeerTube-a840d396093ef968f9512862197ac166a1ff9921.tar.zst PeerTube-a840d396093ef968f9512862197ac166a1ff9921.zip |
Add authentication tokens to make friends/quit friends
Diffstat (limited to 'client/app/users')
-rw-r--r-- | client/app/users/index.ts | 2 | ||||
-rw-r--r-- | client/app/users/login/index.ts | 1 | ||||
-rw-r--r-- | client/app/users/login/login.component.html | 14 | ||||
-rw-r--r-- | client/app/users/login/login.component.scss | 0 | ||||
-rw-r--r-- | client/app/users/login/login.component.ts | 37 | ||||
-rw-r--r-- | client/app/users/shared/auth-status.model.ts | 4 | ||||
-rw-r--r-- | client/app/users/shared/auth.service.ts | 108 | ||||
-rw-r--r-- | client/app/users/shared/index.ts | 4 | ||||
-rw-r--r-- | client/app/users/shared/token.model.ts | 32 | ||||
-rw-r--r-- | client/app/users/shared/user.model.ts | 20 |
10 files changed, 0 insertions, 222 deletions
diff --git a/client/app/users/index.ts b/client/app/users/index.ts deleted file mode 100644 index 4f08b8bc7..000000000 --- a/client/app/users/index.ts +++ /dev/null | |||
@@ -1,2 +0,0 @@ | |||
1 | export * from './login/index'; | ||
2 | export * from './shared/index'; | ||
diff --git a/client/app/users/login/index.ts b/client/app/users/login/index.ts deleted file mode 100644 index 69c16441f..000000000 --- a/client/app/users/login/index.ts +++ /dev/null | |||
@@ -1 +0,0 @@ | |||
1 | export * from './login.component'; | ||
diff --git a/client/app/users/login/login.component.html b/client/app/users/login/login.component.html deleted file mode 100644 index 940694515..000000000 --- a/client/app/users/login/login.component.html +++ /dev/null | |||
@@ -1,14 +0,0 @@ | |||
1 | <h3>Login</h3> | ||
2 | <form role="form" (submit)="login(username.value, password.value)"> | ||
3 | <div class="form-group"> | ||
4 | <label for="username">Username</label> | ||
5 | <input type="text" #username class="form-control" id="username" placeholder="Username"> | ||
6 | </div> | ||
7 | |||
8 | <div class="form-group"> | ||
9 | <label for="password">Password</label> | ||
10 | <input type="password" #password class="form-control" id="password" placeholder="Password"> | ||
11 | </div> | ||
12 | |||
13 | <input type="submit" value="Login" class="btn btn-default"> | ||
14 | </form> | ||
diff --git a/client/app/users/login/login.component.scss b/client/app/users/login/login.component.scss deleted file mode 100644 index e69de29bb..000000000 --- a/client/app/users/login/login.component.scss +++ /dev/null | |||
diff --git a/client/app/users/login/login.component.ts b/client/app/users/login/login.component.ts deleted file mode 100644 index 09c5f1af7..000000000 --- a/client/app/users/login/login.component.ts +++ /dev/null | |||
@@ -1,37 +0,0 @@ | |||
1 | import { Component } from '@angular/core'; | ||
2 | import { Router } from '@angular/router-deprecated'; | ||
3 | |||
4 | import { AuthService, AuthStatus, User } from '../shared/index'; | ||
5 | |||
6 | @Component({ | ||
7 | selector: 'my-user-login', | ||
8 | styleUrls: [ 'client/app/users/login/login.component.css' ], | ||
9 | templateUrl: 'client/app/users/login/login.component.html' | ||
10 | }) | ||
11 | |||
12 | export class UserLoginComponent { | ||
13 | constructor( | ||
14 | private authService: AuthService, | ||
15 | private router: Router | ||
16 | ) {} | ||
17 | |||
18 | login(username: string, password: string) { | ||
19 | this.authService.login(username, password).subscribe( | ||
20 | result => { | ||
21 | const user = new User(username, result); | ||
22 | user.save(); | ||
23 | |||
24 | this.authService.setStatus(AuthStatus.LoggedIn); | ||
25 | |||
26 | this.router.navigate(['VideosList']); | ||
27 | }, | ||
28 | error => { | ||
29 | if (error.error === 'invalid_grant') { | ||
30 | alert('Credentials are invalid.'); | ||
31 | } else { | ||
32 | alert(`${error.error}: ${error.error_description}`); | ||
33 | } | ||
34 | } | ||
35 | ); | ||
36 | } | ||
37 | } | ||
diff --git a/client/app/users/shared/auth-status.model.ts b/client/app/users/shared/auth-status.model.ts deleted file mode 100644 index f646bd4cf..000000000 --- a/client/app/users/shared/auth-status.model.ts +++ /dev/null | |||
@@ -1,4 +0,0 @@ | |||
1 | export enum AuthStatus { | ||
2 | LoggedIn, | ||
3 | LoggedOut | ||
4 | } | ||
diff --git a/client/app/users/shared/auth.service.ts b/client/app/users/shared/auth.service.ts deleted file mode 100644 index d63fe38f3..000000000 --- a/client/app/users/shared/auth.service.ts +++ /dev/null | |||
@@ -1,108 +0,0 @@ | |||
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/users/shared/index.ts b/client/app/users/shared/index.ts deleted file mode 100644 index c6816b3c6..000000000 --- a/client/app/users/shared/index.ts +++ /dev/null | |||
@@ -1,4 +0,0 @@ | |||
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/users/shared/token.model.ts b/client/app/users/shared/token.model.ts deleted file mode 100644 index 021c83fad..000000000 --- a/client/app/users/shared/token.model.ts +++ /dev/null | |||
@@ -1,32 +0,0 @@ | |||
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/users/shared/user.model.ts b/client/app/users/shared/user.model.ts deleted file mode 100644 index ca0a5f26c..000000000 --- a/client/app/users/shared/user.model.ts +++ /dev/null | |||
@@ -1,20 +0,0 @@ | |||
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 | } | ||