OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 * UI Pages. Note the order must be in sync with the | 6 * UI Pages. Note the order must be in sync with the |
7 * ArcAuthService::UIPage enum. | 7 * ArcAuthService::UIPage enum. |
8 * @type {Array<string>} | 8 * @type {Array<string>} |
9 */ | 9 */ |
10 var UI_PAGES = ['none', | 10 var UI_PAGES = ['none', |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 return; | 117 return; |
118 } | 118 } |
119 | 119 |
120 var doc = appWindow.contentWindow.document; | 120 var doc = appWindow.contentWindow.document; |
121 var pages = doc.getElementsByClassName('section'); | 121 var pages = doc.getElementsByClassName('section'); |
122 for (var i = 0; i < pages.length; i++) { | 122 for (var i = 0; i < pages.length; i++) { |
123 pages[i].hidden = pages[i].id != pageDivId; | 123 pages[i].hidden = pages[i].id != pageDivId; |
124 } | 124 } |
125 | 125 |
126 if (pageDivId == 'lso-loading') { | 126 if (pageDivId == 'lso-loading') { |
127 webview.src = 'https://accounts.google.com/o/oauth2/programmatic_auth?' + | 127 webview.src = 'https://accounts.google.com/o/oauth2/v2/auth?client_id=' + |
128 'scope=https://www.google.com/accounts/OAuthLogin&client_id' + | 128 '1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.' + |
129 '=1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.' + | 129 'googleusercontent.com&response_type=code&redirect_uri=oob&' + |
130 'googleusercontent.com'; | 130 'scope=https://www.google.com/accounts/OAuthLogin'; |
131 } | 131 } |
132 appWindow.show(); | 132 appWindow.show(); |
133 } | 133 } |
134 | 134 |
135 /** | 135 /** |
136 * Sets error message. | |
137 * @param {string} error message. | |
138 */ | |
139 function setErrorMessage(error) { | |
140 if (!appWindow) { | |
141 return; | |
142 } | |
143 var doc = appWindow.contentWindow.document; | |
144 var messageElement = doc.getElementById('error-message'); | |
145 messageElement.innerText = error; | |
146 } | |
147 | |
148 /** | |
136 * Shows requested page. | 149 * Shows requested page. |
137 * @param {int} pageId Index of the page to show. Must be in the array range of | 150 * @param {int} pageId Index of the page to show. Must be in the array range of |
138 * UI_PAGES. | 151 * UI_PAGES. |
139 * @param {string} status associated with page string status, error message for | 152 * @param {string} status associated with page string status, error message for |
140 * example. | 153 * example. |
141 */ | 154 */ |
142 function showPageWithStatus(pageId, status) { | 155 function showPageWithStatus(pageId, status) { |
143 if (!appWindow) { | 156 if (!appWindow) { |
144 return; | 157 return; |
145 } | 158 } |
146 | 159 |
147 if (UI_PAGES[pageId] == 'error') { | 160 if (UI_PAGES[pageId] == 'error') { |
148 var doc = appWindow.contentWindow.document; | 161 setErrorMessage(status); |
149 var messageElement = doc.getElementById('error-message'); | |
150 messageElement.innerText = status; | |
151 } | 162 } |
152 showPage(UI_PAGES[pageId]); | 163 showPage(UI_PAGES[pageId]); |
153 } | 164 } |
154 | 165 |
155 chrome.app.runtime.onLaunched.addListener(function() { | 166 chrome.app.runtime.onLaunched.addListener(function() { |
156 var onAppContentLoad = function() { | 167 var onAppContentLoad = function() { |
157 var doc = appWindow.contentWindow.document; | 168 var doc = appWindow.contentWindow.document; |
158 webview = doc.getElementById('arc-support'); | 169 webview = doc.getElementById('arc-support'); |
159 | 170 |
171 var isApprovalResponse = function(details) { | |
172 var resultUrlPrefix = | |
173 'https://accounts.google.com/o/oauth2/approval?'; | |
174 return details.statusCode == 200 && | |
175 details.url.substring(0, resultUrlPrefix.length) == | |
176 resultUrlPrefix; | |
177 }; | |
178 | |
179 var onWebviewRequestResponseStarted = function(details) { | |
180 if (isApprovalResponse(details)) { | |
181 // Hide LSO page that displays auth code. | |
182 showPage('arc-loading'); | |
183 } else { | |
184 showPage('lso'); | |
185 } | |
186 }; | |
187 | |
160 var onWebviewRequestCompleted = function(details) { | 188 var onWebviewRequestCompleted = function(details) { |
161 showPage('lso'); | 189 if (!isApprovalResponse(details)) { |
162 var resultUrlPrefix = | 190 return; |
163 'https://accounts.google.com/o/oauth2/programmatic_auth?'; | |
164 if (details.statusCode == 200 && | |
165 details.url.substring(0, resultUrlPrefix.length) == resultUrlPrefix) { | |
166 sendNativeMessage('checkAuthCode'); | |
167 } | 191 } |
192 | |
193 var attemptCount = 10; | |
khmel
2016/03/30 15:24:55
This LSO page returns auth code in title.
I know,
| |
194 var extractAuthCode = function() { | |
195 if (webview.src == details.url) { | |
196 webview.executeScript({code: 'document.title;' | |
197 }, function(results) { | |
198 var authCodePrefix = 'Success code='; | |
199 if (results[0].substring(0, authCodePrefix.length) == | |
200 authCodePrefix) { | |
201 var authCode = results[0].substring(authCodePrefix.length); | |
202 console.log('Auth code:' + authCode); | |
203 var message = { | |
204 'action': 'setAuthCode', | |
205 'code': authCode | |
206 }; | |
207 port.postMessage(message); | |
208 } else { | |
209 setErrorMessage('Authorization failed.'); | |
210 showPage('error'); | |
211 } | |
212 }); | |
213 } else { | |
214 if (attemptCount > 0) { | |
215 attemptCount -= 1; | |
216 setTimeout(extractAuthCode, 100); | |
217 } else { | |
218 setErrorMessage('Authorization time out.'); | |
219 showPage('error'); | |
220 } | |
221 } | |
222 }; | |
223 | |
224 extractAuthCode(); | |
168 }; | 225 }; |
169 | 226 |
170 var requestFilter = { | 227 var requestFilter = { |
171 urls: ['<all_urls>'], | 228 urls: ['<all_urls>'], |
172 types: ['main_frame'] | 229 types: ['main_frame'] |
173 }; | 230 }; |
174 | 231 |
175 webview.request.onCompleted.addListener(onWebviewRequestCompleted, | 232 webview.request.onCompleted.addListener(onWebviewRequestCompleted, |
176 requestFilter); | 233 requestFilter); |
234 webview.request.onResponseStarted.addListener( | |
235 onWebviewRequestResponseStarted, requestFilter); | |
177 | 236 |
178 var onGetStarted = function() { | 237 var onGetStarted = function() { |
179 sendNativeMessage('checkAuthCode'); | 238 sendNativeMessage('startLSO'); |
180 }; | 239 }; |
181 | 240 |
182 var onRetry = function() { | 241 var onRetry = function() { |
183 sendNativeMessage('checkAuthCode'); | 242 sendNativeMessage('startLSO'); |
184 }; | 243 }; |
185 | 244 |
186 doc.getElementById('get-started').addEventListener('click', onGetStarted); | 245 doc.getElementById('get-started').addEventListener('click', onGetStarted); |
187 doc.getElementById('retry').addEventListener('click', onRetry); | 246 doc.getElementById('retry').addEventListener('click', onRetry); |
188 | 247 |
189 connectPort(); | 248 connectPort(); |
190 }; | 249 }; |
191 | 250 |
192 var onWindowCreated = function(createdWindow) { | 251 var onWindowCreated = function(createdWindow) { |
193 appWindow = createdWindow; | 252 appWindow = createdWindow; |
(...skipping 17 matching lines...) Expand all Loading... | |
211 type: 'chrome', | 270 type: 'chrome', |
212 color: '#67a030' | 271 color: '#67a030' |
213 }, | 272 }, |
214 'innerBounds': { | 273 'innerBounds': { |
215 'width': 800, | 274 'width': 800, |
216 'height': 600 | 275 'height': 600 |
217 } | 276 } |
218 }; | 277 }; |
219 chrome.app.window.create('main.html', options, onWindowCreated); | 278 chrome.app.window.create('main.html', options, onWindowCreated); |
220 }); | 279 }); |
OLD | NEW |