1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * Connect set-up state machine for Me2Me and IT2Me
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
16 * @param {Element} pluginParent The node under which to add the client plugin.
17 * @param {function(remoting.ClientSession):void} onOk Callback on success.
18 * @param {function(remoting.Error):void} onError Callback on error.
21 remoting.SessionConnector = function(pluginParent, onOk, onError) {
26 this.pluginParent_ = pluginParent;
29 * @type {function(remoting.ClientSession):void}
35 * @type {function(remoting.Error):void}
38 this.onError_ = onError;
47 * @type {remoting.ClientSession.Mode}
50 this.connectionMode_ = remoting.ClientSession.Mode.ME2ME;
52 // Initialize/declare per-connection state.
57 * Reset the per-connection state so that the object can be re-used for a
58 * second connection. Note the none of the shared WCS state is reset.
60 remoting.SessionConnector.prototype.reset = function() {
62 * Set to true to indicate that the user requested pairing when entering
63 * their PIN for a Me2Me connection.
67 this.pairingRequested = false;
70 * String used to identify the host to which to connect. For IT2Me, this is
71 * the first 7 digits of the access code; for Me2Me it is the host identifier.
79 * For paired connections, the client id of this device, issued by the host.
84 this.clientPairingId_ = '';
87 * For paired connections, the paired secret for this device, issued by the
93 this.clientPairedSecret_ = '';
96 * String used to authenticate to the host on connection. For IT2Me, this is
97 * the access code; for Me2Me it is the PIN.
102 this.passPhrase_ = '';
114 this.hostPublicKey_ = '';
120 this.refreshHostJidIfOffline_ = false;
123 * @type {remoting.ClientSession}
126 this.clientSession_ = null;
129 * @type {XMLHttpRequest}
132 this.pendingXhr_ = null;
135 * Function to interactively obtain the PIN from the user.
136 * @type {function(boolean, function(string):void):void}
139 this.fetchPin_ = function(onPinFetched) {};
142 * @type {function(string, string, string,
143 * function(string, string):void): void}
146 this.fetchThirdPartyToken_ = function(
147 tokenUrl, scope, onThirdPartyTokenFetched) {};
150 * Host 'name', as displayed in the client tool-bar. For a Me2Me connection,
151 * this is the name of the host; for an IT2Me connection, it is the email
152 * address of the person sharing their computer.
157 this.hostDisplayName_ = '';
161 * Initiate a Me2Me connection.
163 * @param {remoting.Host} host The Me2Me host to which to connect.
164 * @param {function(boolean, function(string):void):void} fetchPin Function to
165 * interactively obtain the PIN from the user.
166 * @param {function(string, string, string,
167 * function(string, string): void): void}
168 * fetchThirdPartyToken Function to obtain a token from a third party
169 * authenticaiton server.
170 * @param {string} clientPairingId The client id issued by the host when
171 * this device was paired, if it is already paired.
172 * @param {string} clientPairedSecret The shared secret issued by the host when
173 * this device was paired, if it is already paired.
174 * @return {void} Nothing.
176 remoting.SessionConnector.prototype.connectMe2Me =
177 function(host, fetchPin, fetchThirdPartyToken,
178 clientPairingId, clientPairedSecret) {
179 this.connectMe2MeInternal_(
180 host.hostId, host.jabberId, host.publicKey, host.hostName,
181 fetchPin, fetchThirdPartyToken,
182 clientPairingId, clientPairedSecret, true);
186 * Update the pairing info so that the reconnect function will work correctly.
188 * @param {string} clientId The paired client id.
189 * @param {string} sharedSecret The shared secret.
191 remoting.SessionConnector.prototype.updatePairingInfo =
192 function(clientId, sharedSecret) {
193 this.clientPairingId_ = clientId;
194 this.clientPairedSecret_ = sharedSecret;
198 * Initiate a Me2Me connection.
200 * @param {string} hostId ID of the Me2Me host.
201 * @param {string} hostJid XMPP JID of the host.
202 * @param {string} hostPublicKey Public Key of the host.
203 * @param {string} hostDisplayName Display name (friendly name) of the host.
204 * @param {function(boolean, function(string):void):void} fetchPin Function to
205 * interactively obtain the PIN from the user.
206 * @param {function(string, string, string,
207 * function(string, string): void): void}
208 * fetchThirdPartyToken Function to obtain a token from a third party
209 * authenticaiton server.
210 * @param {string} clientPairingId The client id issued by the host when
211 * this device was paired, if it is already paired.
212 * @param {string} clientPairedSecret The shared secret issued by the host when
213 * this device was paired, if it is already paired.
214 * @param {boolean} refreshHostJidIfOffline Whether to refresh the JID and retry
215 * the connection if the current JID is offline.
216 * @return {void} Nothing.
219 remoting.SessionConnector.prototype.connectMe2MeInternal_ =
220 function(hostId, hostJid, hostPublicKey, hostDisplayName,
221 fetchPin, fetchThirdPartyToken,
222 clientPairingId, clientPairedSecret,
223 refreshHostJidIfOffline) {
224 // Cancel any existing connect operation.
227 this.hostId_ = hostId;
228 this.hostJid_ = hostJid;
229 this.hostPublicKey_ = hostPublicKey;
230 this.fetchPin_ = fetchPin;
231 this.fetchThirdPartyToken_ = fetchThirdPartyToken;
232 this.hostDisplayName_ = hostDisplayName;
233 this.connectionMode_ = remoting.ClientSession.Mode.ME2ME;
234 this.refreshHostJidIfOffline_ = refreshHostJidIfOffline;
235 this.updatePairingInfo(clientPairingId, clientPairedSecret);
236 this.createSession_();
240 * Initiate an IT2Me connection.
242 * @param {string} accessCode The access code as entered by the user.
243 * @return {void} Nothing.
245 remoting.SessionConnector.prototype.connectIT2Me = function(accessCode) {
246 var kSupportIdLen = 7;
247 var kHostSecretLen = 5;
248 var kAccessCodeLen = kSupportIdLen + kHostSecretLen;
250 // Cancel any existing connect operation.
253 var normalizedAccessCode = this.normalizeAccessCode_(accessCode);
254 if (normalizedAccessCode.length != kAccessCodeLen) {
255 this.onError_(remoting.Error.INVALID_ACCESS_CODE);
259 this.hostId_ = normalizedAccessCode.substring(0, kSupportIdLen);
260 this.passPhrase_ = normalizedAccessCode;
261 this.connectionMode_ = remoting.ClientSession.Mode.IT2ME;
262 remoting.identity.callWithToken(this.connectIT2MeWithToken_.bind(this),
267 * Reconnect a closed connection.
269 * @return {void} Nothing.
271 remoting.SessionConnector.prototype.reconnect = function() {
272 if (this.connectionMode_ == remoting.ClientSession.Mode.IT2ME) {
273 console.error('reconnect not supported for IT2Me.');
276 this.connectMe2MeInternal_(
277 this.hostId_, this.hostJid_, this.hostPublicKey_, this.hostDisplayName_,
278 this.fetchPin_, this.fetchThirdPartyToken_,
279 this.clientPairingId_, this.clientPairedSecret_, true);
283 * Cancel a connection-in-progress.
285 remoting.SessionConnector.prototype.cancel = function() {
286 if (this.clientSession_) {
287 this.clientSession_.removePlugin();
288 this.clientSession_ = null;
290 if (this.pendingXhr_) {
291 this.pendingXhr_.abort();
292 this.pendingXhr_ = null;
298 * Get the connection mode (Me2Me or IT2Me)
300 * @return {remoting.ClientSession.Mode}
302 remoting.SessionConnector.prototype.getConnectionMode = function() {
303 return this.connectionMode_;
311 remoting.SessionConnector.prototype.getHostId = function() {
316 * Get host display name.
320 remoting.SessionConnector.prototype.getHostDisplayName = function() {
321 return this.hostDisplayName_;
325 * Continue an IT2Me connection once an access token has been obtained.
327 * @param {string} token An OAuth2 access token.
328 * @return {void} Nothing.
331 remoting.SessionConnector.prototype.connectIT2MeWithToken_ = function(token) {
332 // Resolve the host id to get the host JID.
333 this.pendingXhr_ = remoting.xhr.get(
334 remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' +
335 encodeURIComponent(this.hostId_),
336 this.onIT2MeHostInfo_.bind(this),
338 { 'Authorization': 'OAuth ' + token });
342 * Continue an IT2Me connection once the host JID has been looked up.
344 * @param {XMLHttpRequest} xhr The server response to the support-hosts query.
345 * @return {void} Nothing.
348 remoting.SessionConnector.prototype.onIT2MeHostInfo_ = function(xhr) {
349 this.pendingXhr_ = null;
350 if (xhr.status == 200) {
351 var host = /** @type {{data: {jabberId: string, publicKey: string}}} */
352 jsonParseSafe(xhr.responseText);
353 if (host && host.data && host.data.jabberId && host.data.publicKey) {
354 this.hostJid_ = host.data.jabberId;
355 this.hostPublicKey_ = host.data.publicKey;
356 this.hostDisplayName_ = this.hostJid_.split('/')[0];
357 this.createSession_();
360 console.error('Invalid "support-hosts" response from server.');
363 this.onError_(this.translateSupportHostsError(xhr.status));
368 * Creates ClientSession object.
370 remoting.SessionConnector.prototype.createSession_ = function() {
371 // In some circumstances, the WCS <iframe> can get reloaded, which results
372 // in a new clientJid and a new callback. In this case, remove the old
373 // client plugin before instantiating a new one.
374 if (this.clientSession_) {
375 this.clientSession_.removePlugin();
376 this.clientSession_ = null;
379 var authenticationMethods =
380 'third_party,spake2_pair,spake2_hmac,spake2_plain';
381 this.clientSession_ = new remoting.ClientSession(
382 this.passPhrase_, this.fetchPin_, this.fetchThirdPartyToken_,
383 authenticationMethods, this.hostId_, this.hostJid_, this.hostPublicKey_,
384 this.connectionMode_, this.clientPairingId_, this.clientPairedSecret_);
385 this.clientSession_.logHostOfflineErrors(!this.refreshHostJidIfOffline_);
386 this.clientSession_.setOnStateChange(this.onStateChange_.bind(this));
387 this.clientSession_.createPluginAndConnect(this.pluginParent_);
391 * Handle a change in the state of the client session prior to successful
392 * connection (after connection, this class no longer handles state change
393 * events). Errors that occur while connecting either trigger a reconnect
394 * or notify the onError handler.
396 * @param {number} oldState The previous state of the plugin.
397 * @param {number} newState The current state of the plugin.
398 * @return {void} Nothing.
401 remoting.SessionConnector.prototype.onStateChange_ =
402 function(oldState, newState) {
404 case remoting.ClientSession.State.CONNECTED:
405 // When the connection succeeds, deregister for state-change callbacks
406 // and pass the session to the onOk callback. It is expected that it
407 // will register a new state-change callback to handle disconnect
408 // or error conditions.
409 this.clientSession_.setOnStateChange(null);
410 this.onOk_(this.clientSession_);
413 case remoting.ClientSession.State.CREATED:
414 console.log('Created plugin');
417 case remoting.ClientSession.State.CONNECTING:
418 console.log('Connecting as ' + remoting.identity.getCachedEmail());
421 case remoting.ClientSession.State.INITIALIZING:
422 console.log('Initializing connection');
425 case remoting.ClientSession.State.CLOSED:
426 // This class deregisters for state-change callbacks when the CONNECTED
427 // state is reached, so it only sees the CLOSED state in exceptional
428 // circumstances. For example, a CONNECTING -> CLOSED transition happens
429 // if the host closes the connection without an error message instead of
430 // accepting it. Since there's no way of knowing exactly what went wrong,
431 // we rely on server-side logs in this case and report a generic error
433 this.onError_(remoting.Error.UNEXPECTED);
436 case remoting.ClientSession.State.FAILED:
437 var error = this.clientSession_.getError();
438 console.error('Client plugin reported connection failed: ' + error);
440 error = remoting.Error.UNEXPECTED;
442 if (error == remoting.Error.HOST_IS_OFFLINE &&
443 this.refreshHostJidIfOffline_) {
444 remoting.hostList.refresh(this.onHostListRefresh_.bind(this));
446 this.onError_(error);
451 console.error('Unexpected client plugin state: ' + newState);
452 // This should only happen if the web-app and client plugin get out of
453 // sync, and even then the version check should ensure compatibility.
454 this.onError_(remoting.Error.MISSING_PLUGIN);
459 * @param {boolean} success True if the host list was successfully refreshed;
460 * false if an error occurred.
463 remoting.SessionConnector.prototype.onHostListRefresh_ = function(success) {
465 var host = remoting.hostList.getHostForId(this.hostId_);
467 this.connectMe2MeInternal_(
468 host.hostId, host.jabberId, host.publicKey, host.hostName,
469 this.fetchPin_, this.fetchThirdPartyToken_,
470 this.clientPairingId_, this.clientPairedSecret_, false);
474 this.onError_(remoting.Error.HOST_IS_OFFLINE);
478 * @param {number} error An HTTP error code returned by the support-hosts
480 * @return {remoting.Error} The equivalent remoting.Error code.
483 remoting.SessionConnector.prototype.translateSupportHostsError =
486 case 0: return remoting.Error.NETWORK_FAILURE;
487 case 404: return remoting.Error.INVALID_ACCESS_CODE;
488 case 502: // No break
489 case 503: return remoting.Error.SERVICE_UNAVAILABLE;
490 default: return remoting.Error.UNEXPECTED;
495 * Normalize the access code entered by the user.
497 * @param {string} accessCode The access code, as entered by the user.
498 * @return {string} The normalized form of the code (whitespace removed).
500 remoting.SessionConnector.prototype.normalizeAccessCode_ =
501 function(accessCode) {
503 return accessCode.replace(/\s/g, '');