Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Side by Side Diff: remoting/webapp/crd/js/oauth2_api_impl.js

Issue 1003433002: Updated remoting.xhr API to use promises. Removed access to the native (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@spy-promise
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/webapp/crd/js/oauth2.js ('k') | remoting/webapp/crd/js/session_connector_impl.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 * the access token and expiration time are successfully fetched. 68 * the access token and expiration time are successfully fetched.
69 * @param {function(!remoting.Error):void} onError Callback invoked if an 69 * @param {function(!remoting.Error):void} onError Callback invoked if an
70 * error occurs. 70 * error occurs.
71 * @param {string} clientId OAuth2 client ID. 71 * @param {string} clientId OAuth2 client ID.
72 * @param {string} clientSecret OAuth2 client secret. 72 * @param {string} clientSecret OAuth2 client secret.
73 * @param {string} refreshToken OAuth2 refresh token to be redeemed. 73 * @param {string} refreshToken OAuth2 refresh token to be redeemed.
74 * @return {void} Nothing. 74 * @return {void} Nothing.
75 */ 75 */
76 remoting.OAuth2ApiImpl.prototype.refreshAccessToken = function( 76 remoting.OAuth2ApiImpl.prototype.refreshAccessToken = function(
77 onDone, onError, clientId, clientSecret, refreshToken) { 77 onDone, onError, clientId, clientSecret, refreshToken) {
78 /** @param {XMLHttpRequest} xhr */ 78 /** @param {!remoting.Xhr.Response} response */
79 var onResponse = function(xhr) { 79 var onResponse = function(response) {
80 if (xhr.status == 200) { 80 if (response.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(response.getText());
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: ' + response.status +
93 ' response: ' + xhr.responseText); 93 ' response: ' + response.getText());
94 onError(fromHttpStatus(xhr.status)); 94 onError(remoting.Error.fromHttpStatus(response.status));
95 } 95 }
96 }; 96 };
97 97
98 remoting.xhr.start({ 98 new remoting.Xhr({
99 method: 'POST', 99 method: 'POST',
100 url: this.getOAuth2TokenEndpoint_(), 100 url: this.getOAuth2TokenEndpoint_(),
101 onDone: onResponse,
102 formContent: { 101 formContent: {
103 'client_id': clientId, 102 'client_id': clientId,
104 'client_secret': clientSecret, 103 'client_secret': clientSecret,
105 'refresh_token': refreshToken, 104 'refresh_token': refreshToken,
106 'grant_type': 'refresh_token' 105 'grant_type': 'refresh_token'
107 } 106 }
108 }); 107 }).start().then(onResponse);
109 }; 108 };
110 109
111 /** 110 /**
112 * Asynchronously exchanges an authorization code for access and refresh tokens. 111 * Asynchronously exchanges an authorization code for access and refresh tokens.
113 * 112 *
114 * @param {function(string, string, number): void} onDone Callback to 113 * @param {function(string, string, number): void} onDone Callback to
115 * invoke when the refresh token, access token and access token expiration 114 * invoke when the refresh token, access token and access token expiration
116 * time are successfully fetched. 115 * time are successfully fetched.
117 * @param {function(!remoting.Error):void} onError Callback invoked if an 116 * @param {function(!remoting.Error):void} onError Callback invoked if an
118 * error occurs. 117 * error occurs.
119 * @param {string} clientId OAuth2 client ID. 118 * @param {string} clientId OAuth2 client ID.
120 * @param {string} clientSecret OAuth2 client secret. 119 * @param {string} clientSecret OAuth2 client secret.
121 * @param {string} code OAuth2 authorization code. 120 * @param {string} code OAuth2 authorization code.
122 * @param {string} redirectUri Redirect URI used to obtain this code. 121 * @param {string} redirectUri Redirect URI used to obtain this code.
123 * @return {void} Nothing. 122 * @return {void} Nothing.
124 */ 123 */
125 remoting.OAuth2ApiImpl.prototype.exchangeCodeForTokens = function( 124 remoting.OAuth2ApiImpl.prototype.exchangeCodeForTokens = function(
126 onDone, onError, clientId, clientSecret, code, redirectUri) { 125 onDone, onError, clientId, clientSecret, code, redirectUri) {
127 /** @param {XMLHttpRequest} xhr */ 126 /** @param {!remoting.Xhr.Response} response */
128 var onResponse = function(xhr) { 127 var onResponse = function(response) {
129 if (xhr.status == 200) { 128 if (response.status == 200) {
130 try { 129 try {
131 // Don't use base.jsonParseSafe here unless you also include base.js, 130 // Don't use base.jsonParseSafe here unless you also include base.js,
132 // otherwise this won't work from the OAuth trampoline. 131 // otherwise this won't work from the OAuth trampoline.
133 // TODO(jamiewalch): Fix this once we're no longer using the trampoline. 132 // TODO(jamiewalch): Fix this once we're no longer using the trampoline.
134 var tokens = JSON.parse(xhr.responseText); 133 var tokens = JSON.parse(response.getText());
135 onDone(tokens['refresh_token'], 134 onDone(tokens['refresh_token'],
136 tokens['access_token'], tokens['expires_in']); 135 tokens['access_token'], tokens['expires_in']);
137 } catch (/** @type {Error} */ err) { 136 } catch (/** @type {Error} */ err) {
138 console.error('Invalid "token" response from server:', err); 137 console.error('Invalid "token" response from server:', err);
139 onError(remoting.Error.unexpected()); 138 onError(remoting.Error.unexpected());
140 } 139 }
141 } else { 140 } else {
142 console.error('Failed to exchange code for token. Status: ' + xhr.status + 141 console.error('Failed to exchange code for token. Status: ' +
143 ' response: ' + xhr.responseText); 142 response.status + ' response: ' + response.getText());
144 onError(fromHttpStatus(xhr.status)); 143 onError(remoting.Error.fromHttpStatus(response.status));
145 } 144 }
146 }; 145 };
147 146
148 remoting.xhr.start({ 147 new remoting.Xhr({
149 method: 'POST', 148 method: 'POST',
150 url: this.getOAuth2TokenEndpoint_(), 149 url: this.getOAuth2TokenEndpoint_(),
151 onDone: onResponse,
152 formContent: { 150 formContent: {
153 'client_id': clientId, 151 'client_id': clientId,
154 'client_secret': clientSecret, 152 'client_secret': clientSecret,
155 'redirect_uri': redirectUri, 153 'redirect_uri': redirectUri,
156 'code': code, 154 'code': code,
157 'grant_type': 'authorization_code' 155 'grant_type': 'authorization_code'
158 } 156 }
159 }); 157 }).start().then(onResponse);
160 }; 158 };
161 159
162 /** 160 /**
163 * Get the user's email address. 161 * Get the user's email address.
164 * 162 *
165 * @param {function(string):void} onDone Callback invoked when the email 163 * @param {function(string):void} onDone Callback invoked when the email
166 * address is available. 164 * address is available.
167 * @param {function(!remoting.Error):void} onError Callback invoked if an 165 * @param {function(!remoting.Error):void} onError Callback invoked if an
168 * error occurs. 166 * error occurs.
169 * @param {string} token Access token. 167 * @param {string} token Access token.
170 * @return {void} Nothing. 168 * @return {void} Nothing.
171 */ 169 */
172 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) { 170 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) {
173 /** @param {XMLHttpRequest} xhr */ 171 /** @param {!remoting.Xhr.Response} response */
174 var onResponse = function(xhr) { 172 var onResponse = function(response) {
175 if (xhr.status == 200) { 173 if (response.status == 200) {
176 try { 174 try {
177 var result = JSON.parse(xhr.responseText); 175 var result = JSON.parse(response.getText());
178 onDone(result['email']); 176 onDone(result['email']);
179 } catch (/** @type {Error} */ err) { 177 } catch (/** @type {Error} */ err) {
180 console.error('Invalid "userinfo" response from server:', err); 178 console.error('Invalid "userinfo" response from server:', err);
181 onError(remoting.Error.unexpected()); 179 onError(remoting.Error.unexpected());
182 } 180 }
183 } else { 181 } else {
184 console.error('Failed to get email. Status: ' + xhr.status + 182 console.error('Failed to get email. Status: ' + response.status +
185 ' response: ' + xhr.responseText); 183 ' response: ' + response.getText());
186 onError(fromHttpStatus(xhr.status)); 184 onError(remoting.Error.fromHttpStatus(response.status));
187 } 185 }
188 }; 186 };
189 remoting.xhr.start({ 187 new remoting.Xhr({
190 method: 'GET', 188 method: 'GET',
191 url: this.getOAuth2ApiUserInfoEndpoint_(), 189 url: this.getOAuth2ApiUserInfoEndpoint_(),
192 onDone: onResponse,
193 oauthToken: token 190 oauthToken: token
194 }); 191 }).start().then(onResponse);
195 }; 192 };
196 193
197 /** 194 /**
198 * Get the user's email address and full name. 195 * Get the user's email address and full name.
199 * 196 *
200 * @param {function(string, string):void} onDone Callback invoked when the email 197 * @param {function(string, string):void} onDone Callback invoked when the email
201 * address and full name are available. 198 * address and full name are available.
202 * @param {function(!remoting.Error):void} onError Callback invoked if an 199 * @param {function(!remoting.Error):void} onError Callback invoked if an
203 * error occurs. 200 * error occurs.
204 * @param {string} token Access token. 201 * @param {string} token Access token.
205 * @return {void} Nothing. 202 * @return {void} Nothing.
206 */ 203 */
207 remoting.OAuth2ApiImpl.prototype.getUserInfo = 204 remoting.OAuth2ApiImpl.prototype.getUserInfo =
208 function(onDone, onError, token) { 205 function(onDone, onError, token) {
209 /** @param {XMLHttpRequest} xhr */ 206 /** @param {!remoting.Xhr.Response} response */
210 var onResponse = function(xhr) { 207 var onResponse = function(response) {
211 if (xhr.status == 200) { 208 if (response.status == 200) {
212 try { 209 try {
213 var result = JSON.parse(xhr.responseText); 210 var result = JSON.parse(response.getText());
214 onDone(result['email'], result['name']); 211 onDone(result['email'], result['name']);
215 } catch (/** @type {Error} */ err) { 212 } catch (/** @type {Error} */ err) {
216 console.error('Invalid "userinfo" response from server:', err); 213 console.error('Invalid "userinfo" response from server:', err);
217 onError(remoting.Error.unexpected()); 214 onError(remoting.Error.unexpected());
218 } 215 }
219 } else { 216 } else {
220 console.error('Failed to get user info. Status: ' + xhr.status + 217 console.error('Failed to get user info. Status: ' + response.status +
221 ' response: ' + xhr.responseText); 218 ' response: ' + response.getText());
222 onError(fromHttpStatus(xhr.status)); 219 onError(remoting.Error.fromHttpStatus(response.status));
223 } 220 }
224 }; 221 };
225 remoting.xhr.start({ 222 new remoting.Xhr({
226 method: 'GET', 223 method: 'GET',
227 url: this.getOAuth2ApiUserInfoEndpoint_(), 224 url: this.getOAuth2ApiUserInfoEndpoint_(),
228 onDone: onResponse,
229 oauthToken: token 225 oauthToken: token
230 }); 226 }).start().then(onResponse);
231 }; 227 };
232 228
233 /** @returns {!remoting.Error} */ 229 /** @returns {!remoting.Error} */
234 function fromHttpStatus(/** number */ status) { 230 function fromHttpStatus(/** number */ status) {
235 var error = remoting.Error.fromHttpStatus(status); 231 var error = remoting.Error.fromHttpStatus(status);
236 if (error === remoting.Error.unexpected()) { 232 if (error === remoting.Error.unexpected()) {
237 // Return AUTHENTICATION_FAILED by default, so that the user can try to 233 // Return AUTHENTICATION_FAILED by default, so that the user can try to
238 // recover from an unexpected failure by signing in again. 234 // recover from an unexpected failure by signing in again.
239 return new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); 235 return new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED);
240 } 236 }
241 return error; 237 return error;
242 } 238 }
243 239
244 /** @type {remoting.OAuth2Api} */ 240 /** @type {remoting.OAuth2Api} */
245 remoting.oauth2Api = new remoting.OAuth2ApiImpl(); 241 remoting.oauth2Api = new remoting.OAuth2ApiImpl();
246 242
247 })(); 243 })();
OLDNEW
« no previous file with comments | « remoting/webapp/crd/js/oauth2.js ('k') | remoting/webapp/crd/js/session_connector_impl.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698