You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthHandler.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* global APP, config, JitsiMeetJS, Promise */
  2. import LoginDialog from './LoginDialog';
  3. import UIUtil from '../util/UIUtil';
  4. import {openConnection} from '../../../connection';
  5. const ConferenceEvents = JitsiMeetJS.events.conference;
  6. const ConnectionErrors = JitsiMeetJS.errors.connection;
  7. let externalAuthWindow;
  8. let authRequiredDialog;
  9. let isTokenAuthEnabled
  10. = typeof config.tokenAuthUrl === "string" && config.tokenAuthUrl.length;
  11. let getTokenAuthUrl
  12. = JitsiMeetJS.util.AuthUtil.getTokenAuthUrl.bind(null, config.tokenAuthUrl);
  13. /**
  14. * Authenticate using external service or just focus
  15. * external auth window if there is one already.
  16. *
  17. * @param {JitsiConference} room
  18. * @param {string} [lockPassword] password to use if the conference is locked
  19. */
  20. function doExternalAuth (room, lockPassword) {
  21. if (externalAuthWindow) {
  22. externalAuthWindow.focus();
  23. return;
  24. }
  25. if (room.isJoined()) {
  26. let getUrl;
  27. if (isTokenAuthEnabled) {
  28. getUrl = Promise.resolve(getTokenAuthUrl(room.getName(), true));
  29. initJWTTokenListener(room);
  30. } else {
  31. getUrl = room.getExternalAuthUrl(true);
  32. }
  33. getUrl.then(function (url) {
  34. externalAuthWindow = LoginDialog.showExternalAuthDialog(
  35. url,
  36. function () {
  37. externalAuthWindow = null;
  38. if (!isTokenAuthEnabled) {
  39. room.join(lockPassword);
  40. }
  41. }
  42. );
  43. });
  44. } else {
  45. // If conference has not been started yet
  46. // then redirect to login page
  47. if (isTokenAuthEnabled) {
  48. redirectToTokenAuthService(room.getName());
  49. } else {
  50. room.getExternalAuthUrl().then(UIUtil.redirect);
  51. }
  52. }
  53. }
  54. /**
  55. * Redirect the user to the token authentication service for the login to be
  56. * performed. Once complete it is expected that the service wil bring the user
  57. * back with "?jwt={the JWT token}" query parameter added.
  58. * @param {string} [roomName] the name of the conference room.
  59. */
  60. function redirectToTokenAuthService(roomName) {
  61. UIUtil.redirect(getTokenAuthUrl(roomName, false));
  62. }
  63. /**
  64. * Initializes 'message' listener that will wait for a JWT token to be received
  65. * from the token authentication service opened in a popup window.
  66. * @param room the name fo the conference room.
  67. */
  68. function initJWTTokenListener(room) {
  69. var self = this;
  70. var listener = function (event) {
  71. if (externalAuthWindow !== event.source) {
  72. console.warn("Ignored message not coming " +
  73. "from external authnetication window");
  74. return;
  75. }
  76. if (event.data && event.data.jwtToken) {
  77. config.token = event.data.jwtToken;
  78. console.info("Received JWT token:", config.token);
  79. var roomName = room.getName();
  80. openConnection({retry: false, roomName: roomName })
  81. .then(function (connection) {
  82. // Start new connection
  83. let newRoom = connection.initJitsiConference(
  84. roomName, APP.conference._getConferenceOptions());
  85. // Authenticate from the new connection to get
  86. // the session-ID from the focus, which wil then be used
  87. // to upgrade current connection's user role
  88. newRoom.room.moderator.authenticate().then(function () {
  89. connection.disconnect();
  90. // At this point we'll have session-ID stored in
  91. // the settings. It wil be used in the call below
  92. // to upgrade user's role
  93. room.room.moderator.authenticate()
  94. .then(function () {
  95. console.info("User role upgrade done !");
  96. unregister();
  97. }).catch(function (err, errCode) {
  98. console.error(
  99. "Authentication failed: ", err, errCode);
  100. unregister();
  101. }
  102. );
  103. }).catch(function (error, code) {
  104. unregister();
  105. connection.disconnect();
  106. console.error(
  107. 'Authentication failed on the new connection',
  108. error, code);
  109. });
  110. }, function (err) {
  111. unregister();
  112. console.error("Failed to open new connection", err);
  113. });
  114. }
  115. };
  116. var unregister = function () {
  117. window.removeEventListener("message", listener);
  118. };
  119. if (window.addEventListener) {
  120. window.addEventListener("message", listener, false);
  121. }
  122. }
  123. /**
  124. * Authenticate on the server.
  125. * @param {JitsiConference} room
  126. * @param {string} [lockPassword] password to use if the conference is locked
  127. */
  128. function doXmppAuth (room, lockPassword) {
  129. let loginDialog = LoginDialog.showAuthDialog(function (id, password) {
  130. // auth "on the fly":
  131. // 1. open new connection with proper id and password
  132. // 2. connect to the room
  133. // (this will store sessionId in the localStorage)
  134. // 3. close new connection
  135. // 4. reallocate focus in current room
  136. openConnection({id, password, roomName: room.getName()}).then(
  137. function (connection) {
  138. // open room
  139. let newRoom = connection.initJitsiConference(
  140. room.getName(), APP.conference._getConferenceOptions()
  141. );
  142. loginDialog.displayConnectionStatus(
  143. APP.translation.translateString('connection.FETCH_SESSION_ID')
  144. );
  145. newRoom.room.moderator.authenticate().then(function () {
  146. connection.disconnect();
  147. loginDialog.displayConnectionStatus(
  148. APP.translation.translateString('connection.GOT_SESSION_ID')
  149. );
  150. // authenticate conference on the fly
  151. room.join(lockPassword);
  152. loginDialog.close();
  153. }).catch(function (error, code) {
  154. connection.disconnect();
  155. console.error('Auth on the fly failed', error);
  156. let errorMsg = APP.translation.translateString(
  157. 'connection.GET_SESSION_ID_ERROR'
  158. );
  159. loginDialog.displayError(errorMsg + code);
  160. });
  161. }, function (err) {
  162. loginDialog.displayError(err);
  163. });
  164. }, function () { // user canceled
  165. loginDialog.close();
  166. });
  167. }
  168. /**
  169. * Authenticate for the conference.
  170. * Uses external service for auth if conference supports that.
  171. * @param {JitsiConference} room
  172. * @param {string} [lockPassword] password to use if the conference is locked
  173. */
  174. function authenticate (room, lockPassword) {
  175. if (isTokenAuthEnabled || room.isExternalAuthEnabled()) {
  176. doExternalAuth(room, lockPassword);
  177. } else {
  178. doXmppAuth(room, lockPassword);
  179. }
  180. }
  181. /**
  182. * De-authenticate local user.
  183. *
  184. * @param {JitsiConference} room
  185. * @param {string} [lockPassword] password to use if the conference is locked
  186. * @returns {Promise}
  187. */
  188. function logout (room) {
  189. return new Promise(function (resolve) {
  190. room.room.moderator.logout(resolve);
  191. }).then(function (url) {
  192. // de-authenticate conference on the fly
  193. if (room.isJoined()) {
  194. room.join();
  195. }
  196. return url;
  197. });
  198. }
  199. /**
  200. * Notify user that authentication is required to create the conference.
  201. * @param {JitsiConference} room
  202. * @param {string} [lockPassword] password to use if the conference is locked
  203. */
  204. function requireAuth(room, lockPassword) {
  205. if (authRequiredDialog) {
  206. return;
  207. }
  208. authRequiredDialog = LoginDialog.showAuthRequiredDialog(
  209. room.getName(), authenticate.bind(null, room, lockPassword)
  210. );
  211. }
  212. /**
  213. * Close auth-related dialogs if there are any.
  214. */
  215. function closeAuth() {
  216. if (externalAuthWindow) {
  217. externalAuthWindow.close();
  218. externalAuthWindow = null;
  219. }
  220. if (authRequiredDialog) {
  221. authRequiredDialog.close();
  222. authRequiredDialog = null;
  223. }
  224. }
  225. function showXmppPasswordPrompt(roomName, connect) {
  226. return new Promise(function (resolve, reject) {
  227. let authDialog = LoginDialog.showAuthDialog(
  228. function (id, password) {
  229. connect(id, password, roomName).then(function (connection) {
  230. authDialog.close();
  231. resolve(connection);
  232. }, function (err) {
  233. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  234. authDialog.displayError(err);
  235. } else {
  236. authDialog.close();
  237. reject(err);
  238. }
  239. });
  240. }
  241. );
  242. });
  243. }
  244. /**
  245. * Show Authentication Dialog and try to connect with new credentials.
  246. * If failed to connect because of PASSWORD_REQUIRED error
  247. * then ask for password again.
  248. * @param {string} [roomName] name of the conference room
  249. * @param {function(id, password, roomName)} [connect] function that returns
  250. * a Promise which resolves with JitsiConnection or fails with one of
  251. * ConnectionErrors.
  252. * @returns {Promise<JitsiConnection>}
  253. */
  254. function requestAuth(roomName, connect) {
  255. if (isTokenAuthEnabled) {
  256. // This Promise never resolves as user gets redirected to another URL
  257. return new Promse(function (resolve, reject) {
  258. redirectToTokenAuthService(roomName);
  259. });
  260. } else {
  261. return showXmppPasswordPrompt(roomName, connect);
  262. }
  263. }
  264. export default {
  265. authenticate,
  266. requireAuth,
  267. requestAuth,
  268. closeAuth,
  269. logout
  270. };