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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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: ' + xhr.status + |
91 ' response: ' + xhr.responseText); | 91 ' response: ' + xhr.responseText); |
92 onError(remoting.Error.fromHttpStatus(xhr.status)); | 92 onError(remoting.Error.fromHttpStatus(xhr.status)); |
93 } | 93 } |
94 }; | 94 }; |
95 | 95 |
96 var parameters = { | 96 remoting.xhr.start({ |
97 'client_id': clientId, | 97 method: 'POST', |
98 'client_secret': clientSecret, | 98 url: this.getOAuth2TokenEndpoint_(), |
99 'refresh_token': refreshToken, | 99 onDone: onResponse, |
100 'grant_type': 'refresh_token' | 100 formContent: { |
101 }; | 101 'client_id': clientId, |
102 | 102 'client_secret': clientSecret, |
103 remoting.xhr.post(this.getOAuth2TokenEndpoint_(), onResponse, parameters); | 103 'refresh_token': refreshToken, |
| 104 'grant_type': 'refresh_token' |
| 105 } |
| 106 }); |
104 }; | 107 }; |
105 | 108 |
106 /** | 109 /** |
107 * Asynchronously exchanges an authorization code for access and refresh tokens. | 110 * Asynchronously exchanges an authorization code for access and refresh tokens. |
108 * | 111 * |
109 * @param {function(string, string, number): void} onDone Callback to | 112 * @param {function(string, string, number): void} onDone Callback to |
110 * invoke when the refresh token, access token and access token expiration | 113 * invoke when the refresh token, access token and access token expiration |
111 * time are successfully fetched. | 114 * time are successfully fetched. |
112 * @param {function(remoting.Error):void} onError Callback invoked if an | 115 * @param {function(remoting.Error):void} onError Callback invoked if an |
113 * error occurs. | 116 * error occurs. |
(...skipping 19 matching lines...) Expand all Loading... |
133 console.error('Invalid "token" response from server:', err); | 136 console.error('Invalid "token" response from server:', err); |
134 onError(remoting.Error.UNEXPECTED); | 137 onError(remoting.Error.UNEXPECTED); |
135 } | 138 } |
136 } else { | 139 } else { |
137 console.error('Failed to exchange code for token. Status: ' + xhr.status + | 140 console.error('Failed to exchange code for token. Status: ' + xhr.status + |
138 ' response: ' + xhr.responseText); | 141 ' response: ' + xhr.responseText); |
139 onError(remoting.Error.fromHttpStatus(xhr.status)); | 142 onError(remoting.Error.fromHttpStatus(xhr.status)); |
140 } | 143 } |
141 }; | 144 }; |
142 | 145 |
143 var parameters = { | 146 remoting.xhr.start({ |
144 'client_id': clientId, | 147 method: 'POST', |
145 'client_secret': clientSecret, | 148 url: this.getOAuth2TokenEndpoint_(), |
146 'redirect_uri': redirectUri, | 149 onDone: onResponse, |
147 'code': code, | 150 formContent: { |
148 'grant_type': 'authorization_code' | 151 'client_id': clientId, |
149 }; | 152 'client_secret': clientSecret, |
150 remoting.xhr.post(this.getOAuth2TokenEndpoint_(), onResponse, parameters); | 153 'redirect_uri': redirectUri, |
| 154 'code': code, |
| 155 'grant_type': 'authorization_code' |
| 156 } |
| 157 }); |
151 }; | 158 }; |
152 | 159 |
153 /** | 160 /** |
154 * Get the user's email address. | 161 * Get the user's email address. |
155 * | 162 * |
156 * @param {function(string):void} onDone Callback invoked when the email | 163 * @param {function(string):void} onDone Callback invoked when the email |
157 * address is available. | 164 * address is available. |
158 * @param {function(remoting.Error):void} onError Callback invoked if an | 165 * @param {function(remoting.Error):void} onError Callback invoked if an |
159 * error occurs. | 166 * error occurs. |
160 * @param {string} token Access token. | 167 * @param {string} token Access token. |
161 * @return {void} Nothing. | 168 * @return {void} Nothing. |
162 */ | 169 */ |
163 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) { | 170 remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) { |
164 /** @param {XMLHttpRequest} xhr */ | 171 /** @param {XMLHttpRequest} xhr */ |
165 var onResponse = function(xhr) { | 172 var onResponse = function(xhr) { |
166 if (xhr.status == 200) { | 173 if (xhr.status == 200) { |
167 try { | 174 try { |
168 var result = JSON.parse(xhr.responseText); | 175 var result = JSON.parse(xhr.responseText); |
169 onDone(result['email']); | 176 onDone(result['email']); |
170 } catch (/** @type {Error} */ err) { | 177 } catch (/** @type {Error} */ err) { |
171 console.error('Invalid "userinfo" response from server:', err); | 178 console.error('Invalid "userinfo" response from server:', err); |
172 onError(remoting.Error.UNEXPECTED); | 179 onError(remoting.Error.UNEXPECTED); |
173 } | 180 } |
174 } else { | 181 } else { |
175 console.error('Failed to get email. Status: ' + xhr.status + | 182 console.error('Failed to get email. Status: ' + xhr.status + |
176 ' response: ' + xhr.responseText); | 183 ' response: ' + xhr.responseText); |
177 onError(remoting.Error.fromHttpStatus(xhr.status)); | 184 onError(remoting.Error.fromHttpStatus(xhr.status)); |
178 } | 185 } |
179 }; | 186 }; |
180 var headers = { 'Authorization': 'OAuth ' + token }; | 187 remoting.xhr.start({ |
181 remoting.xhr.get(this.getOAuth2ApiUserInfoEndpoint_(), | 188 method: 'GET', |
182 onResponse, '', headers); | 189 url: this.getOAuth2ApiUserInfoEndpoint_(), |
| 190 onDone: onResponse, |
| 191 oauthToken: token |
| 192 }); |
183 }; | 193 }; |
184 | 194 |
185 /** | 195 /** |
186 * Get the user's email address and full name. | 196 * Get the user's email address and full name. |
187 * | 197 * |
188 * @param {function(string, string):void} onDone Callback invoked when the email | 198 * @param {function(string, string):void} onDone Callback invoked when the email |
189 * address and full name are available. | 199 * address and full name are available. |
190 * @param {function(remoting.Error):void} onError Callback invoked if an | 200 * @param {function(remoting.Error):void} onError Callback invoked if an |
191 * error occurs. | 201 * error occurs. |
192 * @param {string} token Access token. | 202 * @param {string} token Access token. |
(...skipping 10 matching lines...) Expand all Loading... |
203 } catch (/** @type {Error} */ err) { | 213 } catch (/** @type {Error} */ err) { |
204 console.error('Invalid "userinfo" response from server:', err); | 214 console.error('Invalid "userinfo" response from server:', err); |
205 onError(remoting.Error.UNEXPECTED); | 215 onError(remoting.Error.UNEXPECTED); |
206 } | 216 } |
207 } else { | 217 } else { |
208 console.error('Failed to get user info. Status: ' + xhr.status + | 218 console.error('Failed to get user info. Status: ' + xhr.status + |
209 ' response: ' + xhr.responseText); | 219 ' response: ' + xhr.responseText); |
210 onError(remoting.Error.fromHttpStatus(xhr.status)); | 220 onError(remoting.Error.fromHttpStatus(xhr.status)); |
211 } | 221 } |
212 }; | 222 }; |
213 var headers = { 'Authorization': 'OAuth ' + token }; | 223 remoting.xhr.start({ |
214 remoting.xhr.get(this.getOAuth2ApiUserInfoEndpoint_(), | 224 method: 'GET', |
215 onResponse, '', headers); | 225 url: this.getOAuth2ApiUserInfoEndpoint_(), |
| 226 onDone: onResponse, |
| 227 oauthToken: token |
| 228 }); |
216 }; | 229 }; |
217 | 230 |
218 /** @type {remoting.OAuth2Api} */ | 231 /** @type {remoting.OAuth2Api} */ |
219 remoting.oauth2Api = new remoting.OAuth2ApiImpl(); | 232 remoting.oauth2Api = new remoting.OAuth2ApiImpl(); |
OLD | NEW |