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.

connection.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* global APP, JitsiMeetJS, config */
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. import Logger from 'jitsi-meet-logger';
  4. import { redirectToTokenAuthService } from './modules/UI/authentication/AuthHandler';
  5. import { LoginDialog } from './react/features/authentication/components';
  6. import { isTokenAuthEnabled } from './react/features/authentication/functions';
  7. import {
  8. connectionEstablished,
  9. connectionFailed
  10. } from './react/features/base/connection/actions';
  11. import { openDialog } from './react/features/base/dialog/actions';
  12. import { setJWT } from './react/features/base/jwt';
  13. import {
  14. isFatalJitsiConnectionError,
  15. JitsiConnectionErrors,
  16. JitsiConnectionEvents
  17. } from './react/features/base/lib-jitsi-meet';
  18. import { getCustomerDetails } from './react/features/jaas/actions.any';
  19. import { isVpaasMeeting, getJaasJWT } from './react/features/jaas/functions';
  20. import { setPrejoinDisplayNameRequired } from './react/features/prejoin/actions';
  21. const logger = Logger.getLogger(__filename);
  22. /**
  23. * The feature announced so we can distinguish jibri participants.
  24. *
  25. * @type {string}
  26. */
  27. export const DISCO_JIBRI_FEATURE = 'http://jitsi.org/protocol/jibri';
  28. /**
  29. * Checks if we have data to use attach instead of connect. If we have the data
  30. * executes attach otherwise check if we have to wait for the data. If we have
  31. * to wait for the attach data we are setting handler to APP.connect.handler
  32. * which is going to be called when the attach data is received otherwise
  33. * executes connect.
  34. *
  35. * @param {string} [id] user id
  36. * @param {string} [password] password
  37. * @param {string} [roomName] the name of the conference.
  38. */
  39. function checkForAttachParametersAndConnect(id, password, connection) {
  40. if (window.XMPPAttachInfo) {
  41. APP.connect.status = 'connecting';
  42. // When connection optimization is not deployed or enabled the default
  43. // value will be window.XMPPAttachInfo.status = "error"
  44. // If the connection optimization is deployed and enabled and there is
  45. // a failure the value will be window.XMPPAttachInfo.status = "error"
  46. if (window.XMPPAttachInfo.status === 'error') {
  47. connection.connect({
  48. id,
  49. password
  50. });
  51. return;
  52. }
  53. const attachOptions = window.XMPPAttachInfo.data;
  54. if (attachOptions) {
  55. connection.attach(attachOptions);
  56. delete window.XMPPAttachInfo.data;
  57. } else {
  58. connection.connect({
  59. id,
  60. password
  61. });
  62. }
  63. } else {
  64. APP.connect.status = 'ready';
  65. APP.connect.handler
  66. = checkForAttachParametersAndConnect.bind(
  67. null,
  68. id, password, connection);
  69. }
  70. }
  71. /**
  72. * Try to open connection using provided credentials.
  73. * @param {string} [id]
  74. * @param {string} [password]
  75. * @param {string} [roomName]
  76. * @returns {Promise<JitsiConnection>} connection if
  77. * everything is ok, else error.
  78. */
  79. export async function connect(id, password, roomName) {
  80. const connectionConfig = Object.assign({}, config);
  81. const state = APP.store.getState();
  82. let { jwt } = state['features/base/jwt'];
  83. const { iAmRecorder, iAmSipGateway } = state['features/base/config'];
  84. if (!iAmRecorder && !iAmSipGateway && isVpaasMeeting(state)) {
  85. await APP.store.dispatch(getCustomerDetails());
  86. if (!jwt) {
  87. jwt = await getJaasJWT(state);
  88. APP.store.dispatch(setJWT(jwt));
  89. }
  90. }
  91. // Use Websocket URL for the web app if configured. Note that there is no 'isWeb' check, because there's assumption
  92. // that this code executes only on web browsers/electron. This needs to be changed when mobile and web are unified.
  93. let serviceUrl = connectionConfig.websocket || connectionConfig.bosh;
  94. serviceUrl += `?room=${roomName}`;
  95. connectionConfig.serviceUrl = serviceUrl;
  96. if (connectionConfig.websocketKeepAliveUrl) {
  97. connectionConfig.websocketKeepAliveUrl += `?room=${roomName}`;
  98. }
  99. const connection = new JitsiMeetJS.JitsiConnection(null, jwt, connectionConfig);
  100. if (config.iAmRecorder) {
  101. connection.addFeature(DISCO_JIBRI_FEATURE);
  102. }
  103. return new Promise((resolve, reject) => {
  104. connection.addEventListener(
  105. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  106. handleConnectionEstablished);
  107. connection.addEventListener(
  108. JitsiConnectionEvents.CONNECTION_FAILED,
  109. handleConnectionFailed);
  110. connection.addEventListener(
  111. JitsiConnectionEvents.CONNECTION_FAILED,
  112. connectionFailedHandler);
  113. connection.addEventListener(
  114. JitsiConnectionEvents.DISPLAY_NAME_REQUIRED,
  115. displayNameRequiredHandler
  116. );
  117. /* eslint-disable max-params */
  118. /**
  119. *
  120. */
  121. function connectionFailedHandler(error, message, credentials, details) {
  122. /* eslint-enable max-params */
  123. APP.store.dispatch(
  124. connectionFailed(
  125. connection, {
  126. credentials,
  127. details,
  128. message,
  129. name: error
  130. }));
  131. if (isFatalJitsiConnectionError(error)) {
  132. connection.removeEventListener(
  133. JitsiConnectionEvents.CONNECTION_FAILED,
  134. connectionFailedHandler);
  135. }
  136. }
  137. /**
  138. *
  139. */
  140. function unsubscribe() {
  141. connection.removeEventListener(
  142. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  143. handleConnectionEstablished);
  144. connection.removeEventListener(
  145. JitsiConnectionEvents.CONNECTION_FAILED,
  146. handleConnectionFailed);
  147. }
  148. /**
  149. *
  150. */
  151. function handleConnectionEstablished() {
  152. APP.store.dispatch(connectionEstablished(connection, Date.now()));
  153. unsubscribe();
  154. resolve(connection);
  155. }
  156. /**
  157. *
  158. */
  159. function handleConnectionFailed(err) {
  160. unsubscribe();
  161. logger.error('CONNECTION FAILED:', err);
  162. reject(err);
  163. }
  164. /**
  165. * Marks the display name for the prejoin screen as required.
  166. * This can happen if a user tries to join a room with lobby enabled.
  167. */
  168. function displayNameRequiredHandler() {
  169. APP.store.dispatch(setPrejoinDisplayNameRequired());
  170. }
  171. checkForAttachParametersAndConnect(id, password, connection);
  172. });
  173. }
  174. /**
  175. * Open JitsiConnection using provided credentials.
  176. * If retry option is true it will show auth dialog on PASSWORD_REQUIRED error.
  177. *
  178. * @param {object} options
  179. * @param {string} [options.id]
  180. * @param {string} [options.password]
  181. * @param {string} [options.roomName]
  182. * @param {boolean} [retry] if we should show auth dialog
  183. * on PASSWORD_REQUIRED error.
  184. *
  185. * @returns {Promise<JitsiConnection>}
  186. */
  187. export function openConnection({ id, password, retry, roomName }) {
  188. const usernameOverride
  189. = jitsiLocalStorage.getItem('xmpp_username_override');
  190. const passwordOverride
  191. = jitsiLocalStorage.getItem('xmpp_password_override');
  192. if (usernameOverride && usernameOverride.length > 0) {
  193. id = usernameOverride; // eslint-disable-line no-param-reassign
  194. }
  195. if (passwordOverride && passwordOverride.length > 0) {
  196. password = passwordOverride; // eslint-disable-line no-param-reassign
  197. }
  198. return connect(id, password, roomName).catch(err => {
  199. if (retry) {
  200. const { jwt } = APP.store.getState()['features/base/jwt'];
  201. if (err === JitsiConnectionErrors.PASSWORD_REQUIRED && !jwt) {
  202. return requestAuth(roomName);
  203. }
  204. }
  205. throw err;
  206. });
  207. }
  208. /**
  209. * Show Authentication Dialog and try to connect with new credentials.
  210. * If failed to connect because of PASSWORD_REQUIRED error
  211. * then ask for password again.
  212. * @param {string} [roomName] name of the conference room
  213. *
  214. * @returns {Promise<JitsiConnection>}
  215. */
  216. function requestAuth(roomName) {
  217. const config = APP.store.getState()['features/base/config'];
  218. if (isTokenAuthEnabled(config)) {
  219. // This Promise never resolves as user gets redirected to another URL
  220. return new Promise(() => redirectToTokenAuthService(roomName));
  221. }
  222. return new Promise(resolve => {
  223. const onSuccess = connection => {
  224. resolve(connection);
  225. };
  226. APP.store.dispatch(
  227. openDialog(LoginDialog, { onSuccess,
  228. roomName })
  229. );
  230. });
  231. }