aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/users
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-07-20 16:24:18 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-07-20 16:25:06 +0200
commitbd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869 (patch)
tree66df283a1554f27b92e392fca36b8e272d7535bc /client/src/app/shared/users
parent2f372a865487427ff97ad17edd0e6adfbb478c80 (diff)
downloadPeerTube-bd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869.tar.gz
PeerTube-bd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869.tar.zst
PeerTube-bd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869.zip
Client: Add authHttp service that authentificates the http request and
optionally refresh the access token if needed
Diffstat (limited to 'client/src/app/shared/users')
-rw-r--r--client/src/app/shared/users/auth-status.model.ts4
-rw-r--r--client/src/app/shared/users/auth.service.ts113
-rw-r--r--client/src/app/shared/users/index.ts4
-rw-r--r--client/src/app/shared/users/token.model.ts32
-rw-r--r--client/src/app/shared/users/user.model.ts20
5 files changed, 0 insertions, 173 deletions
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 @@
1export enum AuthStatus {
2 LoggedIn,
3 LoggedOut
4}
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 @@
1import { Injectable } from '@angular/core';
2import { Headers, Http, RequestOptions, Response, URLSearchParams } from '@angular/http';
3import { Observable } from 'rxjs/Observable';
4import { Subject } from 'rxjs/Subject';
5
6import { AuthStatus } from './auth-status.model';
7import { User } from './user.model';
8
9@Injectable()
10export class AuthService {
11 private static BASE_CLIENT_URL = '/api/v1/users/client';
12 private static BASE_LOGIN_URL = '/api/v1/users/token';
13
14 loginChangedSource: Observable<AuthStatus>;
15
16 private clientId: string;
17 private clientSecret: string;
18 private loginChanged: Subject<AuthStatus>;
19
20 constructor(private http: Http) {
21 this.loginChanged = new Subject<AuthStatus>();
22 this.loginChangedSource = this.loginChanged.asObservable();
23
24 // Fetch the client_id/client_secret
25 // FIXME: save in local storage?
26 this.http.get(AuthService.BASE_CLIENT_URL)
27 .map(res => res.json())
28 .catch(this.handleError)
29 .subscribe(
30 result => {
31 this.clientId = result.client_id;
32 this.clientSecret = result.client_secret;
33 console.log('Client credentials loaded.');
34 },
35 error => {
36 alert(error);
37 }
38 );
39 }
40
41 getAuthRequestOptions(): RequestOptions {
42 return new RequestOptions({ headers: this.getRequestHeader() });
43 }
44
45 getRequestHeader() {
46 return new Headers({ 'Authorization': this.getRequestHeaderValue() });
47 }
48
49 getRequestHeaderValue() {
50 return `${this.getTokenType()} ${this.getToken()}`;
51 }
52
53 getToken() {
54 return localStorage.getItem('access_token');
55 }
56
57 getTokenType() {
58 return localStorage.getItem('token_type');
59 }
60
61 getUser(): User {
62 if (this.isLoggedIn() === false) {
63 return null;
64 }
65
66 const user = User.load();
67
68 return user;
69 }
70
71 isLoggedIn() {
72 if (this.getToken()) {
73 return true;
74 } else {
75 return false;
76 }
77 }
78
79 login(username: string, password: string) {
80 let body = new URLSearchParams();
81 body.set('client_id', this.clientId);
82 body.set('client_secret', this.clientSecret);
83 body.set('response_type', 'code');
84 body.set('grant_type', 'password');
85 body.set('scope', 'upload');
86 body.set('username', username);
87 body.set('password', password);
88
89 let headers = new Headers();
90 headers.append('Content-Type', 'application/x-www-form-urlencoded');
91
92 let options = {
93 headers: headers
94 };
95
96 return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options)
97 .map(res => res.json())
98 .catch(this.handleError);
99 }
100
101 logout() {
102 // TODO make HTTP request
103 }
104
105 setStatus(status: AuthStatus) {
106 this.loginChanged.next(status);
107 }
108
109 private handleError (error: Response) {
110 console.error(error);
111 return Observable.throw(error.json() || { error: 'Server error' });
112 }
113}
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 @@
1export * from './auth-status.model';
2export * from './auth.service';
3export * from './token.model';
4export * 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 @@
1export 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/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 @@
1import { Token } from './token.model';
2
3export 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}