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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. const isTokenAuthEnabled
  13. = typeof config.tokenAuthUrl === 'string' && config.tokenAuthUrl.length;
  14. const 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(url => {
  37. externalAuthWindow = LoginDialog.showExternalAuthDialog(
  38. url,
  39. () => {
  40. externalAuthWindow = null;
  41. if (!isTokenAuthEnabled) {
  42. room.join(lockPassword);
  43. }
  44. }
  45. );
  46. });
  47. } else if (isTokenAuthEnabled) {
  48. redirectToTokenAuthService(room.getName());
  49. } else {
  50. room.getExternalAuthUrl().then(UIUtil.redirect);
  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. // FIXME: This method will not preserve the other URL params that were
  61. // originally passed.
  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. /**
  71. *
  72. */
  73. function listener({ data, source }) {
  74. if (externalAuthWindow !== source) {
  75. logger.warn('Ignored message not coming '
  76. + 'from external authnetication window');
  77. return;
  78. }
  79. let jwt;
  80. if (data && (jwt = data.jwtToken)) {
  81. logger.info('Received JSON Web Token (JWT):', jwt);
  82. APP.store.dispatch(setJWT(jwt));
  83. const roomName = room.getName();
  84. openConnection({
  85. retry: false,
  86. roomName
  87. }).then(connection => {
  88. // Start new connection
  89. const newRoom = connection.initJitsiConference(
  90. roomName, APP.conference._getConferenceOptions());
  91. // Authenticate from the new connection to get
  92. // the session-ID from the focus, which wil then be used
  93. // to upgrade current connection's user role
  94. newRoom.room.moderator.authenticate()
  95. .then(() => {
  96. connection.disconnect();
  97. // At this point we'll have session-ID stored in
  98. // the settings. It wil be used in the call below
  99. // to upgrade user's role
  100. room.room.moderator.authenticate()
  101. .then(() => {
  102. logger.info('User role upgrade done !');
  103. // eslint-disable-line no-use-before-define
  104. unregister();
  105. })
  106. .catch((err, errCode) => {
  107. logger.error('Authentication failed: ',
  108. err, errCode);
  109. unregister();
  110. });
  111. })
  112. .catch((error, code) => {
  113. unregister();
  114. connection.disconnect();
  115. logger.error(
  116. 'Authentication failed on the new connection',
  117. error, code);
  118. });
  119. }, err => {
  120. unregister();
  121. logger.error('Failed to open new connection', err);
  122. });
  123. }
  124. }
  125. /**
  126. *
  127. */
  128. function unregister() {
  129. window.removeEventListener('message', listener);
  130. }
  131. if (window.addEventListener) {
  132. window.addEventListener('message', listener, false);
  133. }
  134. }
  135. /**
  136. * Authenticate on the server.
  137. * @param {JitsiConference} room
  138. * @param {string} [lockPassword] password to use if the conference is locked
  139. */
  140. function doXmppAuth(room, lockPassword) {
  141. const loginDialog = LoginDialog.showAuthDialog(
  142. /* successCallback */ (id, password) => {
  143. room.authenticateAndUpgradeRole({
  144. id,
  145. password,
  146. roomPassword: lockPassword,
  147. /** Called when the XMPP login succeeds. */
  148. onLoginSuccessful() {
  149. loginDialog.displayConnectionStatus(
  150. 'connection.FETCH_SESSION_ID');
  151. }
  152. })
  153. .then(
  154. /* onFulfilled */ () => {
  155. loginDialog.displayConnectionStatus(
  156. 'connection.GOT_SESSION_ID');
  157. loginDialog.close();
  158. },
  159. /* onRejected */ error => {
  160. logger.error('authenticateAndUpgradeRole failed', error);
  161. const { authenticationError, connectionError } = error;
  162. if (authenticationError) {
  163. loginDialog.displayError(
  164. 'connection.GET_SESSION_ID_ERROR',
  165. { msg: authenticationError });
  166. } else if (connectionError) {
  167. loginDialog.displayError(connectionError);
  168. }
  169. });
  170. },
  171. /* cancelCallback */ () => loginDialog.close());
  172. }
  173. /**
  174. * Authenticate for the conference.
  175. * Uses external service for auth if conference supports that.
  176. * @param {JitsiConference} room
  177. * @param {string} [lockPassword] password to use if the conference is locked
  178. */
  179. function authenticate(room, lockPassword) {
  180. if (isTokenAuthEnabled || room.isExternalAuthEnabled()) {
  181. doExternalAuth(room, lockPassword);
  182. } else {
  183. doXmppAuth(room, lockPassword);
  184. }
  185. }
  186. /**
  187. * De-authenticate local user.
  188. *
  189. * @param {JitsiConference} room
  190. * @param {string} [lockPassword] password to use if the conference is locked
  191. * @returns {Promise}
  192. */
  193. function logout(room) {
  194. return new Promise(resolve => {
  195. room.room.moderator.logout(resolve);
  196. }).then(url => {
  197. // de-authenticate conference on the fly
  198. if (room.isJoined()) {
  199. room.join();
  200. }
  201. return url;
  202. });
  203. }
  204. /**
  205. * Notify user that authentication is required to create the conference.
  206. * @param {JitsiConference} room
  207. * @param {string} [lockPassword] password to use if the conference is locked
  208. */
  209. function requireAuth(room, lockPassword) {
  210. if (authRequiredDialog) {
  211. return;
  212. }
  213. authRequiredDialog = LoginDialog.showAuthRequiredDialog(
  214. room.getName(), authenticate.bind(null, room, lockPassword)
  215. );
  216. }
  217. /**
  218. * Close auth-related dialogs if there are any.
  219. */
  220. function closeAuth() {
  221. if (externalAuthWindow) {
  222. externalAuthWindow.close();
  223. externalAuthWindow = null;
  224. }
  225. if (authRequiredDialog) {
  226. authRequiredDialog.close();
  227. authRequiredDialog = null;
  228. }
  229. }
  230. /**
  231. *
  232. */
  233. function showXmppPasswordPrompt(roomName, connect) {
  234. return new Promise((resolve, reject) => {
  235. const authDialog = LoginDialog.showAuthDialog(
  236. (id, password) => {
  237. connect(id, password, roomName).then(connection => {
  238. authDialog.close();
  239. resolve(connection);
  240. }, err => {
  241. if (err === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  242. authDialog.displayError(err);
  243. } else {
  244. authDialog.close();
  245. reject(err);
  246. }
  247. });
  248. }
  249. );
  250. });
  251. }
  252. /**
  253. * Show Authentication Dialog and try to connect with new credentials.
  254. * If failed to connect because of PASSWORD_REQUIRED error
  255. * then ask for password again.
  256. * @param {string} [roomName] name of the conference room
  257. * @param {function(id, password, roomName)} [connect] function that returns
  258. * a Promise which resolves with JitsiConnection or fails with one of
  259. * JitsiConnectionErrors.
  260. * @returns {Promise<JitsiConnection>}
  261. */
  262. function requestAuth(roomName, connect) {
  263. if (isTokenAuthEnabled) {
  264. // This Promise never resolves as user gets redirected to another URL
  265. return new Promise(() => redirectToTokenAuthService(roomName));
  266. }
  267. return showXmppPasswordPrompt(roomName, connect);
  268. }
  269. export default {
  270. authenticate,
  271. requireAuth,
  272. requestAuth,
  273. closeAuth,
  274. logout
  275. };