您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AuthHandler.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. }).catch(function (error, code) {
  104. unregister();
  105. connection.disconnect();
  106. logger.error(
  107. 'Authentication failed on the new connection',
  108. error, code);
  109. });
  110. }, function (err) {
  111. unregister();
  112. logger.error("Failed to open new connection", err);
  113. });
  114. }
  115. };
  116. var unregister = function () {
  117. window.removeEventListener("message", listener);
  118. };
  119. if (window.addEventListener) {
  120. window.addEventListener("message", listener, false);
  121. }
  122. }
  123. /**
  124. * Authenticate on the server.
  125. * @param {JitsiConference} room
  126. * @param {string} [lockPassword] password to use if the conference is locked
  127. */
  128. function doXmppAuth(room, lockPassword) {
  129. const loginDialog = LoginDialog.showAuthDialog(
  130. /* successCallback */ (id, password) => {
  131. room.authenticateAndUpgradeRole({
  132. id,
  133. password,
  134. roomPassword: lockPassword,
  135. /** Called when the XMPP login succeeds. */
  136. onLoginSuccessful() {
  137. loginDialog.displayConnectionStatus(
  138. 'connection.FETCH_SESSION_ID');
  139. }
  140. })
  141. .then(
  142. /* onFulfilled */ () => {
  143. loginDialog.displayConnectionStatus(
  144. 'connection.GOT_SESSION_ID');
  145. loginDialog.close();
  146. },
  147. /* onRejected */ error => {
  148. logger.error('authenticateAndUpgradeRole failed', error);
  149. const { authenticationError, connectionError } = error;
  150. if (authenticationError) {
  151. loginDialog.displayError(
  152. 'connection.GET_SESSION_ID_ERROR',
  153. { msg: authenticationError });
  154. } else if (connectionError) {
  155. loginDialog.displayError(connectionError);
  156. }
  157. });
  158. },
  159. /* cancelCallback */ () => loginDialog.close());
  160. }
  161. /**
  162. * Authenticate for the conference.
  163. * Uses external service for auth if conference supports that.
  164. * @param {JitsiConference} room
  165. * @param {string} [lockPassword] password to use if the conference is locked
  166. */
  167. function authenticate (room, lockPassword) {
  168. if (isTokenAuthEnabled || room.isExternalAuthEnabled()) {
  169. doExternalAuth(room, lockPassword);
  170. } else {
  171. doXmppAuth(room, lockPassword);
  172. }
  173. }
  174. /**
  175. * De-authenticate local user.
  176. *
  177. * @param {JitsiConference} room
  178. * @param {string} [lockPassword] password to use if the conference is locked
  179. * @returns {Promise}
  180. */
  181. function logout (room) {
  182. return new Promise(function (resolve) {
  183. room.room.moderator.logout(resolve);
  184. }).then(function (url) {
  185. // de-authenticate conference on the fly
  186. if (room.isJoined()) {
  187. room.join();
  188. }
  189. return url;
  190. });
  191. }
  192. /**
  193. * Notify user that authentication is required to create the conference.
  194. * @param {JitsiConference} room
  195. * @param {string} [lockPassword] password to use if the conference is locked
  196. */
  197. function requireAuth(room, lockPassword) {
  198. if (authRequiredDialog) {
  199. return;
  200. }
  201. authRequiredDialog = LoginDialog.showAuthRequiredDialog(
  202. room.getName(), authenticate.bind(null, room, lockPassword)
  203. );
  204. }
  205. /**
  206. * Close auth-related dialogs if there are any.
  207. */
  208. function closeAuth() {
  209. if (externalAuthWindow) {
  210. externalAuthWindow.close();
  211. externalAuthWindow = null;
  212. }
  213. if (authRequiredDialog) {
  214. authRequiredDialog.close();
  215. authRequiredDialog = null;
  216. }
  217. }
  218. function showXmppPasswordPrompt(roomName, connect) {
  219. return new Promise(function (resolve, reject) {
  220. let authDialog = LoginDialog.showAuthDialog(
  221. function (id, password) {
  222. connect(id, password, roomName).then(function (connection) {
  223. authDialog.close();
  224. resolve(connection);
  225. }, function (err) {
  226. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  227. authDialog.displayError(err);
  228. } else {
  229. authDialog.close();
  230. reject(err);
  231. }
  232. });
  233. }
  234. );
  235. });
  236. }
  237. /**
  238. * Show Authentication Dialog and try to connect with new credentials.
  239. * If failed to connect because of PASSWORD_REQUIRED error
  240. * then ask for password again.
  241. * @param {string} [roomName] name of the conference room
  242. * @param {function(id, password, roomName)} [connect] function that returns
  243. * a Promise which resolves with JitsiConnection or fails with one of
  244. * ConnectionErrors.
  245. * @returns {Promise<JitsiConnection>}
  246. */
  247. function requestAuth(roomName, connect) {
  248. if (isTokenAuthEnabled) {
  249. // This Promise never resolves as user gets redirected to another URL
  250. return new Promise(() => redirectToTokenAuthService(roomName));
  251. } else {
  252. return showXmppPasswordPrompt(roomName, connect);
  253. }
  254. }
  255. export default {
  256. authenticate,
  257. requireAuth,
  258. requestAuth,
  259. closeAuth,
  260. logout
  261. };