OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * OAuth2 API flow implementations. | 7 * OAuth2 API flow implementations. |
8 */ | 8 */ |
9 | 9 |
10 /** @suppress {duplicate} */ | 10 /** @suppress {duplicate} */ |
(...skipping 30 matching lines...) Expand all Loading... |
41 * | 41 * |
42 * @private | 42 * @private |
43 * @param {number} xhrStatus Status (HTTP response code) of the XMLHttpRequest. | 43 * @param {number} xhrStatus Status (HTTP response code) of the XMLHttpRequest. |
44 * @return {!remoting.Error} An error code to be raised. | 44 * @return {!remoting.Error} An error code to be raised. |
45 */ | 45 */ |
46 remoting.OAuth2ApiImpl.prototype.interpretXhrStatus_ = | 46 remoting.OAuth2ApiImpl.prototype.interpretXhrStatus_ = |
47 function(xhrStatus) { | 47 function(xhrStatus) { |
48 // Return AUTHENTICATION_FAILED by default, so that the user can try to | 48 // Return AUTHENTICATION_FAILED by default, so that the user can try to |
49 // recover from an unexpected failure by signing in again. | 49 // recover from an unexpected failure by signing in again. |
50 /** @type {!remoting.Error} */ | 50 /** @type {!remoting.Error} */ |
51 var error = remoting.Error.AUTHENTICATION_FAILED; | 51 var error = new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); |
52 if (xhrStatus == 400 || xhrStatus == 401 || xhrStatus == 403) { | 52 if (xhrStatus == 400 || xhrStatus == 401 || xhrStatus == 403) { |
53 error = remoting.Error.AUTHENTICATION_FAILED; | 53 error = new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); |
54 } else if (xhrStatus == 502 || xhrStatus == 503) { | 54 } else if (xhrStatus == 502 || xhrStatus == 503) { |
55 error = remoting.Error.SERVICE_UNAVAILABLE; | 55 error = new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); |
56 } else if (xhrStatus == 0) { | 56 } else if (xhrStatus == 0) { |
57 error = remoting.Error.NETWORK_FAILURE; | 57 error = new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); |
58 } else { | 58 } else { |
59 console.warn('Unexpected authentication response code: ' + xhrStatus); | 59 console.warn('Unexpected authentication response code: ' + xhrStatus); |
60 } | 60 } |
61 return error; | 61 return error; |
62 }; | 62 }; |
63 | 63 |
64 /** | 64 /** |
65 * Asynchronously retrieves a new access token from the server. | 65 * Asynchronously retrieves a new access token from the server. |
66 * | 66 * |
67 * @param {function(string, number): void} onDone Callback to invoke when | 67 * @param {function(string, number): void} onDone Callback to invoke when |
(...skipping 11 matching lines...) Expand all Loading... |
79 var onResponse = function(xhr) { | 79 var onResponse = function(xhr) { |
80 if (xhr.status == 200) { | 80 if (xhr.status == 200) { |
81 try { | 81 try { |
82 // Don't use base.jsonParseSafe here unless you also include base.js, | 82 // Don't use base.jsonParseSafe here unless you also include base.js, |
83 // otherwise this won't work from the OAuth trampoline. | 83 // otherwise this won't work from the OAuth trampoline. |
84 // TODO(jamiewalch): Fix this once we're no longer using the trampoline. | 84 // TODO(jamiewalch): Fix this once we're no longer using the trampoline. |
85 var tokens = JSON.parse(xhr.responseText); | 85 var tokens = JSON.parse(xhr.responseText); |
86 onDone(tokens['access_token'], tokens['expires_in']); | 86 onDone(tokens['access_token'], tokens['expires_in']); |
87 } catch (/** @type {Error} */ err) { | 87 } catch (/** @type {Error} */ err) { |
88 console.error('Invalid "token" response from server:', err); | 88 console.error('Invalid "token" response from server:', err); |
89 onError(remoting.Error.UNEXPECTED); | 89 onError(remoting.Error.unexpected()); |
90 } | 90 } |
91 } else { | 91 } else { |
92 console.error('Failed to refresh token. Status: ' + xhr.status + | 92 console.error('Failed to refresh token. Status: ' + xhr.status + |
93 ' response: ' + xhr.responseText); | 93 ' response: ' + xhr.responseText); |
94 onError(fromHttpStatus(xhr.status)); | 94 onError(fromHttpStatus(xhr.status)); |
95 } | 95 } |
96 }; | 96 }; |
97 | 97 |
98 remoting.xhr.start({ | 98 remoting.xhr.start({ |
99 method: 'POST', | 99 method: 'POST', |
(...skipping 29 matching lines...) Expand all Loading... |
129 if (xhr.status == 200) { | 129 if (xhr.status == 200) { |
130 try { | 130 try { |
131 // Don't use base.jsonParseSafe here unless you also include base.js, | 131 // Don't use base.jsonParseSafe here unless you also include base.js, |
132 // otherwise this won't work from the OAuth trampoline. | 132 // otherwise this won't work from the OAuth trampoline. |
133 // TODO(jamiewalch): Fix this once we're no longer using the trampoline. | 133 // TODO(jamiewalch): Fix this once we're no longer using the trampoline. |
134 var tokens = JSON.parse(xhr.responseText); | 134 var tokens = JSON.parse(xhr.responseText); |
135 onDone(tokens['refresh_token'], | 135 onDone(tokens['refresh_token'], |
136 tokens['access_token'], tokens['expires_in']); | 136 tokens['access_token'], tokens['expires_in']); |
137 } catch (/** @type {Error} */ err) { | 137 } catch (/** @type {Error} */ err) { |
138 console.error('Invalid "token" response from server:', err); | 138 console.error('Invalid "token" response from server:', err); |
139 onError(remoting.Error.UNEXPECTED); | 139 onError(remoting.Error.unexpected()); |
140 } | 140 } |
141 } else { | 141 } else { |
142 console.error('Failed to exchange code for token. Status: ' + xhr.status + | 142 console.error('Failed to exchange code for token. Status: ' + xhr.status + |
143 ' response: ' + xhr.responseText); | 143 ' response: ' + xhr.responseText); |
144 onError(fromHttpStatus(xhr.status)); | 144 onError(fromHttpStatus(xhr.status)); |
145 } | 145 } |
146 }; | 146 }; |
147 | 147 |
148 remoting.xhr.start({ | 148 remoting.xhr.start({ |
149 method: 'POST', | 149 method: 'POST', |
(...skipping 21 matching lines...) Expand all Loading... |
171 */ | 171 */ |
172 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) { | 172 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) { |
173 /** @param {XMLHttpRequest} xhr */ | 173 /** @param {XMLHttpRequest} xhr */ |
174 var onResponse = function(xhr) { | 174 var onResponse = function(xhr) { |
175 if (xhr.status == 200) { | 175 if (xhr.status == 200) { |
176 try { | 176 try { |
177 var result = JSON.parse(xhr.responseText); | 177 var result = JSON.parse(xhr.responseText); |
178 onDone(result['email']); | 178 onDone(result['email']); |
179 } catch (/** @type {Error} */ err) { | 179 } catch (/** @type {Error} */ err) { |
180 console.error('Invalid "userinfo" response from server:', err); | 180 console.error('Invalid "userinfo" response from server:', err); |
181 onError(remoting.Error.UNEXPECTED); | 181 onError(remoting.Error.unexpected()); |
182 } | 182 } |
183 } else { | 183 } else { |
184 console.error('Failed to get email. Status: ' + xhr.status + | 184 console.error('Failed to get email. Status: ' + xhr.status + |
185 ' response: ' + xhr.responseText); | 185 ' response: ' + xhr.responseText); |
186 onError(fromHttpStatus(xhr.status)); | 186 onError(fromHttpStatus(xhr.status)); |
187 } | 187 } |
188 }; | 188 }; |
189 remoting.xhr.start({ | 189 remoting.xhr.start({ |
190 method: 'GET', | 190 method: 'GET', |
191 url: this.getOAuth2ApiUserInfoEndpoint_(), | 191 url: this.getOAuth2ApiUserInfoEndpoint_(), |
(...skipping 15 matching lines...) Expand all Loading... |
207 remoting.OAuth2ApiImpl.prototype.getUserInfo = | 207 remoting.OAuth2ApiImpl.prototype.getUserInfo = |
208 function(onDone, onError, token) { | 208 function(onDone, onError, token) { |
209 /** @param {XMLHttpRequest} xhr */ | 209 /** @param {XMLHttpRequest} xhr */ |
210 var onResponse = function(xhr) { | 210 var onResponse = function(xhr) { |
211 if (xhr.status == 200) { | 211 if (xhr.status == 200) { |
212 try { | 212 try { |
213 var result = JSON.parse(xhr.responseText); | 213 var result = JSON.parse(xhr.responseText); |
214 onDone(result['email'], result['name']); | 214 onDone(result['email'], result['name']); |
215 } catch (/** @type {Error} */ err) { | 215 } catch (/** @type {Error} */ err) { |
216 console.error('Invalid "userinfo" response from server:', err); | 216 console.error('Invalid "userinfo" response from server:', err); |
217 onError(remoting.Error.UNEXPECTED); | 217 onError(remoting.Error.unexpected()); |
218 } | 218 } |
219 } else { | 219 } else { |
220 console.error('Failed to get user info. Status: ' + xhr.status + | 220 console.error('Failed to get user info. Status: ' + xhr.status + |
221 ' response: ' + xhr.responseText); | 221 ' response: ' + xhr.responseText); |
222 onError(fromHttpStatus(xhr.status)); | 222 onError(fromHttpStatus(xhr.status)); |
223 } | 223 } |
224 }; | 224 }; |
225 remoting.xhr.start({ | 225 remoting.xhr.start({ |
226 method: 'GET', | 226 method: 'GET', |
227 url: this.getOAuth2ApiUserInfoEndpoint_(), | 227 url: this.getOAuth2ApiUserInfoEndpoint_(), |
228 onDone: onResponse, | 228 onDone: onResponse, |
229 oauthToken: token | 229 oauthToken: token |
230 }); | 230 }); |
231 }; | 231 }; |
232 | 232 |
233 /** @returns {!remoting.Error} */ | 233 /** @returns {!remoting.Error} */ |
234 function fromHttpStatus(/** number */ status) { | 234 function fromHttpStatus(/** number */ status) { |
235 var error = remoting.Error.fromHttpStatus(status); | 235 var error = remoting.Error.fromHttpStatus(status); |
236 if (error === remoting.Error.UNEXPECTED) { | 236 if (error === remoting.Error.unexpected()) { |
237 // Return AUTHENTICATION_FAILED by default, so that the user can try to | 237 // Return AUTHENTICATION_FAILED by default, so that the user can try to |
238 // recover from an unexpected failure by signing in again. | 238 // recover from an unexpected failure by signing in again. |
239 return remoting.Error.AUTHENTICATION_FAILED; | 239 return new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); |
240 } | 240 } |
241 return error; | 241 return error; |
242 } | 242 } |
243 | 243 |
244 /** @type {remoting.OAuth2Api} */ | 244 /** @type {remoting.OAuth2Api} */ |
245 remoting.oauth2Api = new remoting.OAuth2ApiImpl(); | 245 remoting.oauth2Api = new remoting.OAuth2ApiImpl(); |
246 | 246 |
247 })(); | 247 })(); |
OLD | NEW |