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

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

Issue 945033002: Updated XHR API so call sites are more descriptive. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@xhr-test
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 'use strict'; 10 'use strict';
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 {remoting.Xhr.Response} xhrr */
77 var onResponse = function(xhr) { 77 var onResponse = function(xhrr) {
78 if (xhr.status == 200) { 78 if (xhrr.status == 200) {
79 try { 79 try {
80 // Don't use base.jsonParseSafe here unless you also include base.js, 80 // Don't use base.jsonParseSafe here unless you also include base.js,
81 // otherwise this won't work from the OAuth trampoline. 81 // otherwise this won't work from the OAuth trampoline.
82 // TODO(jamiewalch): Fix this once we're no longer using the trampoline. 82 // TODO(jamiewalch): Fix this once we're no longer using the trampoline.
83 var tokens = JSON.parse(xhr.responseText); 83 var tokens = JSON.parse(xhrr.responseText);
84 onDone(tokens['access_token'], tokens['expires_in']); 84 onDone(tokens['access_token'], tokens['expires_in']);
85 } catch (/** @type {Error} */ err) { 85 } catch (/** @type {Error} */ err) {
86 console.error('Invalid "token" response from server:', err); 86 console.error('Invalid "token" response from server:', err);
87 onError(remoting.Error.UNEXPECTED); 87 onError(remoting.Error.UNEXPECTED);
88 } 88 }
89 } else { 89 } else {
90 console.error('Failed to refresh token. Status: ' + xhr.status + 90 console.error('Failed to refresh token. Status: ' + xhrr.status +
91 ' response: ' + xhr.responseText); 91 ' response: ' + xhrr.responseText);
92 onError(remoting.Error.fromHttpStatus(xhr.status)); 92 onError(remoting.Error.fromHttpStatus(xhrr.status));
93 } 93 }
94 }; 94 };
95 95
96 remoting.xhr.start({ 96 new remoting.Xhr({
97 method: 'POST', 97 method: 'POST',
98 url: this.getOAuth2TokenEndpoint_(), 98 url: this.getOAuth2TokenEndpoint_(),
99 onDone: onResponse,
100 formContent: { 99 formContent: {
101 'client_id': clientId, 100 'client_id': clientId,
102 'client_secret': clientSecret, 101 'client_secret': clientSecret,
103 'refresh_token': refreshToken, 102 'refresh_token': refreshToken,
104 'grant_type': 'refresh_token' 103 'grant_type': 'refresh_token'
105 } 104 }
106 }); 105 }).then(onResponse);
107 }; 106 };
108 107
109 /** 108 /**
110 * Asynchronously exchanges an authorization code for access and refresh tokens. 109 * Asynchronously exchanges an authorization code for access and refresh tokens.
111 * 110 *
112 * @param {function(string, string, number): void} onDone Callback to 111 * @param {function(string, string, number): void} onDone Callback to
113 * invoke when the refresh token, access token and access token expiration 112 * invoke when the refresh token, access token and access token expiration
114 * time are successfully fetched. 113 * time are successfully fetched.
115 * @param {function(!remoting.Error):void} onError Callback invoked if an 114 * @param {function(!remoting.Error):void} onError Callback invoked if an
116 * error occurs. 115 * error occurs.
117 * @param {string} clientId OAuth2 client ID. 116 * @param {string} clientId OAuth2 client ID.
118 * @param {string} clientSecret OAuth2 client secret. 117 * @param {string} clientSecret OAuth2 client secret.
119 * @param {string} code OAuth2 authorization code. 118 * @param {string} code OAuth2 authorization code.
120 * @param {string} redirectUri Redirect URI used to obtain this code. 119 * @param {string} redirectUri Redirect URI used to obtain this code.
121 * @return {void} Nothing. 120 * @return {void} Nothing.
122 */ 121 */
123 remoting.OAuth2ApiImpl.prototype.exchangeCodeForTokens = function( 122 remoting.OAuth2ApiImpl.prototype.exchangeCodeForTokens = function(
124 onDone, onError, clientId, clientSecret, code, redirectUri) { 123 onDone, onError, clientId, clientSecret, code, redirectUri) {
125 /** @param {XMLHttpRequest} xhr */ 124 /** @param {remoting.Xhr.Response} xhrr */
126 var onResponse = function(xhr) { 125 var onResponse = function(xhrr) {
127 if (xhr.status == 200) { 126 if (xhrr.status == 200) {
128 try { 127 try {
129 // Don't use base.jsonParseSafe here unless you also include base.js, 128 // Don't use base.jsonParseSafe here unless you also include base.js,
130 // otherwise this won't work from the OAuth trampoline. 129 // otherwise this won't work from the OAuth trampoline.
131 // TODO(jamiewalch): Fix this once we're no longer using the trampoline. 130 // TODO(jamiewalch): Fix this once we're no longer using the trampoline.
132 var tokens = JSON.parse(xhr.responseText); 131 var tokens = JSON.parse(xhrr.responseText);
133 onDone(tokens['refresh_token'], 132 onDone(tokens['refresh_token'],
134 tokens['access_token'], tokens['expires_in']); 133 tokens['access_token'], tokens['expires_in']);
135 } catch (/** @type {Error} */ err) { 134 } catch (/** @type {Error} */ err) {
136 console.error('Invalid "token" response from server:', err); 135 console.error('Invalid "token" response from server:', err);
137 onError(remoting.Error.UNEXPECTED); 136 onError(remoting.Error.UNEXPECTED);
138 } 137 }
139 } else { 138 } else {
140 console.error('Failed to exchange code for token. Status: ' + xhr.status + 139 console.error('Failed to exchange code for token. Status: ' +
141 ' response: ' + xhr.responseText); 140 xhrr.status + ' response: ' + xhrr.responseText);
142 onError(remoting.Error.fromHttpStatus(xhr.status)); 141 onError(remoting.Error.fromHttpStatus(xhrr.status));
143 } 142 }
144 }; 143 };
145 144
146 remoting.xhr.start({ 145 new remoting.Xhr({
147 method: 'POST', 146 method: 'POST',
148 url: this.getOAuth2TokenEndpoint_(), 147 url: this.getOAuth2TokenEndpoint_(),
149 onDone: onResponse,
150 formContent: { 148 formContent: {
151 'client_id': clientId, 149 'client_id': clientId,
152 'client_secret': clientSecret, 150 'client_secret': clientSecret,
153 'redirect_uri': redirectUri, 151 'redirect_uri': redirectUri,
154 'code': code, 152 'code': code,
155 'grant_type': 'authorization_code' 153 'grant_type': 'authorization_code'
156 } 154 }
157 }); 155 }).then(onResponse);
158 }; 156 };
159 157
160 /** 158 /**
161 * Get the user's email address. 159 * Get the user's email address.
162 * 160 *
163 * @param {function(string):void} onDone Callback invoked when the email 161 * @param {function(string):void} onDone Callback invoked when the email
164 * address is available. 162 * address is available.
165 * @param {function(!remoting.Error):void} onError Callback invoked if an 163 * @param {function(!remoting.Error):void} onError Callback invoked if an
166 * error occurs. 164 * error occurs.
167 * @param {string} token Access token. 165 * @param {string} token Access token.
168 * @return {void} Nothing. 166 * @return {void} Nothing.
169 */ 167 */
170 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) { 168 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) {
171 /** @param {XMLHttpRequest} xhr */ 169 /** @param {remoting.Xhr.Response} xhrr */
172 var onResponse = function(xhr) { 170 var onResponse = function(xhrr) {
173 if (xhr.status == 200) { 171 if (xhrr.status == 200) {
174 try { 172 try {
175 var result = JSON.parse(xhr.responseText); 173 var result = JSON.parse(xhrr.responseText);
176 onDone(result['email']); 174 onDone(result['email']);
177 } catch (/** @type {Error} */ err) { 175 } catch (/** @type {Error} */ err) {
178 console.error('Invalid "userinfo" response from server:', err); 176 console.error('Invalid "userinfo" response from server:', err);
179 onError(remoting.Error.UNEXPECTED); 177 onError(remoting.Error.UNEXPECTED);
180 } 178 }
181 } else { 179 } else {
182 console.error('Failed to get email. Status: ' + xhr.status + 180 console.error('Failed to get email. Status: ' + xhrr.status +
183 ' response: ' + xhr.responseText); 181 ' response: ' + xhrr.responseText);
184 onError(remoting.Error.fromHttpStatus(xhr.status)); 182 onError(remoting.Error.fromHttpStatus(xhrr.status));
185 } 183 }
186 }; 184 };
187 remoting.xhr.start({ 185 new remoting.Xhr({
188 method: 'GET', 186 method: 'GET',
189 url: this.getOAuth2ApiUserInfoEndpoint_(), 187 url: this.getOAuth2ApiUserInfoEndpoint_(),
190 onDone: onResponse,
191 oauthToken: token 188 oauthToken: token
192 }); 189 }).then(onResponse);
193 }; 190 };
194 191
195 /** 192 /**
196 * Get the user's email address and full name. 193 * Get the user's email address and full name.
197 * 194 *
198 * @param {function(string, string):void} onDone Callback invoked when the email 195 * @param {function(string, string):void} onDone Callback invoked when the email
199 * address and full name are available. 196 * address and full name are available.
200 * @param {function(!remoting.Error):void} onError Callback invoked if an 197 * @param {function(!remoting.Error):void} onError Callback invoked if an
201 * error occurs. 198 * error occurs.
202 * @param {string} token Access token. 199 * @param {string} token Access token.
203 * @return {void} Nothing. 200 * @return {void} Nothing.
204 */ 201 */
205 remoting.OAuth2ApiImpl.prototype.getUserInfo = 202 remoting.OAuth2ApiImpl.prototype.getUserInfo =
206 function(onDone, onError, token) { 203 function(onDone, onError, token) {
207 /** @param {XMLHttpRequest} xhr */ 204 /** @param {remoting.Xhr.Response} xhrr */
208 var onResponse = function(xhr) { 205 var onResponse = function(xhrr) {
209 if (xhr.status == 200) { 206 if (xhrr.status == 200) {
210 try { 207 try {
211 var result = JSON.parse(xhr.responseText); 208 var result = JSON.parse(xhrr.responseText);
212 onDone(result['email'], result['name']); 209 onDone(result['email'], result['name']);
213 } catch (/** @type {Error} */ err) { 210 } catch (/** @type {Error} */ err) {
214 console.error('Invalid "userinfo" response from server:', err); 211 console.error('Invalid "userinfo" response from server:', err);
215 onError(remoting.Error.UNEXPECTED); 212 onError(remoting.Error.UNEXPECTED);
216 } 213 }
217 } else { 214 } else {
218 console.error('Failed to get user info. Status: ' + xhr.status + 215 console.error('Failed to get user info. Status: ' + xhrr.status +
219 ' response: ' + xhr.responseText); 216 ' response: ' + xhrr.responseText);
220 onError(remoting.Error.fromHttpStatus(xhr.status)); 217 onError(remoting.Error.fromHttpStatus(xhrr.status));
221 } 218 }
222 }; 219 };
223 remoting.xhr.start({ 220 new remoting.Xhr({
224 method: 'GET', 221 method: 'GET',
225 url: this.getOAuth2ApiUserInfoEndpoint_(), 222 url: this.getOAuth2ApiUserInfoEndpoint_(),
226 onDone: onResponse,
227 oauthToken: token 223 oauthToken: token
228 }); 224 }).then(onResponse);
229 }; 225 };
230 226
231 /** @type {remoting.OAuth2Api} */ 227 /** @type {remoting.OAuth2Api} */
232 remoting.oauth2Api = new remoting.OAuth2ApiImpl(); 228 remoting.oauth2Api = new remoting.OAuth2ApiImpl();
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