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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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('connection.FETCH_SESSION_ID');
  141. newRoom.room.moderator.authenticate().then(function () {
  142. connection.disconnect();
  143. loginDialog.displayConnectionStatus(
  144. 'connection.GOT_SESSION_ID');
  145. // authenticate conference on the fly
  146. room.join(lockPassword);
  147. loginDialog.close();
  148. }).catch(function (error, code) {
  149. connection.disconnect();
  150. console.error('Auth on the fly failed', error);
  151. loginDialog.displayError(
  152. 'connection.GET_SESSION_ID_ERROR', {code: code});
  153. });
  154. }, function (err) {
  155. loginDialog.displayError(err);
  156. });
  157. }, function () { // user canceled
  158. loginDialog.close();
  159. });
  160. }
  161. /**
  162. * Authenticate for the conference.
  163. * Uses external service for auth if conference supports that.
  164. * @param {JitsiConference} room
  165. * @param {string} [lockPassword] password to use if the conference is locked
  166. */
  167. function authenticate (room, lockPassword) {
  168. if (isTokenAuthEnabled || room.isExternalAuthEnabled()) {
  169. doExternalAuth(room, lockPassword);
  170. } else {
  171. doXmppAuth(room, lockPassword);
  172. }
  173. }
  174. /**
  175. * De-authenticate local user.
  176. *
  177. * @param {JitsiConference} room
  178. * @param {string} [lockPassword] password to use if the conference is locked
  179. * @returns {Promise}
  180. */
  181. function logout (room) {
  182. return new Promise(function (resolve) {
  183. room.room.moderator.logout(resolve);
  184. }).then(function (url) {
  185. // de-authenticate conference on the fly
  186. if (room.isJoined()) {
  187. room.join();
  188. }
  189. return url;
  190. });
  191. }
  192. /**
  193. * Notify user that authentication is required to create the conference.
  194. * @param {JitsiConference} room
  195. * @param {string} [lockPassword] password to use if the conference is locked
  196. */
  197. function requireAuth(room, lockPassword) {
  198. if (authRequiredDialog) {
  199. return;
  200. }
  201. authRequiredDialog = LoginDialog.showAuthRequiredDialog(
  202. room.getName(), authenticate.bind(null, room, lockPassword)
  203. );
  204. }
  205. /**
  206. * Close auth-related dialogs if there are any.
  207. */
  208. function closeAuth() {
  209. if (externalAuthWindow) {
  210. externalAuthWindow.close();
  211. externalAuthWindow = null;
  212. }
  213. if (authRequiredDialog) {
  214. authRequiredDialog.close();
  215. authRequiredDialog = null;
  216. }
  217. }
  218. function showXmppPasswordPrompt(roomName, connect) {
  219. return new Promise(function (resolve, reject) {
  220. let authDialog = LoginDialog.showAuthDialog(
  221. function (id, password) {
  222. connect(id, password, roomName).then(function (connection) {
  223. authDialog.close();
  224. resolve(connection);
  225. }, function (err) {
  226. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  227. authDialog.displayError(err);
  228. } else {
  229. authDialog.close();
  230. reject(err);
  231. }
  232. });
  233. }
  234. );
  235. });
  236. }
  237. /**
  238. * Show Authentication Dialog and try to connect with new credentials.
  239. * If failed to connect because of PASSWORD_REQUIRED error
  240. * then ask for password again.
  241. * @param {string} [roomName] name of the conference room
  242. * @param {function(id, password, roomName)} [connect] function that returns
  243. * a Promise which resolves with JitsiConnection or fails with one of
  244. * ConnectionErrors.
  245. * @returns {Promise<JitsiConnection>}
  246. */
  247. function requestAuth(roomName, connect) {
  248. if (isTokenAuthEnabled) {
  249. // This Promise never resolves as user gets redirected to another URL
  250. return new Promise(() => redirectToTokenAuthService(roomName));
  251. } else {
  252. return showXmppPasswordPrompt(roomName, connect);
  253. }
  254. }
  255. export default {
  256. authenticate,
  257. requireAuth,
  258. requestAuth,
  259. closeAuth,
  260. logout
  261. };