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

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

Powered by Google App Engine
This is Rietveld 408576698