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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* global APP, config, JitsiMeetJS, Promise */
  2. import { openConnection } from '../../../connection';
  3. import { setJWT } from '../../../react/features/jwt';
  4. import UIUtil from '../util/UIUtil';
  5. import LoginDialog from './LoginDialog';
  6. const ConnectionErrors = JitsiMeetJS.errors.connection;
  7. const logger = require("jitsi-meet-logger").getLogger(__filename);
  8. let externalAuthWindow;
  9. let authRequiredDialog;
  10. let isTokenAuthEnabled
  11. = typeof config.tokenAuthUrl === "string" && config.tokenAuthUrl.length;
  12. let getTokenAuthUrl
  13. = JitsiMeetJS.util.AuthUtil.getTokenAuthUrl.bind(null, config.tokenAuthUrl);
  14. /**
  15. * Authenticate using external service or just focus
  16. * external auth window if there is one already.
  17. *
  18. * @param {JitsiConference} room
  19. * @param {string} [lockPassword] password to use if the conference is locked
  20. */
  21. function doExternalAuth (room, lockPassword) {
  22. if (externalAuthWindow) {
  23. externalAuthWindow.focus();
  24. return;
  25. }
  26. if (room.isJoined()) {
  27. let getUrl;
  28. if (isTokenAuthEnabled) {
  29. getUrl = Promise.resolve(getTokenAuthUrl(room.getName(), true));
  30. initJWTTokenListener(room);
  31. } else {
  32. getUrl = room.getExternalAuthUrl(true);
  33. }
  34. getUrl.then(function (url) {
  35. externalAuthWindow = LoginDialog.showExternalAuthDialog(
  36. url,
  37. function () {
  38. externalAuthWindow = null;
  39. if (!isTokenAuthEnabled) {
  40. room.join(lockPassword);
  41. }
  42. }
  43. );
  44. });
  45. } else {
  46. // If conference has not been started yet
  47. // then redirect to login page
  48. if (isTokenAuthEnabled) {
  49. redirectToTokenAuthService(room.getName());
  50. } else {
  51. room.getExternalAuthUrl().then(UIUtil.redirect);
  52. }
  53. }
  54. }
  55. /**
  56. * Redirect the user to the token authentication service for the login to be
  57. * performed. Once complete it is expected that the service wil bring the user
  58. * back with "?jwt={the JWT token}" query parameter added.
  59. * @param {string} [roomName] the name of the conference room.
  60. */
  61. function redirectToTokenAuthService(roomName) {
  62. UIUtil.redirect(getTokenAuthUrl(roomName, false));
  63. }
  64. /**
  65. * Initializes 'message' listener that will wait for a JWT token to be received
  66. * from the token authentication service opened in a popup window.
  67. * @param room the name fo the conference room.
  68. */
  69. function initJWTTokenListener(room) {
  70. var listener = function ({ data, source }) {
  71. if (externalAuthWindow !== source) {
  72. logger.warn("Ignored message not coming " +
  73. "from external authnetication window");
  74. return;
  75. }
  76. let jwt;
  77. if (data && (jwt = data.jwtToken)) {
  78. logger.info("Received JSON Web Token (JWT):", jwt);
  79. APP.store.dispatch(setJWT(jwt));
  80. var roomName = room.getName();
  81. openConnection({retry: false, roomName: roomName })
  82. .then(function (connection) {
  83. // Start new connection
  84. let newRoom = connection.initJitsiConference(
  85. roomName, APP.conference._getConferenceOptions());
  86. // Authenticate from the new connection to get
  87. // the session-ID from the focus, which wil then be used
  88. // to upgrade current connection's user role
  89. newRoom.room.moderator.authenticate().then(function () {
  90. connection.disconnect();
  91. // At this point we'll have session-ID stored in
  92. // the settings. It wil be used in the call below
  93. // to upgrade user's role
  94. room.room.moderator.authenticate()
  95. .then(function () {
  96. logger.info("User role upgrade done !");
  97. unregister();
  98. }).catch(function (err, errCode) {
  99. logger.error(
  100. "Authentication failed: ", err, errCode);
  101. unregister();
  102. }
  103. );
  104. }).catch(function (error, code) {
  105. unregister();
  106. connection.disconnect();
  107. logger.error(
  108. 'Authentication failed on the new connection',
  109. error, code);
  110. });
  111. }, function (err) {
  112. unregister();
  113. logger.error("Failed to open new connection", err);
  114. });
  115. }
  116. };
  117. var unregister = function () {
  118. window.removeEventListener("message", listener);
  119. };
  120. if (window.addEventListener) {
  121. window.addEventListener("message", listener, false);
  122. }
  123. }
  124. /**
  125. * Authenticate on the server.
  126. * @param {JitsiConference} room
  127. * @param {string} [lockPassword] password to use if the conference is locked
  128. */
  129. function doXmppAuth(room, lockPassword) {
  130. const loginDialog = LoginDialog.showAuthDialog(
  131. /* successCallback */ (id, password) => {
  132. room.authenticateAndUpgradeRole({
  133. id,
  134. password,
  135. roomPassword: lockPassword,
  136. /** Called when the XMPP login succeeds. */
  137. onLoginSuccessful() {
  138. loginDialog.displayConnectionStatus(
  139. 'connection.FETCH_SESSION_ID');
  140. }
  141. })
  142. .then(
  143. /* onFulfilled */ () => {
  144. loginDialog.displayConnectionStatus(
  145. 'connection.GOT_SESSION_ID');
  146. loginDialog.close();
  147. },
  148. /* onRejected */ error => {
  149. logger.error('authenticateAndUpgradeRole failed', error);
  150. const { authenticationError, connectionError } = error;
  151. if (authenticationError) {
  152. loginDialog.displayError(
  153. 'connection.GET_SESSION_ID_ERROR',
  154. { msg: authenticationError });
  155. } else if (connectionError) {
  156. loginDialog.displayError(connectionError);
  157. }
  158. });
  159. },
  160. /* cancelCallback */ () => loginDialog.close());
  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. };