Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 var remoting = {}; | 5 var remoting = {}; |
| 6 | 6 |
| 7 function setItem(key, value) { | 7 function setItem(key, value) { |
| 8 window.localStorage.setItem(key, value); | 8 window.localStorage.setItem(key, value); |
| 9 } | 9 } |
| 10 | 10 |
| 11 function getItem(key, defaultValue) { | 11 function getItem(key, defaultValue) { |
| 12 var result = window.localStorage.getItem(key); | 12 var result = window.localStorage.getItem(key); |
| 13 return (result != null) ? result : defaultValue; | 13 return (result != null) ? result : defaultValue; |
| 14 } | 14 } |
| 15 | 15 |
| 16 function removeItem(key) { | 16 function removeItem(key) { |
| 17 window.localStorage.removeItem(key); | 17 window.localStorage.removeItem(key); |
| 18 } | 18 } |
| 19 | 19 |
| 20 function clearAll() { | 20 function clearAll() { |
| 21 window.localStorage.clear(); | 21 window.localStorage.clear(); |
| 22 } | 22 } |
| 23 | 23 |
|
Jamie
2011/05/19 21:01:04
Does this belong in this file? It doesn't seem to
awong
2011/05/19 21:33:06
Moved out.
| |
| 24 var oauth = ChromeExOAuth.initBackgroundPage({ | 24 // Declare an OAuth2 class to handle retrieval/storage of an OAuth2 token. |
| 25 'request_url': 'https://www.google.com/accounts/OAuthGetRequestToken', | 25 // |
| 26 'authorize_url': 'https://www.google.com/accounts/OAuthAuthorizeToken', | 26 // Ideally, this should impelment the OAuth2 PostMessage flow to avoid needing |
|
Jamie
2011/05/19 21:01:04
Nit: implement.
awong
2011/05/19 21:33:06
Done.
| |
| 27 'access_url': 'https://www.google.com/accounts/OAuthGetAccessToken', | 27 // to copy and paste a code, but that does not support extension URL schemes |
| 28 'consumer_key': 'anonymous', | 28 // quite yet. Instead, we currently use the native app flow with an |
| 29 'consumer_secret': 'anonymous', | 29 // authorization code that the user must cut/paste. |
| 30 'scope': 'https://www.googleapis.com/auth/chromoting', | 30 function OAuth2() { |
| 31 'app_name': 'Remoting WebApp' | 31 this.OAUTH2_REFRESH_TOKEN_NAME = 'oauth2_refresh_token'; |
| 32 }); | 32 |
| 33 this.client_id = encodeURIComponent( | |
| 34 '440925447803-m890isgsr23kdkcu2erd4mirnrjalf98.' + | |
| 35 'apps.googleusercontent.com'); | |
| 36 this.client_secret = encodeURIComponent('TgKrL73H2kJe6Ir0ufp7bf6e'); | |
| 37 this.scope = encodeURIComponent( | |
| 38 'https://www.googleapis.com/auth/chromoting ' + | |
| 39 'https://www.googleapis.com/auth/googletalk'); | |
| 40 this.redirect_uri = encodeURIComponent('urn:ietf:wg:oauth:2.0:oob'); | |
| 41 } | |
| 42 | |
| 43 OAuth2.prototype.isAuthenticated = function() { | |
| 44 if(this.getRefreshToken()) { | |
| 45 return true; | |
| 46 } | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 OAuth2.prototype.getAccessToken = function() { | |
|
Jamie
2011/05/19 21:01:04
I would expect a get method to have a return state
awong
2011/05/19 21:33:06
Cut and paste error. This function is defined low
| |
| 51 if (!this.isAuthenticated()) { | |
| 52 throw "Not Authenticated"; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 OAuth2.prototype.clear = function() { | |
| 57 removeItem(this.OAUTH2_REFRESH_TOKEN_NAME); | |
| 58 delete this.access_token; | |
| 59 delete this.access_token_expiration; | |
| 60 } | |
| 61 | |
| 62 OAuth2.prototype.setRefreshToken = function(token) { | |
| 63 setItem(this.OAUTH2_REFRESH_TOKEN_NAME, token); | |
| 64 } | |
| 65 | |
| 66 OAuth2.prototype.getRefreshToken = function(token) { | |
| 67 return getItem(this.OAUTH2_REFRESH_TOKEN_NAME); | |
| 68 } | |
| 69 | |
| 70 OAuth2.prototype.setAccessToken = function(token, expiration) { | |
| 71 this.access_token = token; | |
| 72 this.access_token_expiration = expiration; | |
| 73 } | |
| 74 | |
| 75 OAuth2.prototype.needsNewAccessToken = function() { | |
| 76 if (!this.isAuthenticated()) { | |
| 77 throw "Not Authenticated."; | |
| 78 } | |
| 79 if (!this.access_token) { | |
| 80 return true; | |
| 81 } | |
| 82 if (Date.now() > this.access_token_expiration) { | |
| 83 return true; | |
| 84 } | |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 OAuth2.prototype.getAccessToken = function() { | |
| 89 if (this.needsNewAccessToken()) { | |
| 90 throw "Access Token expired."; | |
| 91 } | |
| 92 return this.access_token; | |
| 93 } | |
| 94 | |
| 95 OAuth2.prototype.refreshAccessToken = function(on_done) { | |
| 96 if (!this.isAuthenticated()) { | |
| 97 throw "Not Authenticated."; | |
| 98 } | |
| 99 var xhr = new XMLHttpRequest(); | |
| 100 var that = this; | |
|
Jamie
2011/05/19 21:01:04
:)
awong
2011/05/19 21:33:06
Heh...apparently "var that = this;" is a common ja
| |
| 101 xhr.onreadystatechange = function() { | |
| 102 if (xhr.readyState != 4) { | |
| 103 return; | |
| 104 } | |
| 105 if (xhr.status == 200) { | |
| 106 tokens = JSON.parse(xhr.responseText); | |
| 107 that.setAccessToken(tokens['access_token'], | |
| 108 tokens['expires_in'] * 1000 + Date.now()); | |
| 109 } else { | |
| 110 console.log("Refresh access token failed. Status: " + xhr.status + | |
| 111 " response: " + xhr.responseText); | |
| 112 } | |
| 113 on_done(); | |
| 114 }; | |
| 115 xhr.open('POST', 'https://accounts.google.com/o/oauth2/token', true); | |
| 116 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | |
| 117 var post_data = 'client_id=' + this.client_id | |
| 118 + '&client_secret=' + this.client_secret | |
| 119 + '&refresh_token=' + encodeURIComponent(this.getRefreshToken()) | |
| 120 + '&grant_type=refresh_token'; | |
| 121 xhr.send(post_data); | |
| 122 } | |
| 123 | |
| 124 OAuth2.prototype.openOAuth2Window = function() { | |
| 125 var GET_CODE_URL = 'https://accounts.google.com/o/oauth2/auth?' | |
| 126 + 'client_id=' + this.client_id | |
| 127 + '&redirect_uri=' + this.redirect_uri | |
| 128 + '&scope=' + this.scope | |
| 129 + '&response_type=code'; | |
| 130 window.open(GET_CODE_URL); | |
| 131 } | |
| 132 | |
| 133 OAuth2.prototype.exchangeCodeForToken = function(code, on_done) { | |
| 134 var xhr = new XMLHttpRequest(); | |
| 135 var that = this; | |
| 136 xhr.onreadystatechange = function() { | |
| 137 if (xhr.readyState != 4) { | |
| 138 return; | |
| 139 } | |
| 140 if (xhr.status == 200) { | |
| 141 tokens = JSON.parse(xhr.responseText); | |
| 142 that.setRefreshToken(tokens['refresh_token']); | |
| 143 that.setAccessToken(tokens['access_token'], | |
| 144 tokens['expires_in'] + Date.now()); | |
| 145 } else { | |
| 146 console.log("Code exchnage failed. Status: " + xhr.status + | |
| 147 " response: " + xhr.responseText); | |
| 148 } | |
| 149 on_done(); | |
| 150 }; | |
| 151 xhr.open('POST', 'https://accounts.google.com/o/oauth2/token', true); | |
| 152 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | |
| 153 var post_data = 'client_id=' + this.client_id | |
| 154 + '&client_secret=' + this.client_secret | |
| 155 + '&redirect_uri=' + this.redirect_uri | |
| 156 + '&code=' + encodeURIComponent(code) | |
| 157 + '&grant_type=authorization_code'; | |
| 158 xhr.send(post_data); | |
| 159 } | |
| 160 | |
| 161 var oauth2 = new OAuth2(); | |
| OLD | NEW |