OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // Flag to indicate whether or not to show offline hosts in the host list. | 5 // Flag to indicate whether or not to show offline hosts in the host list. |
6 chromoting.showOfflineHosts = true; | 6 chromoting.showOfflineHosts = true; |
7 | 7 |
8 // String to identify bad auth tokens. | 8 // String to identify bad auth tokens. |
9 var BAD_AUTH_TOKEN = 'bad_token'; | 9 var BAD_AUTH_TOKEN = 'bad_token'; |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 // Update the login status region (at the bottom of the popup) with the | 42 // Update the login status region (at the bottom of the popup) with the |
43 // current account and links to sign in/out. | 43 // current account and links to sign in/out. |
44 function updateLoginStatus() { | 44 function updateLoginStatus() { |
45 var username = getCookie('username'); | 45 var username = getCookie('username'); |
46 | 46 |
47 var loginDiv = document.getElementById('login_div'); | 47 var loginDiv = document.getElementById('login_div'); |
48 clear(loginDiv); | 48 clear(loginDiv); |
49 | 49 |
50 if (!username) { | 50 if (!username) { |
51 var signinLink = document.createElement('a'); | 51 var signinLink = document.createElement('a'); |
52 signinLink.setAttribute('href', | 52 signinLink.setAttribute('href', 'javascript:showDirectoryLogin();'); |
53 "javascript:window.open('login.html', 'Sign In', " + | |
54 "'width=400,height=200,scrollbars=no'); return false;"); | |
55 signinLink.appendChild(document.createTextNode('Sign In')); | 53 signinLink.appendChild(document.createTextNode('Sign In')); |
56 loginDiv.appendChild(signinLink); | 54 loginDiv.appendChild(signinLink); |
57 } else { | 55 } else { |
58 var email = document.createElement('span'); | 56 var email = document.createElement('span'); |
59 email.setAttribute('class', 'login_email'); | 57 email.setAttribute('class', 'login_email'); |
60 email.appendChild(document.createTextNode(username)); | 58 email.appendChild(document.createTextNode(username)); |
61 loginDiv.appendChild(email); | 59 loginDiv.appendChild(email); |
62 | 60 |
63 loginDiv.appendChild(document.createTextNode(' | ')); | 61 loginDiv.appendChild(document.createTextNode(' | ')); |
64 | 62 |
65 var signoutLink = document.createElement('a'); | 63 var signoutLink = document.createElement('a'); |
66 signoutLink.setAttribute('href', 'javascript:logout(this.form);'); | 64 signoutLink.setAttribute('href', 'javascript:logoutAndReload(this.form);'); |
67 signoutLink.appendChild(document.createTextNode('Sign Out')); | 65 signoutLink.appendChild(document.createTextNode('Sign Out')); |
68 loginDiv.appendChild(signoutLink); | 66 loginDiv.appendChild(signoutLink); |
69 } | 67 } |
70 } | 68 } |
71 | 69 |
| 70 // Sign out the current user and reload the host list. |
| 71 function logoutAndReload(form) { |
| 72 logout(); |
| 73 populateHostList(); |
| 74 } |
| 75 |
72 // Sign out the current user by erasing the auth cookies. | 76 // Sign out the current user by erasing the auth cookies. |
73 function logout(form) { | 77 function logout() { |
74 setCookie('username', '', AUTH_EXPIRES); | 78 setCookie('username', '', AUTH_EXPIRES); |
75 setCookie('chromoting_auth', '', AUTH_EXPIRES); | 79 setCookie('chromoting_auth', '', AUTH_EXPIRES); |
76 setCookie('xmpp_auth', '', AUTH_EXPIRES); | 80 setCookie('xmpp_auth', '', AUTH_EXPIRES); |
77 | 81 |
78 updateLoginStatus(); | 82 updateLoginStatus(); |
79 populateHostList(); | |
80 } | 83 } |
81 | 84 |
82 function login(form) { | 85 // Sign in to Chromoting Directory services. |
83 var status = document.getElementById('login_status'); | 86 function showDirectoryLogin() { |
84 clear(status); | 87 document.getElementById("login_panel").style.display = "block"; |
85 doLogin(form.username.value, form.password.value, checkLogin); | 88 } |
| 89 |
| 90 function login() { |
| 91 var username = document.getElementById("username").value; |
| 92 var password = document.getElementById("password").value; |
| 93 |
| 94 doLogin(username, password, checkLogin); |
86 } | 95 } |
87 | 96 |
88 // Check to see if the login was successful. | 97 // Check to see if the login was successful. |
89 function checkLogin() { | 98 function checkLogin() { |
90 var username = getCookie('username'); | 99 var username = getCookie('username'); |
91 var cauth = getCookie('chromoting_auth'); | 100 var cauth = getCookie('chromoting_auth'); |
92 var xauth = getCookie('xmpp_auth'); | 101 var xauth = getCookie('xmpp_auth'); |
93 | 102 |
94 // Verify login and show login status. | 103 // Verify login and show login status. |
95 var status = document.getElementById('login_status'); | |
96 if (cauth == BAD_AUTH_TOKEN || xauth == BAD_AUTH_TOKEN) { | 104 if (cauth == BAD_AUTH_TOKEN || xauth == BAD_AUTH_TOKEN) { |
97 appendMessage(status, '', 'Sign in failed!'); | 105 // Erase the username cookie. |
98 if (username) { | 106 setCookie('username', '', AUTH_EXPIRES); |
99 setCookie('username', '', AUTH_EXPIRES); | 107 showLoginError("Sign in failed!"); |
100 } | |
101 } else { | 108 } else { |
102 appendMessage(status, '', 'Successfully signed in as ' + username); | 109 // Successful login - update status and update host list. |
| 110 updateLoginStatus(); |
| 111 populateHostList(); |
| 112 |
| 113 // Hide login dialog and clear out values. |
| 114 document.getElementById('login_panel').style.display = "none"; |
| 115 document.getElementById('username').value = ""; |
| 116 document.getElementById('password').value = ""; |
103 } | 117 } |
104 } | 118 } |
105 | 119 |
106 function doLogin(username, password, done) { | 120 function doLogin(username, password, done) { |
107 // Don't call |done| callback until both login requests have completed. | 121 // Don't call |done| callback until both login requests have completed. |
108 var count = 2; | 122 var count = 2; |
109 var barrier = function() { | 123 var barrier = function() { |
110 count--; | 124 count--; |
111 if (done && count == 0) { | 125 if (done && count == 0) { |
112 done(); | 126 done(); |
(...skipping 26 matching lines...) Expand all Loading... |
139 } | 153 } |
140 }; | 154 }; |
141 | 155 |
142 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | 156 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); |
143 xhr.send('accountType=HOSTED_OR_GOOGLE&Email=' + | 157 xhr.send('accountType=HOSTED_OR_GOOGLE&Email=' + |
144 encodeURIComponent(username) + '&Passwd=' + | 158 encodeURIComponent(username) + '&Passwd=' + |
145 encodeURIComponent(password) + '&service=' + | 159 encodeURIComponent(password) + '&service=' + |
146 encodeURIComponent(service) + '&source=chromoclient'); | 160 encodeURIComponent(service) + '&source=chromoclient'); |
147 } | 161 } |
148 | 162 |
| 163 function showLoginError() { |
| 164 var errorDiv = document.getElementById('errormsg_div'); |
| 165 clear(errorDiv); |
| 166 |
| 167 errorDiv.appendChild(document.createTextNode( |
| 168 "The username or password you entered is incorrect [")); |
| 169 |
| 170 var helpLink = document.createElement('a'); |
| 171 helpLink.setAttribute('href', |
| 172 'http://www.google.com/support/accounts/bin/answer.py?answer=27444'); |
| 173 helpLink.setAttribute('target', '_blank'); |
| 174 helpLink.appendChild(document.createTextNode('?')); |
| 175 errorDiv.appendChild(helpLink); |
| 176 |
| 177 errorDiv.appendChild(document.createTextNode("]")); |
| 178 } |
| 179 |
149 function extractAuthToken(message) { | 180 function extractAuthToken(message) { |
150 var lines = message.split('\n'); | 181 var lines = message.split('\n'); |
151 for (var i = 0; i < lines.length; i++) { | 182 for (var i = 0; i < lines.length; i++) { |
152 if (lines[i].match('^Auth=.*')) { | 183 if (lines[i].match('^Auth=.*')) { |
153 return lines[i].split('=')[1]; | 184 return lines[i].split('=')[1]; |
154 } | 185 } |
155 } | 186 } |
156 | 187 |
157 console.log('Could not parse auth token in : "' + message + '"'); | 188 console.log('Could not parse auth token in : "' + message + '"'); |
158 return BAD_AUTH_TOKEN; | 189 return BAD_AUTH_TOKEN; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 appendHostLinks(parsed_response.data.items); | 247 appendHostLinks(parsed_response.data.items); |
217 } else { | 248 } else { |
218 var errorResponse = JSON.parse(xhr.responseText); | 249 var errorResponse = JSON.parse(xhr.responseText); |
219 | 250 |
220 console.log('Error: Bad status on host list query: "' + | 251 console.log('Error: Bad status on host list query: "' + |
221 xhr.status + ' ' + xhr.statusText); | 252 xhr.status + ' ' + xhr.statusText); |
222 console.log('Error code ' + errorResponse.error.code); | 253 console.log('Error code ' + errorResponse.error.code); |
223 console.log('Error message ' + errorResponse.error.message); | 254 console.log('Error message ' + errorResponse.error.message); |
224 | 255 |
225 clear(hostlistDiv); | 256 clear(hostlistDiv); |
226 appendMessage(hostlistDiv, 'message', | 257 if (errorResponse.error.message == "Token expired") { |
227 'Unable to load host list for ' + username + '. ' + | 258 appendMessage(hostlistDiv, 'message', |
228 'Please try again later.'); | 259 'The authentication token for ' + username + |
229 appendMessage(hostlistDiv, 'message', | 260 ' has expired. Please sign in again.'); |
230 'Error code: ' + errorResponse.error.code); | 261 logout(); |
231 appendMessage(hostlistDiv, 'message', | 262 } else if (errorResponse.error.message == "Token invalid") { |
232 'Message: ' + errorResponse.error.message); | 263 appendMessage(hostlistDiv, 'message', |
| 264 'Invalid authentication token for ' + username + '. ' + |
| 265 'Please sign in again.'); |
| 266 logout(); |
| 267 } else { |
| 268 appendMessage(hostlistDiv, 'message', |
| 269 'Unable to load host list for ' + username + '. ' + |
| 270 'Please try again later.'); |
| 271 appendMessage(hostlistDiv, 'message', |
| 272 'Error code: ' + errorResponse.error.code); |
| 273 appendMessage(hostlistDiv, 'message', |
| 274 'Message: ' + errorResponse.error.message); |
| 275 } |
233 } | 276 } |
234 }; | 277 }; |
235 | 278 |
236 xhr.open('GET', 'https://www.googleapis.com/chromoting/v1/@me/hosts'); | 279 xhr.open('GET', 'https://www.googleapis.com/chromoting/v1/@me/hosts'); |
237 xhr.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); | 280 xhr.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8'); |
238 xhr.setRequestHeader('Authorization', 'GoogleLogin auth=' + token); | 281 xhr.setRequestHeader('Authorization', 'GoogleLogin auth=' + token); |
239 xhr.send(null); | 282 xhr.send(null); |
240 } | 283 } |
241 | 284 |
242 // Populate the 'hostlist_div' element with the list of hosts for this user. | 285 // Populate the 'hostlist_div' element with the list of hosts for this user. |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 hostStatus.appendChild(document.createTextNode(host.status)); | 359 hostStatus.appendChild(document.createTextNode(host.status)); |
317 hostEntry.appendChild(hostStatus); | 360 hostEntry.appendChild(hostStatus); |
318 | 361 |
319 var hostInfo = document.createElement('p'); | 362 var hostInfo = document.createElement('p'); |
320 hostInfo.setAttribute('class', 'hostindent hostinfo'); | 363 hostInfo.setAttribute('class', 'hostindent hostinfo'); |
321 hostInfo.appendChild(document.createTextNode(host.jabberId)); | 364 hostInfo.appendChild(document.createTextNode(host.jabberId)); |
322 hostEntry.appendChild(hostInfo); | 365 hostEntry.appendChild(hostInfo); |
323 | 366 |
324 return hostEntry; | 367 return hostEntry; |
325 } | 368 } |
OLD | NEW |