refactor authentication module

This commit is contained in:
isymchych 2015-12-15 14:09:44 +02:00
parent fbe5ef7ee6
commit ec2be349df
5 changed files with 33 additions and 300 deletions

5
app.js
View File

@ -460,6 +460,10 @@ function initConference(localTracks, connection) {
// APP.xmpp.eject(self.id); // APP.xmpp.eject(self.id);
}); });
APP.UI.addListener(UIEvents.AUTH_CLICKED, function () {
// FIXME handle
});
APP.UI.addListener(UIEvents.SELECTED_ENDPOINT, function (id) { APP.UI.addListener(UIEvents.SELECTED_ENDPOINT, function (id) {
room.selectParticipant(id); room.selectParticipant(id);
}); });
@ -484,7 +488,6 @@ function initConference(localTracks, connection) {
// FIXME handle errors here // FIXME handle errors here
APP.UI.closeAuthenticationDialog();
room.join(); room.join();
}).catch(function (err) { }).catch(function (err) {
// FIXME notify that we cannot conenct to the room // FIXME notify that we cannot conenct to the room

View File

@ -22,7 +22,6 @@ var EventEmitter = require("events");
var Settings = require("./../settings/Settings"); var Settings = require("./../settings/Settings");
UI.messageHandler = require("./util/MessageHandler"); UI.messageHandler = require("./util/MessageHandler");
var messageHandler = UI.messageHandler; var messageHandler = UI.messageHandler;
var Authentication = require("./authentication/Authentication");
var JitsiPopover = require("./util/JitsiPopover"); var JitsiPopover = require("./util/JitsiPopover");
var CQEvents = require("../../service/connectionquality/CQEvents"); var CQEvents = require("../../service/connectionquality/CQEvents");
var DesktopSharingEventTypes var DesktopSharingEventTypes
@ -208,10 +207,6 @@ function registerListeners() {
UI.addListener(UIEvents.FULLSCREEN_TOGGLE, toggleFullScreen); UI.addListener(UIEvents.FULLSCREEN_TOGGLE, toggleFullScreen);
UI.addListener(UIEvents.AUTH_CLICKED, function () {
Authentication.authenticate();
});
UI.addListener(UIEvents.TOGGLE_CHAT, UI.toggleChat); UI.addListener(UIEvents.TOGGLE_CHAT, UI.toggleChat);
UI.addListener(UIEvents.TOGGLE_SETTINGS, function () { UI.addListener(UIEvents.TOGGLE_SETTINGS, function () {
@ -407,7 +402,6 @@ UI.updateLocalRole = function (isModerator) {
SettingsMenu.onRoleChanged(); SettingsMenu.onRoleChanged();
if (isModerator) { if (isModerator) {
Authentication.closeAuthenticationWindow();
messageHandler.notify(null, "notify.me", 'connected', "notify.moderator"); messageHandler.notify(null, "notify.me", 'connected', "notify.moderator");
Toolbar.checkAutoRecord(); Toolbar.checkAutoRecord();
@ -437,10 +431,6 @@ UI.updateUserRole = function (user) {
} }
}; };
UI.notifyAuthRequired = function (intervalCallback) {
Authentication.openAuthenticationDialog(APP.conference.roomName, intervalCallback);
};
UI.toggleSmileys = function () { UI.toggleSmileys = function () {
Chat.toggleSmileys(); Chat.toggleSmileys();
@ -506,11 +496,6 @@ UI.showLoginPopup = function(callback) {
); );
}; };
UI.closeAuthenticationDialog = function () {
Authentication.closeAuthenticationDialog();
Authentication.stopInterval();
};
UI.askForNickname = function () { UI.askForNickname = function () {
return window.prompt('Your nickname (optional)'); return window.prompt('Your nickname (optional)');
}; };

View File

@ -1,156 +0,0 @@
/* global $, APP*/
/* jshint -W101 */
import messageHandler from '../util/MessageHandler';
var LoginDialog = require('./LoginDialog');
var Moderator = require('../../xmpp/moderator');
/* Initial "authentication required" dialog */
var authDialog = null;
/* Loop retry ID that wits for other user to create the room */
var authRetryId = null;
var authenticationWindow = null;
var Authentication = {
authenticate () {
Authentication.focusAuthenticationWindow();
if (!APP.xmpp.isExternalAuthEnabled()) {
Authentication.xmppAuthenticate();
return;
}
// Get authentication URL
if (!APP.xmpp.isMUCJoined()) {
APP.xmpp.getLoginUrl(APP.conference.roomName, function (url) {
// If conference has not been started yet - redirect to login page
window.location.href = url;
});
} else {
APP.xmpp.getPopupLoginUrl(APP.conference.roomName, function (url) {
// Otherwise - open popup with authentication URL
var authenticationWindow = Authentication.createAuthenticationWindow(
function () {
// On popup closed - retry room allocation
APP.xmpp.allocateConferenceFocus(
APP.conference.roomName,
function () { console.info("AUTH DONE"); }
);
}, url);
if (!authenticationWindow) {
messageHandler.openMessageDialog(null, "dialog.popupError");
}
});
}
},
openAuthenticationDialog (roomName, intervalCallback) {
// This is the loop that will wait for the room to be created by
// someone else. 'auth_required.moderator' will bring us back here.
authRetryId = window.setTimeout(intervalCallback, 5000);
// Show prompt only if it's not open
if (authDialog !== null) {
return;
}
// extract room name from 'room@muc.server.net'
var room = roomName.substr(0, roomName.indexOf('@'));
var title
= APP.translation.generateTranslationHTML("dialog.WaitingForHost");
var msg
= APP.translation.generateTranslationHTML(
"dialog.WaitForHostMsg", {room: room});
var buttonTxt
= APP.translation.generateTranslationHTML("dialog.IamHost");
var buttons = [];
buttons.push({title: buttonTxt, value: "authNow"});
authDialog = messageHandler.openDialog(
title,
msg,
true,
buttons,
function (onSubmitEvent, submitValue) {
// Do not close the dialog yet
onSubmitEvent.preventDefault();
// Open login popup
if (submitValue === 'authNow') {
Authentication.authenticate();
}
}
);
},
closeAuthenticationWindow () {
if (authenticationWindow) {
authenticationWindow.close();
authenticationWindow = null;
}
},
xmppAuthenticate () {
var loginDialog = LoginDialog.show(
function (connection, state) {
if (!state) {
// User cancelled
loginDialog.close();
return;
} else if (state == APP.xmpp.Status.CONNECTED) {
loginDialog.close();
Authentication.stopInterval();
Authentication.closeAuthenticationDialog();
// Close the connection as anonymous one will be used
// to create the conference. Session-id will authorize
// the request.
connection.disconnect();
var roomName = APP.conference.roomName;
Moderator.allocateConferenceFocus(roomName, function () {
// If it's not "on the fly" authentication now join
// the conference room
if (!APP.xmpp.isMUCJoined()) {
APP.UI.checkForNicknameAndJoin();
}
});
}
}, true);
},
focusAuthenticationWindow () {
// If auth window exists just bring it to the front
if (authenticationWindow) {
authenticationWindow.focus();
return;
}
},
closeAuthenticationDialog () {
// Close authentication dialog if opened
if (authDialog) {
authDialog.close();
authDialog = null;
}
},
createAuthenticationWindow (callback, url) {
authenticationWindow = messageHandler.openCenteredPopup(
url, 910, 660,
// On closed
function () {
// Close authentication dialog if opened
Authentication.closeAuthenticationDialog();
callback();
authenticationWindow = null;
});
return authenticationWindow;
},
stopInterval () {
// Clear retry interval, so that we don't call 'doJoinAfterFocus' twice
if (authRetryId) {
window.clearTimeout(authRetryId);
authRetryId = null;
}
}
};
module.exports = Authentication;

View File

@ -1,35 +1,19 @@
/* global $, APP, config*/ /* global $, APP, config*/
var XMPP = require('../../xmpp/xmpp'); var messageHandler = require('../util/MessageHandler');
var Moderator = require('../../xmpp/moderator');
//FIXME: use LoginDialog to add retries to XMPP.connect method used when //FIXME: use LoginDialog to add retries to XMPP.connect method used when
// anonymous domain is not enabled // anonymous domain is not enabled
/** function Dialog(successCallback, cancelCallback) {
* Creates new <tt>Dialog</tt> instance.
* @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt> called
* when we either fail to connect or succeed(check Strophe.Status).
* @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to Jicofo
* in order to create session-id after the connection is established.
* @constructor
*/
function Dialog(callback, obtainSession) {
var self = this;
var stop = false;
var connection = APP.xmpp.createConnection();
var message = '<h2 data-i18n="dialog.passwordRequired">'; var message = '<h2 data-i18n="dialog.passwordRequired">';
message += APP.translation.translateString("dialog.passwordRequired"); message += APP.translation.translateString("dialog.passwordRequired");
message += '</h2>' + message += '</h2>' +
'<input name="username" type="text" '; '<input name="username" type="text" ';
if (config.hosts.authdomain) { if (config.hosts.authdomain) {
message += 'placeholder="user identity" autofocus>'; message += 'placeholder="user identity" autofocus>';
} else { } else {
message += 'placeholder="user@domain.net" autofocus>'; message += 'placeholder="user@domain.net" autofocus>';
} }
message += '<input name="password" ' + message += '<input name="password" ' +
'type="password" data-i18n="[placeholder]dialog.userPassword"' + 'type="password" data-i18n="[placeholder]dialog.userPassword"' +
@ -53,23 +37,12 @@ function Dialog(callback, obtainSession) {
var jid = f.username; var jid = f.username;
var password = f.password; var password = f.password;
if (jid && password) { if (jid && password) {
stop = false;
if (jid.indexOf("@") < 0) {
jid = jid.concat('@');
if (config.hosts.authdomain) {
jid += config.hosts.authdomain;
} else {
jid += config.hosts.domain;
}
}
connection.reset();
connDialog.goToState('connecting'); connDialog.goToState('connecting');
connection.connect(jid, password, stateHandler); successCallback(jid, password);
} }
} else { } else {
// User cancelled // User cancelled
stop = true; cancelCallback();
callback();
} }
} }
}, },
@ -95,102 +68,18 @@ function Dialog(callback, obtainSession) {
defaultButton: 0, defaultButton: 0,
submit: function (e, v, m, f) { submit: function (e, v, m, f) {
e.preventDefault(); e.preventDefault();
if (v === 'retry') if (v === 'retry') {
connDialog.goToState('login'); connDialog.goToState('login');
else } else {
callback(); cancelCallback();
}
} }
} }
}; };
var connDialog var connDialog = messageHandler.openDialogWithStates(
= APP.UI.messageHandler.openDialogWithStates(states, states, { persistent: true, closeText: '' }, null
{ persistent: true, closeText: '' }, null); );
var stateHandler = function (status, message) {
if (stop) {
return;
}
var translateKey = "connection." + XMPP.getStatusString(status);
var statusStr = APP.translation.translateString(translateKey);
// Display current state
var connectionStatus =
connDialog.getState('connecting').find('#connectionStatus');
connectionStatus.text(statusStr);
switch (status) {
case XMPP.Status.CONNECTED:
stop = true;
if (!obtainSession) {
callback(connection, status);
return;
}
// Obtaining session-id status
connectionStatus.text(
APP.translation.translateString(
'connection.FETCH_SESSION_ID'));
// Authenticate with Jicofo and obtain session-id
var roomName = APP.conference.roomName;
// Jicofo will return new session-id when connected
// from authenticated domain
connection.sendIQ(
Moderator.createConferenceIq(roomName),
function (result) {
connectionStatus.text(
APP.translation.translateString(
'connection.GOT_SESSION_ID'));
stop = true;
// Parse session-id
Moderator.parseSessionId(result);
callback(connection, status);
},
function (error) {
console.error("Auth on the fly failed", error);
stop = true;
var errorMsg =
APP.translation.translateString(
'connection.GET_SESSION_ID_ERROR') +
$(error).find('>error').attr('code');
self.displayError(errorMsg);
connection.disconnect();
});
break;
case XMPP.Status.AUTHFAIL:
case XMPP.Status.CONNFAIL:
case XMPP.Status.DISCONNECTED:
stop = true;
callback(connection, status);
var errorMessage = statusStr;
if (message)
{
errorMessage += ': ' + message;
}
self.displayError(errorMessage);
break;
default:
break;
}
};
/** /**
* Displays error message in 'finished' state which allows either to cancel * Displays error message in 'finished' state which allows either to cancel
@ -211,7 +100,6 @@ function Dialog(callback, obtainSession) {
* Closes LoginDialog. * Closes LoginDialog.
*/ */
this.close = function () { this.close = function () {
stop = true;
connDialog.close(); connDialog.close();
}; };
} }
@ -232,8 +120,22 @@ var LoginDialog = {
* established. * established.
* @returns {Dialog} * @returns {Dialog}
*/ */
show: function (callback, obtainSession) { show: function (successCallback, cancelCallback) {
return new Dialog(callback, obtainSession); return new Dialog(successCallback, cancelCallback);
},
showExternalAuthDialog: function (url, callback) {
var dialog = messageHandler.openCenteredPopup(
url, 910, 660,
// On closed
callback
);
if (!dialog) {
messageHandler.openMessageDialog(null, "dialog.popupError");
}
return dialog;
} }
}; };

View File

@ -5,7 +5,6 @@ import UIUtil from "../util/UIUtil";
import LargeVideo from "./LargeVideo"; import LargeVideo from "./LargeVideo";
var RTCBrowserType = require("../../RTC/RTCBrowserType"); var RTCBrowserType = require("../../RTC/RTCBrowserType");
var MediaStreamType = require("../../../service/RTC/MediaStreamTypes");
function SmallVideo() { function SmallVideo() {
this.isMuted = false; this.isMuted = false;