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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* global APP, config, JitsiMeetJS, Promise */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import LoginDialog from './LoginDialog';
  4. import UIUtil from '../util/UIUtil';
  5. import {openConnection} from '../../../connection';
  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 listener = function (event) {
  70. if (externalAuthWindow !== event.source) {
  71. logger.warn("Ignored message not coming " +
  72. "from external authnetication window");
  73. return;
  74. }
  75. if (event.data && event.data.jwtToken) {
  76. config.token = event.data.jwtToken;
  77. logger.info("Received JWT token:", config.token);
  78. var roomName = room.getName();
  79. openConnection({retry: false, roomName: roomName })
  80. .then(function (connection) {
  81. // Start new connection
  82. let newRoom = connection.initJitsiConference(
  83. roomName, APP.conference._getConferenceOptions());
  84. // Authenticate from the new connection to get
  85. // the session-ID from the focus, which wil then be used
  86. // to upgrade current connection's user role
  87. newRoom.room.moderator.authenticate().then(function () {
  88. connection.disconnect();
  89. // At this point we'll have session-ID stored in
  90. // the settings. It wil be used in the call below
  91. // to upgrade user's role
  92. room.room.moderator.authenticate()
  93. .then(function () {
  94. logger.info("User role upgrade done !");
  95. unregister();
  96. }).catch(function (err, errCode) {
  97. logger.error(
  98. "Authentication failed: ", err, errCode);
  99. unregister();
  100. }
  101. );
  102. }).catch(function (error, code) {
  103. unregister();
  104. connection.disconnect();
  105. logger.error(
  106. 'Authentication failed on the new connection',
  107. error, code);
  108. });
  109. }, function (err) {
  110. unregister();
  111. logger.error("Failed to open new connection", err);
  112. });
  113. }
  114. };
  115. var unregister = function () {
  116. window.removeEventListener("message", listener);
  117. };
  118. if (window.addEventListener) {
  119. window.addEventListener("message", listener, false);
  120. }
  121. }
  122. /**
  123. * Authenticate on the server.
  124. * @param {JitsiConference} room
  125. * @param {string} [lockPassword] password to use if the conference is locked
  126. */
  127. function doXmppAuth (room, lockPassword) {
  128. let loginDialog = LoginDialog.showAuthDialog(function (id, password) {
  129. // auth "on the fly":
  130. // 1. open new connection with proper id and password
  131. // 2. connect to the room
  132. // (this will store sessionId in the localStorage)
  133. // 3. close new connection
  134. // 4. reallocate focus in current room
  135. openConnection({id, password, roomName: room.getName()}).then(
  136. function (connection) {
  137. // open room
  138. let newRoom = connection.initJitsiConference(
  139. room.getName(), APP.conference._getConferenceOptions()
  140. );
  141. loginDialog.displayConnectionStatus('connection.FETCH_SESSION_ID');
  142. newRoom.room.moderator.authenticate().then(function () {
  143. connection.disconnect();
  144. loginDialog.displayConnectionStatus(
  145. 'connection.GOT_SESSION_ID');
  146. // authenticate conference on the fly
  147. room.join(lockPassword);
  148. loginDialog.close();
  149. }).catch(function (error, code) {
  150. connection.disconnect();
  151. logger.error('Auth on the fly failed', error);
  152. loginDialog.displayError(
  153. 'connection.GET_SESSION_ID_ERROR', {code: code});
  154. });
  155. }, function (err) {
  156. loginDialog.displayError(err);
  157. });
  158. }, function () { // user canceled
  159. loginDialog.close();
  160. });
  161. }
  162. /**
  163. * Authenticate for the conference.
  164. * Uses external service for auth if conference supports that.
  165. * @param {JitsiConference} room
  166. * @param {string} [lockPassword] password to use if the conference is locked
  167. */
  168. function authenticate (room, lockPassword) {
  169. if (isTokenAuthEnabled || room.isExternalAuthEnabled()) {
  170. doExternalAuth(room, lockPassword);
  171. } else {
  172. doXmppAuth(room, lockPassword);
  173. }
  174. }
  175. /**
  176. * De-authenticate local user.
  177. *
  178. * @param {JitsiConference} room
  179. * @param {string} [lockPassword] password to use if the conference is locked
  180. * @returns {Promise}
  181. */
  182. function logout (room) {
  183. return new Promise(function (resolve) {
  184. room.room.moderator.logout(resolve);
  185. }).then(function (url) {
  186. // de-authenticate conference on the fly
  187. if (room.isJoined()) {
  188. room.join();
  189. }
  190. return url;
  191. });
  192. }
  193. /**
  194. * Notify user that authentication is required to create the conference.
  195. * @param {JitsiConference} room
  196. * @param {string} [lockPassword] password to use if the conference is locked
  197. */
  198. function requireAuth(room, lockPassword) {
  199. if (authRequiredDialog) {
  200. return;
  201. }
  202. authRequiredDialog = LoginDialog.showAuthRequiredDialog(
  203. room.getName(), authenticate.bind(null, room, lockPassword)
  204. );
  205. }
  206. /**
  207. * Close auth-related dialogs if there are any.
  208. */
  209. function closeAuth() {
  210. if (externalAuthWindow) {
  211. externalAuthWindow.close();
  212. externalAuthWindow = null;
  213. }
  214. if (authRequiredDialog) {
  215. authRequiredDialog.close();
  216. authRequiredDialog = null;
  217. }
  218. }
  219. function showXmppPasswordPrompt(roomName, connect) {
  220. return new Promise(function (resolve, reject) {
  221. let authDialog = LoginDialog.showAuthDialog(
  222. function (id, password) {
  223. connect(id, password, roomName).then(function (connection) {
  224. authDialog.close();
  225. resolve(connection);
  226. }, function (err) {
  227. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  228. authDialog.displayError(err);
  229. } else {
  230. authDialog.close();
  231. reject(err);
  232. }
  233. });
  234. }
  235. );
  236. });
  237. }
  238. /**
  239. * Show Authentication Dialog and try to connect with new credentials.
  240. * If failed to connect because of PASSWORD_REQUIRED error
  241. * then ask for password again.
  242. * @param {string} [roomName] name of the conference room
  243. * @param {function(id, password, roomName)} [connect] function that returns
  244. * a Promise which resolves with JitsiConnection or fails with one of
  245. * ConnectionErrors.
  246. * @returns {Promise<JitsiConnection>}
  247. */
  248. function requestAuth(roomName, connect) {
  249. if (isTokenAuthEnabled) {
  250. // This Promise never resolves as user gets redirected to another URL
  251. return new Promise(() => redirectToTokenAuthService(roomName));
  252. } else {
  253. return showXmppPasswordPrompt(roomName, connect);
  254. }
  255. }
  256. export default {
  257. authenticate,
  258. requireAuth,
  259. requestAuth,
  260. closeAuth,
  261. logout
  262. };