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

Side by Side Diff: remoting/webapp/me2mom/remoting.js

Issue 7046012: Basic OAuth2 support using the native app flow. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more fixes Created 9 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // TODO(ajwong): This seems like a bad idea. Why are we doing it like this?
5 var remoting = chrome.extension.getBackgroundPage().remoting; 6 var remoting = chrome.extension.getBackgroundPage().remoting;
7
6 XMPP_LOGIN_NAME = 'xmpp_login'; 8 XMPP_LOGIN_NAME = 'xmpp_login';
7 XMPP_TOKEN_NAME = 'xmpp_token'; 9 XMPP_TOKEN_NAME = 'xmpp_token';
8 OAUTH2_TOKEN_NAME = 'oauth2_token';
9 HOST_PLUGIN_ID = 'host_plugin_id'; 10 HOST_PLUGIN_ID = 'host_plugin_id';
10 11
11 function updateAuthStatus_() { 12 function updateAuthStatus_() {
12 var oauth1_status = document.getElementById('oauth1_status'); 13 var oauth2_status = document.getElementById('oauth2_status');
13 if (remoting.oauth.hasToken()) { 14 if (remoting.oauth2.isAuthenticated()) {
14 oauth1_status.innerText = 'OK'; 15 oauth2_status.innerText = 'OK';
15 oauth1_status.style.color = 'green'; 16 oauth2_status.style.color = 'green';
17 document.getElementById('oauth2_code_button').style.display = 'none';
18 document.getElementById('oauth2_clear_button').style.display = 'inline';
19 document.getElementById('oauth2_form').style.display = 'none';
16 } else { 20 } else {
17 oauth1_status.innerText = 'Unauthorized'; 21 oauth2_status.innerText = 'Unauthorized';
18 oauth1_status.style.color = 'red'; 22 oauth2_status.style.color = 'red';
23 document.getElementById('oauth2_code_button').style.display = 'inline';
24 document.getElementById('oauth2_clear_button').style.display = 'none';
25 document.getElementById('oauth2_form').style.display = 'inline';
19 } 26 }
20 var xmpp_status = document.getElementById('xmpp_status'); 27 var xmpp_status = document.getElementById('xmpp_status');
21 if (remoting.getItem(XMPP_TOKEN_NAME) && remoting.getItem(XMPP_LOGIN_NAME)) { 28 if (remoting.getItem(XMPP_TOKEN_NAME) && remoting.getItem(XMPP_LOGIN_NAME)) {
22 document.getElementById('xmpp_clear').style.display = 'inline'; 29 document.getElementById('xmpp_clear').style.display = 'inline';
23 document.getElementById('xmpp_form').style.display = 'none'; 30 document.getElementById('xmpp_form').style.display = 'none';
24 xmpp_status.innerText = 'OK'; 31 xmpp_status.innerText = 'OK';
25 xmpp_status.style.color = 'green'; 32 xmpp_status.style.color = 'green';
26 remoting.xmppAuthToken = remoting.getItem(XMPP_TOKEN_NAME); 33 remoting.xmppAuthToken = remoting.getItem(XMPP_TOKEN_NAME);
27 } else { 34 } else {
28 document.getElementById('xmpp_clear').style.display = 'none'; 35 document.getElementById('xmpp_clear').style.display = 'none';
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 78
72 function initAuthPanel_() { 79 function initAuthPanel_() {
73 updateAuthStatus_(); 80 updateAuthStatus_();
74 resetXmppErrors_(); 81 resetXmppErrors_();
75 } 82 }
76 83
77 function initBackgroundFuncs_() { 84 function initBackgroundFuncs_() {
78 remoting.getItem = chrome.extension.getBackgroundPage().getItem; 85 remoting.getItem = chrome.extension.getBackgroundPage().getItem;
79 remoting.setItem = chrome.extension.getBackgroundPage().setItem; 86 remoting.setItem = chrome.extension.getBackgroundPage().setItem;
80 remoting.removeItem = chrome.extension.getBackgroundPage().removeItem; 87 remoting.removeItem = chrome.extension.getBackgroundPage().removeItem;
81 remoting.oauth = chrome.extension.getBackgroundPage().oauth; 88 remoting.oauth2 = chrome.extension.getBackgroundPage().oauth2;
82 } 89 }
83 90
84 function authorizeXmpp(form) { 91 function authorizeXmpp(form) {
85 var xhr = new XMLHttpRequest(); 92 var xhr = new XMLHttpRequest();
86 var captcha_result = readAndClearCaptcha_(form); 93 var captcha_result = readAndClearCaptcha_(form);
87 94
88 xhr.onreadystatechange = function() { 95 xhr.onreadystatechange = function() {
89 if (xhr.readyState != 4) { 96 if (xhr.readyState != 4) {
90 return; 97 return;
91 } 98 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 '&Email=' + encodeURIComponent(form['xmpp_username'].value) + 131 '&Email=' + encodeURIComponent(form['xmpp_username'].value) +
125 '&Passwd=' + encodeURIComponent(form['xmpp_password'].value); 132 '&Passwd=' + encodeURIComponent(form['xmpp_password'].value);
126 133
127 if (captcha_result[0]) { 134 if (captcha_result[0]) {
128 post_data += '&logintoken=' + encodeURIComponent(captcha_result[0]) + 135 post_data += '&logintoken=' + encodeURIComponent(captcha_result[0]) +
129 '&logincaptcha=' + encodeURIComponent(captcha_result[1]); 136 '&logincaptcha=' + encodeURIComponent(captcha_result[1]);
130 } 137 }
131 xhr.send(post_data); 138 xhr.send(post_data);
132 } 139 }
133 140
134 function authorizeOAuth1() { 141 function authorizeOAuth2(code) {
135 remoting.oauth.authorize(updateAuthStatus_); 142 remoting.oauth2.exchangeCodeForToken(code, updateAuthStatus_);
136 } 143 }
137 144
138 function clearOAuth1() { 145 function clearOAuth2() {
139 remoting.oauth.clearTokens(); 146 remoting.oauth2.clear();
140 updateAuthStatus_(); 147 updateAuthStatus_();
141 } 148 }
142 149
143 function clearXmpp() { 150 function clearXmpp() {
144 remoting.removeItem(XMPP_TOKEN_NAME); 151 remoting.removeItem(XMPP_TOKEN_NAME);
145 updateAuthStatus_(); 152 updateAuthStatus_();
146 } 153 }
147 154
148 // Show the div with id |mode| and hide those with other ids in |modes|. 155 // Show the div with id |mode| and hide those with other ids in |modes|.
149 function setMode_(mode, modes) { 156 function setMode_(mode, modes) {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 } else { 243 } else {
237 invalid.style.display = 'none'; 244 invalid.style.display = 'none';
238 other.style.display = 'block'; 245 other.style.display = 'block';
239 var responseNode = document.getElementById('server_response'); 246 var responseNode = document.getElementById('server_response');
240 responseNode.innerText = responseString + ' (' + responseCode + ')'; 247 responseNode.innerText = responseString + ' (' + responseCode + ')';
241 } 248 }
242 remoting.accessCode = ''; 249 remoting.accessCode = '';
243 setClientMode('connect_failed'); 250 setClientMode('connect_failed');
244 } 251 }
245 252
246 function parseServerResponse_(reply, xhr) { 253 function parseServerResponse_(xhr) {
247 if (xhr.status == 200) { 254 if (xhr.status == 200) {
248 var host = JSON.parse(xhr.responseText); 255 var host = JSON.parse(xhr.responseText);
249 if (host.data && host.data.jabberId) { 256 if (host.data && host.data.jabberId) {
250 remoting.hostjid = host.data.jabberId; 257 remoting.hostjid = host.data.jabberId;
251 startSession_(); 258 startSession_();
252 return; 259 return;
253 } 260 }
254 } 261 }
255 showConnectError_(xhr.status, xhr.responseText); 262 showConnectError_(xhr.status, xhr.responseText);
256 } 263 }
257 264
265 function resolveSupportId(support_id) {
266 var xhr = new XMLHttpRequest();
267 xhr.onreadystatechange = function() {
268 if (xhr.readyState != 4) {
269 return;
270 }
271 parseServerResponse_(xhr);
272 };
273
274 xhr.open('GET',
275 'https://www.googleapis.com/chromoting/v1/support-hosts/' +
276 encodeURIComponent(support_id),
277 true);
278 xhr.setRequestHeader('Authorization',
279 'OAuth ' + remoting.oauth2.getAccessToken());
280 xhr.send(null);
281 }
282
258 function tryConnect(form) { 283 function tryConnect(form) {
259 remoting.accessCode = form['access_code_entry'].value; 284 remoting.accessCode = form['access_code_entry'].value;
260 // TODO(jamiewalch): Since the mapping from (SupportId, HostSecret) to 285 // TODO(jamiewalch): Since the mapping from (SupportId, HostSecret) to
261 // AccessCode is not yet defined, assume it's hyphen-separated for now. 286 // AccessCode is not yet defined, assume it's hyphen-separated for now.
262 var parts = remoting.accessCode.split('-'); 287 var parts = remoting.accessCode.split('-');
263 if (parts.length != 2) { 288 if (parts.length != 2) {
264 showConnectError_(404); 289 showConnectError_(404);
265 } else { 290 } else {
266 setClientMode('connecting'); 291 setClientMode('connecting');
267 var urlBase = 'https://www.googleapis.com/chromoting/v1/support-hosts/'; 292 if (remoting.oauth2.needsNewAccessToken()) {
Jamie 2011/05/19 21:01:04 Should needsNewAccessToken take RTT into account,
awong 2011/05/19 21:33:06 I timeshifted it on the setting of hte expiration.
268 remoting.oauth.sendSignedRequest( 293 remoting.oauth2.refreshAccessToken(function() {
269 urlBase + '' + encodeURIComponent(parts[0]) + '', 294 resolveSupportId(parts[0]);
270 parseServerResponse_); 295 });
296 return;
297 } else {
298 resolveSupportId(parts[0]);
299 }
271 } 300 }
272 } 301 }
273 302
274 function cancelConnect() { 303 function cancelConnect() {
275 remoting.accessCode = ''; 304 remoting.accessCode = '';
276 setClientMode('unconnected'); 305 setClientMode('unconnected');
277 } 306 }
OLDNEW
« remoting/webapp/me2mom/main.css ('K') | « remoting/webapp/me2mom/manifest.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698