]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/auth/auth.service.ts
Server: delete user with the id and not the username
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / auth / auth.service.ts
CommitLineData
230809ef 1import { Injectable } from '@angular/core';
0f3a78e7 2import { Headers, Http, Response, URLSearchParams } from '@angular/http';
5555f886
C
3import { Observable } from 'rxjs/Observable';
4import { Subject } from 'rxjs/Subject';
b1794c53 5
41a2aee3
C
6import { AuthStatus } from './auth-status.model';
7import { User } from './user.model';
b1794c53
C
8
9@Injectable()
10export class AuthService {
6606150c 11 private static BASE_CLIENT_URL = '/api/v1/clients/local';
bd5c83a8 12 private static BASE_TOKEN_URL = '/api/v1/users/token';
629d8d6f 13 private static BASE_USER_INFORMATIONS_URL = '/api/v1/users/me';
b1794c53 14
ccf6ed16 15 loginChangedSource: Observable<AuthStatus>;
b1794c53 16
ccf6ed16
C
17 private clientId: string;
18 private clientSecret: string;
4fd8aa32 19 private loginChanged: Subject<AuthStatus>;
bd5c83a8 20 private user: User = null;
ccf6ed16
C
21
22 constructor(private http: Http) {
23 this.loginChanged = new Subject<AuthStatus>();
24 this.loginChangedSource = this.loginChanged.asObservable();
23a5a916
C
25
26 // Fetch the client_id/client_secret
27 // FIXME: save in local storage?
ccf6ed16 28 this.http.get(AuthService.BASE_CLIENT_URL)
23a5a916
C
29 .map(res => res.json())
30 .catch(this.handleError)
31 .subscribe(
32 result => {
ccf6ed16
C
33 this.clientId = result.client_id;
34 this.clientSecret = result.client_secret;
23a5a916
C
35 console.log('Client credentials loaded.');
36 },
37 error => {
38 alert(error);
39 }
ad10a70b 40 );
bd5c83a8
C
41
42 // Return null if there is nothing to load
43 this.user = User.load();
1553e15d 44 }
b1794c53 45
bd5c83a8
C
46 getRefreshToken() {
47 if (this.user === null) return null;
48
49 return this.user.getRefreshToken();
50 }
51
e822fdae 52 getRequestHeaderValue() {
0f3a78e7 53 return `${this.getTokenType()} ${this.getAccessToken()}`;
1553e15d
C
54 }
55
0f3a78e7 56 getAccessToken() {
bd5c83a8
C
57 if (this.user === null) return null;
58
59 return this.user.getAccessToken();
1553e15d
C
60 }
61
ccf6ed16 62 getTokenType() {
bd5c83a8
C
63 if (this.user === null) return null;
64
65 return this.user.getTokenType();
1553e15d
C
66 }
67
68 getUser(): User {
bd5c83a8 69 return this.user;
1553e15d
C
70 }
71
ccf6ed16 72 isLoggedIn() {
0f3a78e7 73 if (this.getAccessToken()) {
1553e15d
C
74 return true;
75 } else {
76 return false;
77 }
78 }
79
4fd8aa32
C
80 login(username: string, password: string) {
81 let body = new URLSearchParams();
82 body.set('client_id', this.clientId);
83 body.set('client_secret', this.clientSecret);
84 body.set('response_type', 'code');
85 body.set('grant_type', 'password');
86 body.set('scope', 'upload');
87 body.set('username', username);
88 body.set('password', password);
89
90 let headers = new Headers();
91 headers.append('Content-Type', 'application/x-www-form-urlencoded');
92
93 let options = {
94 headers: headers
95 };
96
bd5c83a8 97 return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options)
4fd8aa32 98 .map(res => res.json())
bd5c83a8
C
99 .map(res => {
100 res.username = username;
101 return res;
102 })
629d8d6f 103 .flatMap(res => this.fetchUserInformations(res))
bd5c83a8 104 .map(res => this.handleLogin(res))
4fd8aa32
C
105 .catch(this.handleError);
106 }
107
108 logout() {
bd5c83a8
C
109 // TODO: make an HTTP request to revoke the tokens
110 this.user = null;
111 User.flush();
e62f6ef7
C
112
113 this.setStatus(AuthStatus.LoggedOut);
bd5c83a8
C
114 }
115
116 refreshAccessToken() {
117 console.log('Refreshing token...');
118
119 const refreshToken = this.getRefreshToken();
120
121 let body = new URLSearchParams();
122 body.set('refresh_token', refreshToken);
123 body.set('client_id', this.clientId);
124 body.set('client_secret', this.clientSecret);
125 body.set('response_type', 'code');
126 body.set('grant_type', 'refresh_token');
127
128 let headers = new Headers();
129 headers.append('Content-Type', 'application/x-www-form-urlencoded');
130
131 let options = {
132 headers: headers
133 };
134
135 return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options)
136 .map(res => res.json())
137 .map(res => this.handleRefreshToken(res))
138 .catch(this.handleError);
4fd8aa32
C
139 }
140
629d8d6f
C
141 private fetchUserInformations (obj: any) {
142 // Do not call authHttp here to avoid circular dependencies headaches
143
144 const headers = new Headers();
145 headers.set('Authorization', `Bearer ${obj.access_token}`);
146
147 return this.http.get(AuthService.BASE_USER_INFORMATIONS_URL, { headers })
148 .map(res => res.json())
149 .map(res => {
150 obj.id = res.id;
151 obj.role = res.role;
152 return obj;
153 }
154 );
155 }
156
157 private handleError (error: Response) {
158 console.error(error);
159 return Observable.throw(error.json() || { error: 'Server error' });
b1794c53
C
160 }
161
bd5c83a8 162 private handleLogin (obj: any) {
629d8d6f 163 const id = obj.id;
bd5c83a8 164 const username = obj.username;
629d8d6f 165 const role = obj.role;
bd5c83a8
C
166 const hash_tokens = {
167 access_token: obj.access_token,
168 token_type: obj.token_type,
169 refresh_token: obj.refresh_token
170 };
171
629d8d6f 172 this.user = new User(id, username, role, hash_tokens);
bd5c83a8
C
173 this.user.save();
174
175 this.setStatus(AuthStatus.LoggedIn);
176 }
177
bd5c83a8
C
178 private handleRefreshToken (obj: any) {
179 this.user.refreshTokens(obj.access_token, obj.refresh_token);
180 this.user.save();
181 }
629d8d6f
C
182
183 private setStatus(status: AuthStatus) {
184 this.loginChanged.next(status);
185 }
186
b1794c53 187}