| 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'; |
| 11 | 11 |
| 12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
| 13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
| 14 | 14 |
| 15 /** @constructor */ | 15 /** @constructor */ |
| 16 remoting.OAuth2Api = function() { | 16 remoting.OAuth2Api = function() { |
| 17 }; | 17 }; |
| 18 | 18 |
| 19 /** @private | 19 /** @private |
| 20 * @return {string} OAuth2 token URL. | 20 * @return {string} OAuth2 token URL. |
| 21 */ | 21 */ |
| 22 remoting.OAuth2Api.getOAuth2TokenEndpoint_ = function() { | 22 remoting.OAuth2Api.getOAuth2TokenEndpoint_ = function() { |
| 23 return remoting.settings.OAUTH2_BASE_URL + '/token'; | 23 return remoting.settings.OAUTH2_BASE_URL + '/token'; |
| 24 }; | 24 }; |
| 25 | 25 |
| 26 /** @private | 26 /** @private |
| 27 * @return {string} OAuth token revocation URL. | |
| 28 */ | |
| 29 remoting.OAuth2Api.getOAuth2RevokeTokenEndpoint_ = function() { | |
| 30 return remoting.settings.OAUTH2_BASE_URL + '/revoke'; | |
| 31 }; | |
| 32 | |
| 33 /** @private | |
| 34 * @return {string} OAuth2 userinfo API URL. | 27 * @return {string} OAuth2 userinfo API URL. |
| 35 */ | 28 */ |
| 36 remoting.OAuth2Api.getOAuth2ApiUserInfoEndpoint_ = function() { | 29 remoting.OAuth2Api.getOAuth2ApiUserInfoEndpoint_ = function() { |
| 37 return remoting.settings.OAUTH2_API_BASE_URL + '/v1/userinfo'; | 30 return remoting.settings.OAUTH2_API_BASE_URL + '/v1/userinfo'; |
| 38 }; | 31 }; |
| 39 | 32 |
| 40 | 33 |
| 41 /** | 34 /** |
| 42 * Interprets HTTP error responses in authentication XMLHttpRequests. | 35 * Interprets HTTP error responses in authentication XMLHttpRequests. |
| 43 * | 36 * |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 } else { | 176 } else { |
| 184 console.error('Failed to get email. Status: ' + xhr.status + | 177 console.error('Failed to get email. Status: ' + xhr.status + |
| 185 ' response: ' + xhr.responseText); | 178 ' response: ' + xhr.responseText); |
| 186 onError(remoting.OAuth2Api.interpretXhrStatus_(xhr.status)); | 179 onError(remoting.OAuth2Api.interpretXhrStatus_(xhr.status)); |
| 187 } | 180 } |
| 188 }; | 181 }; |
| 189 var headers = { 'Authorization': 'OAuth ' + token }; | 182 var headers = { 'Authorization': 'OAuth ' + token }; |
| 190 remoting.xhr.get(remoting.OAuth2Api.getOAuth2ApiUserInfoEndpoint_(), | 183 remoting.xhr.get(remoting.OAuth2Api.getOAuth2ApiUserInfoEndpoint_(), |
| 191 onResponse, '', headers); | 184 onResponse, '', headers); |
| 192 }; | 185 }; |
| 193 | |
| 194 /** | |
| 195 * Revokes a refresh or an access token. | |
| 196 * | |
| 197 * @param {function():void} onDone Callback invoked when the token is | |
| 198 * revoked. | |
| 199 * @param {function(remoting.Error):void} onError Callback invoked if an | |
| 200 * error occurs. | |
| 201 * @param {string} token An access or refresh token. | |
| 202 * @return {void} Nothing. | |
| 203 */ | |
| 204 remoting.OAuth2Api.revokeToken = function(onDone, onError, token) { | |
| 205 /** @param {XMLHttpRequest} xhr */ | |
| 206 var onResponse = function(xhr) { | |
| 207 if (xhr.status == 200) { | |
| 208 onDone(); | |
| 209 } else { | |
| 210 console.error('Failed to revoke token. Status: ' + xhr.status + | |
| 211 ' response: ' + xhr.responseText); | |
| 212 onError(remoting.OAuth2Api.interpretXhrStatus_(xhr.status)); | |
| 213 } | |
| 214 }; | |
| 215 | |
| 216 var parameters = { 'token': token }; | |
| 217 remoting.xhr.post(remoting.OAuth2Api.getOAuth2RevokeTokenEndpoint_(), | |
| 218 onResponse, parameters); | |
| 219 }; | |
| OLD | NEW |