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 'use strict'; | 10 'use strict'; |
(...skipping 21 matching lines...) Expand all Loading... |
32 remoting.OAuth2ApiImpl.prototype.getOAuth2ApiUserInfoEndpoint_ = function() { | 32 remoting.OAuth2ApiImpl.prototype.getOAuth2ApiUserInfoEndpoint_ = function() { |
33 return remoting.settings.OAUTH2_API_BASE_URL + '/v1/userinfo'; | 33 return remoting.settings.OAUTH2_API_BASE_URL + '/v1/userinfo'; |
34 }; | 34 }; |
35 | 35 |
36 | 36 |
37 /** | 37 /** |
38 * Interprets HTTP error responses in authentication XMLHttpRequests. | 38 * Interprets HTTP error responses in authentication XMLHttpRequests. |
39 * | 39 * |
40 * @private | 40 * @private |
41 * @param {number} xhrStatus Status (HTTP response code) of the XMLHttpRequest. | 41 * @param {number} xhrStatus Status (HTTP response code) of the XMLHttpRequest. |
42 * @return {remoting.Error} An error code to be raised. | 42 * @return {!remoting.Error} An error code to be raised. |
43 */ | 43 */ |
44 remoting.OAuth2ApiImpl.prototype.interpretXhrStatus_ = | 44 remoting.OAuth2ApiImpl.prototype.interpretXhrStatus_ = |
45 function(xhrStatus) { | 45 function(xhrStatus) { |
46 // Return AUTHENTICATION_FAILED by default, so that the user can try to | 46 // Return AUTHENTICATION_FAILED by default, so that the user can try to |
47 // recover from an unexpected failure by signing in again. | 47 // recover from an unexpected failure by signing in again. |
48 /** @type {remoting.Error} */ | 48 /** @type {!remoting.Error} */ |
49 var error = remoting.Error.AUTHENTICATION_FAILED; | 49 var error = remoting.Error.AUTHENTICATION_FAILED; |
50 if (xhrStatus == 400 || xhrStatus == 401 || xhrStatus == 403) { | 50 if (xhrStatus == 400 || xhrStatus == 401 || xhrStatus == 403) { |
51 error = remoting.Error.AUTHENTICATION_FAILED; | 51 error = remoting.Error.AUTHENTICATION_FAILED; |
52 } else if (xhrStatus == 502 || xhrStatus == 503) { | 52 } else if (xhrStatus == 502 || xhrStatus == 503) { |
53 error = remoting.Error.SERVICE_UNAVAILABLE; | 53 error = remoting.Error.SERVICE_UNAVAILABLE; |
54 } else if (xhrStatus == 0) { | 54 } else if (xhrStatus == 0) { |
55 error = remoting.Error.NETWORK_FAILURE; | 55 error = remoting.Error.NETWORK_FAILURE; |
56 } else { | 56 } else { |
57 console.warn('Unexpected authentication response code: ' + xhrStatus); | 57 console.warn('Unexpected authentication response code: ' + xhrStatus); |
58 } | 58 } |
59 return error; | 59 return error; |
60 }; | 60 }; |
61 | 61 |
62 /** | 62 /** |
63 * Asynchronously retrieves a new access token from the server. | 63 * Asynchronously retrieves a new access token from the server. |
64 * | 64 * |
65 * @param {function(string, number): void} onDone Callback to invoke when | 65 * @param {function(string, number): void} onDone Callback to invoke when |
66 * the access token and expiration time are successfully fetched. | 66 * the access token and expiration time are successfully fetched. |
67 * @param {function(remoting.Error):void} onError Callback invoked if an | 67 * @param {function(!remoting.Error):void} onError Callback invoked if an |
68 * error occurs. | 68 * error occurs. |
69 * @param {string} clientId OAuth2 client ID. | 69 * @param {string} clientId OAuth2 client ID. |
70 * @param {string} clientSecret OAuth2 client secret. | 70 * @param {string} clientSecret OAuth2 client secret. |
71 * @param {string} refreshToken OAuth2 refresh token to be redeemed. | 71 * @param {string} refreshToken OAuth2 refresh token to be redeemed. |
72 * @return {void} Nothing. | 72 * @return {void} Nothing. |
73 */ | 73 */ |
74 remoting.OAuth2ApiImpl.prototype.refreshAccessToken = function( | 74 remoting.OAuth2ApiImpl.prototype.refreshAccessToken = function( |
75 onDone, onError, clientId, clientSecret, refreshToken) { | 75 onDone, onError, clientId, clientSecret, refreshToken) { |
76 /** @param {XMLHttpRequest} xhr */ | 76 /** @param {XMLHttpRequest} xhr */ |
77 var onResponse = function(xhr) { | 77 var onResponse = function(xhr) { |
(...skipping 27 matching lines...) Expand all Loading... |
105 } | 105 } |
106 }); | 106 }); |
107 }; | 107 }; |
108 | 108 |
109 /** | 109 /** |
110 * Asynchronously exchanges an authorization code for access and refresh tokens. | 110 * Asynchronously exchanges an authorization code for access and refresh tokens. |
111 * | 111 * |
112 * @param {function(string, string, number): void} onDone Callback to | 112 * @param {function(string, string, number): void} onDone Callback to |
113 * invoke when the refresh token, access token and access token expiration | 113 * invoke when the refresh token, access token and access token expiration |
114 * time are successfully fetched. | 114 * time are successfully fetched. |
115 * @param {function(remoting.Error):void} onError Callback invoked if an | 115 * @param {function(!remoting.Error):void} onError Callback invoked if an |
116 * error occurs. | 116 * error occurs. |
117 * @param {string} clientId OAuth2 client ID. | 117 * @param {string} clientId OAuth2 client ID. |
118 * @param {string} clientSecret OAuth2 client secret. | 118 * @param {string} clientSecret OAuth2 client secret. |
119 * @param {string} code OAuth2 authorization code. | 119 * @param {string} code OAuth2 authorization code. |
120 * @param {string} redirectUri Redirect URI used to obtain this code. | 120 * @param {string} redirectUri Redirect URI used to obtain this code. |
121 * @return {void} Nothing. | 121 * @return {void} Nothing. |
122 */ | 122 */ |
123 remoting.OAuth2ApiImpl.prototype.exchangeCodeForTokens = function( | 123 remoting.OAuth2ApiImpl.prototype.exchangeCodeForTokens = function( |
124 onDone, onError, clientId, clientSecret, code, redirectUri) { | 124 onDone, onError, clientId, clientSecret, code, redirectUri) { |
125 /** @param {XMLHttpRequest} xhr */ | 125 /** @param {XMLHttpRequest} xhr */ |
(...skipping 29 matching lines...) Expand all Loading... |
155 'grant_type': 'authorization_code' | 155 'grant_type': 'authorization_code' |
156 } | 156 } |
157 }); | 157 }); |
158 }; | 158 }; |
159 | 159 |
160 /** | 160 /** |
161 * Get the user's email address. | 161 * Get the user's email address. |
162 * | 162 * |
163 * @param {function(string):void} onDone Callback invoked when the email | 163 * @param {function(string):void} onDone Callback invoked when the email |
164 * address is available. | 164 * address is available. |
165 * @param {function(remoting.Error):void} onError Callback invoked if an | 165 * @param {function(!remoting.Error):void} onError Callback invoked if an |
166 * error occurs. | 166 * error occurs. |
167 * @param {string} token Access token. | 167 * @param {string} token Access token. |
168 * @return {void} Nothing. | 168 * @return {void} Nothing. |
169 */ | 169 */ |
170 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) { | 170 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) { |
171 /** @param {XMLHttpRequest} xhr */ | 171 /** @param {XMLHttpRequest} xhr */ |
172 var onResponse = function(xhr) { | 172 var onResponse = function(xhr) { |
173 if (xhr.status == 200) { | 173 if (xhr.status == 200) { |
174 try { | 174 try { |
175 var result = JSON.parse(xhr.responseText); | 175 var result = JSON.parse(xhr.responseText); |
(...skipping 14 matching lines...) Expand all Loading... |
190 onDone: onResponse, | 190 onDone: onResponse, |
191 oauthToken: token | 191 oauthToken: token |
192 }); | 192 }); |
193 }; | 193 }; |
194 | 194 |
195 /** | 195 /** |
196 * Get the user's email address and full name. | 196 * Get the user's email address and full name. |
197 * | 197 * |
198 * @param {function(string, string):void} onDone Callback invoked when the email | 198 * @param {function(string, string):void} onDone Callback invoked when the email |
199 * address and full name are available. | 199 * address and full name are available. |
200 * @param {function(remoting.Error):void} onError Callback invoked if an | 200 * @param {function(!remoting.Error):void} onError Callback invoked if an |
201 * error occurs. | 201 * error occurs. |
202 * @param {string} token Access token. | 202 * @param {string} token Access token. |
203 * @return {void} Nothing. | 203 * @return {void} Nothing. |
204 */ | 204 */ |
205 remoting.OAuth2ApiImpl.prototype.getUserInfo = | 205 remoting.OAuth2ApiImpl.prototype.getUserInfo = |
206 function(onDone, onError, token) { | 206 function(onDone, onError, token) { |
207 /** @param {XMLHttpRequest} xhr */ | 207 /** @param {XMLHttpRequest} xhr */ |
208 var onResponse = function(xhr) { | 208 var onResponse = function(xhr) { |
209 if (xhr.status == 200) { | 209 if (xhr.status == 200) { |
210 try { | 210 try { |
(...skipping 12 matching lines...) Expand all Loading... |
223 remoting.xhr.start({ | 223 remoting.xhr.start({ |
224 method: 'GET', | 224 method: 'GET', |
225 url: this.getOAuth2ApiUserInfoEndpoint_(), | 225 url: this.getOAuth2ApiUserInfoEndpoint_(), |
226 onDone: onResponse, | 226 onDone: onResponse, |
227 oauthToken: token | 227 oauthToken: token |
228 }); | 228 }); |
229 }; | 229 }; |
230 | 230 |
231 /** @type {remoting.OAuth2Api} */ | 231 /** @type {remoting.OAuth2Api} */ |
232 remoting.oauth2Api = new remoting.OAuth2ApiImpl(); | 232 remoting.oauth2Api = new remoting.OAuth2ApiImpl(); |
OLD | NEW |