diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-22 21:09:06 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-22 21:09:06 +0000 |
commit | 052dd201bc811f4af5a8dc9c0645d20f7e870371 (patch) | |
tree | b8c9a5f1168a79fa01e9349f3f87472be3e8ee99 /remoting | |
parent | 1d403342d4ab0daa82f3e4ab6df72416ec412787 (diff) | |
download | chromium_src-052dd201bc811f4af5a8dc9c0645d20f7e870371.zip chromium_src-052dd201bc811f4af5a8dc9c0645d20f7e870371.tar.gz chromium_src-052dd201bc811f4af5a8dc9c0645d20f7e870371.tar.bz2 |
Retrieve e-mail programmatically based on OAuth2 token.
BUG=none
TEST=manual
Review URL: http://codereview.chromium.org/7202010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90101 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/webapp/me2mom/choice.css | 9 | ||||
-rw-r--r-- | remoting/webapp/me2mom/choice.html | 19 | ||||
-rw-r--r-- | remoting/webapp/me2mom/oauth2.js | 21 | ||||
-rw-r--r-- | remoting/webapp/me2mom/remoting.js | 47 |
4 files changed, 71 insertions, 25 deletions
diff --git a/remoting/webapp/me2mom/choice.css b/remoting/webapp/me2mom/choice.css index d651cd8..1ca6378 100644 --- a/remoting/webapp/me2mom/choice.css +++ b/remoting/webapp/me2mom/choice.css @@ -179,6 +179,15 @@ section { float: right; } +#email-status { + margin-right: 0.5ex; +} + +#email-label { + color: black; + font-weight: bold; +} + #current-email { color: rgba(0, 0, 0, 0.5); } diff --git a/remoting/webapp/me2mom/choice.html b/remoting/webapp/me2mom/choice.html index bc6a623..8bfa444 100644 --- a/remoting/webapp/me2mom/choice.html +++ b/remoting/webapp/me2mom/choice.html @@ -23,6 +23,11 @@ found in the LICENSE file. <body> <div id="auth-panel"> + <span id="email-status" class="display-inline"> + <span id="email-label">Email:</span> + <span id="current-email" class="display-inline"></span> + </span> <!-- email-status --> + <span id="oauth2-entry"> <form action="" onsubmit="authorizeOAuth2(); return false;"> <label for="oauth2-code">OAuth2 Token:</label> @@ -38,20 +43,6 @@ found in the LICENSE file. </form> </span> <!-- oauth2-entry --> - <span id="email-entry"> - <form action="" - onsubmit="setEmail(this['new-email'].value); return false;"> - <label for="new-email">Email:</label> - <span id="current-email" class="display-inline"></span> - <input id="new-email" class="display-inline" name="new-email" - type="text" /> - <input id="email-submit-button" class="display-inline" - type="submit" /> - <input id="change-email-button" class="display-inline" type="button" - onclick="setEmail(''); return false;" value="Change Email" /> - </form> - </span> <!-- email-entry --> - <span id="debug-enable"> <form> <input id="debug-log-toggle" class="display-inline" type="button" diff --git a/remoting/webapp/me2mom/oauth2.js b/remoting/webapp/me2mom/oauth2.js index be987d7..8ffa3b0 100644 --- a/remoting/webapp/me2mom/oauth2.js +++ b/remoting/webapp/me2mom/oauth2.js @@ -20,7 +20,8 @@ function OAuth2() { this.client_secret = encodeURIComponent('TgKrL73H2kJe6Ir0ufp7bf6e'); this.scope = encodeURIComponent( 'https://www.googleapis.com/auth/chromoting ' + - 'https://www.googleapis.com/auth/googletalk'); + 'https://www.googleapis.com/auth/googletalk ' + + 'https://www.googleapis.com/auth/userinfo#email'); this.redirect_uri = encodeURIComponent('urn:ietf:wg:oauth:2.0:oob'); } @@ -139,3 +140,21 @@ OAuth2.prototype.exchangeCodeForToken = function(code, on_done) { xhr.send(post_data); } +// Call myfunc with an access token as the only parameter. +// +// This will refresh the access token if necessary. If the access token +// cannot be refreshed, an error is thrown. +OAuth2.prototype.callWithToken = function(myfunc) { + if (remoting.oauth2.needsNewAccessToken()) { + remoting.oauth2.refreshAccessToken(function() { + if (remoting.oauth2.needsNewAccessToken()) { + // If we still need it, we're going to infinite loop. + throw "Unable to get access token."; + } + myfunc(remoting.oauth2.getAccessToken()); + }); + return; + } + + myfunc(remoting.oauth2.getAccessToken()); +} diff --git a/remoting/webapp/me2mom/remoting.js b/remoting/webapp/me2mom/remoting.js index 158f09f..7ceb276 100644 --- a/remoting/webapp/me2mom/remoting.js +++ b/remoting/webapp/me2mom/remoting.js @@ -12,7 +12,7 @@ var remoting = chrome.extension.getBackgroundPage().remoting; remoting.CLIENT_MODE = 'client'; remoting.HOST_MODE = 'host'; -remoting.XMPP_LOGIN_NAME = 'xmpp_login'; +remoting.EMAIL = 'email'; remoting.HOST_PLUGIN_ID = 'host-plugin-id'; window.addEventListener('load', init_, false); @@ -27,6 +27,33 @@ function hasClass(element, cls) { return element.className.match(new RegExp('\\b' + cls + '\\b')); } +function retrieveEmail_(access_token) { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState != 4) { + return; + } + if (xhr.status != 200) { + // TODO(ajwong): Have a better way of showing an error. + window.alert("Unable to get e-mail"); + return; + } + + // TODO(ajwong): See if we can't find a JSON endpoint. + setEmail(xhr.responseText.split('&')[0].split('=')[1]); + }; + + xhr.open('GET', 'https://www.googleapis.com/userinfo/email', true); + xhr.setRequestHeader('Authorization', 'OAuth ' + access_token); + xhr.send(null); +} + +function refreshEmail_() { + if (!remoting.getItem(remoting.EMAIL) && remoting.oauth2.isAuthenticated()) { + remoting.oauth2.callWithToken(retrieveEmail_); + } +} + function addClass(element, cls) { if (!hasClass(element, cls)) element.className = element.className + ' ' + cls; @@ -74,14 +101,10 @@ function updateAuthStatus_() { showElementById('oauth2-code-button', !oauthValid); showElementById('oauth2-clear-button', oauthValid); - var loginName = remoting.getItem(remoting.XMPP_LOGIN_NAME); + var loginName = remoting.getItem(remoting.EMAIL); if (loginName) { document.getElementById('current-email').innerText = loginName; } - showElementById('current-email', loginName); - showElementById('change-email-button', loginName); - showElementById('new-email', !loginName); - showElementById('email-submit-button', !loginName); var disableControls = !(loginName && oauthValid); var authPanel = document.getElementById('auth-panel'); @@ -101,7 +124,7 @@ function initBackgroundFuncs_() { } function setEmail(value) { - remoting.setItem(remoting.XMPP_LOGIN_NAME, value); + remoting.setItem(remoting.EMAIL, value); updateAuthStatus_(); } @@ -109,7 +132,10 @@ function exchangedCodeForToken_() { if (!remoting.oauth2.isAuthenticated()) { alert('Your OAuth2 token was invalid. Please try again.'); } - updateAuthStatus_(); + remoting.oauth2.callWithToken(function(token) { + retrieveEmail_(token); + updateAuthStatus_(); + }); } function authorizeOAuth2() { @@ -139,6 +165,7 @@ function setMode_(mode, modes) { function init_() { initBackgroundFuncs_(); updateAuthStatus_(); + refreshEmail_(); setHostMode('unshared'); setClientMode('unconnected'); setGlobalMode(remoting.getItem('startup-mode', remoting.HOST_MODE)); @@ -219,7 +246,7 @@ function tryShare() { div.appendChild(plugin); plugin.onStateChanged = onStateChanged_; plugin.logDebugInfoCallback = debugInfoCallback_; - plugin.connect(remoting.getItem(remoting.XMPP_LOGIN_NAME), + plugin.connect(remoting.getItem(remoting.EMAIL), 'oauth2:' + remoting.oauth2.getAccessToken()); } @@ -274,7 +301,7 @@ function cancelShare() { function startSession_() { addToDebugLog('Starting session...'); - remoting.username = remoting.getItem(remoting.XMPP_LOGIN_NAME); + remoting.username = remoting.getItem(remoting.EMAIL); document.location = 'remoting_session.html'; } |