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

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