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.7KB

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