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.

functions.web.ts 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import base64js from 'base64-js';
  2. import { IConfig } from '../base/config/configType';
  3. import { browser } from '../base/lib-jitsi-meet';
  4. import { _getTokenAuthState } from './functions.any';
  5. export * from './functions.any';
  6. /**
  7. * Based on rfc7636 we need a random string for a code verifier.
  8. */
  9. const POSSIBLE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  10. /**
  11. * Crypto random, alternative of Math.random.
  12. *
  13. * @returns {float} A random value.
  14. */
  15. function _cryptoRandom() {
  16. const typedArray = new Uint8Array(1);
  17. const randomValue = crypto.getRandomValues(typedArray)[0];
  18. return randomValue / Math.pow(2, 8);
  19. }
  20. /**
  21. * Creates the URL pointing to JWT token authentication service. It is
  22. * formatted from the 'urlPattern' argument which can contain the following
  23. * constants:
  24. * '{room}' - name of the conference room passed as <tt>roomName</tt>
  25. * argument to this method.
  26. *
  27. * @param {Object} config - Configuration state object from store. A URL pattern pointing to the login service.
  28. * @param {string} roomName - The name of the conference room for which the user will be authenticated.
  29. * @param {string} tenant - The name of the conference tenant.
  30. * @param {string} skipPrejoin - The name of the conference room for which the user will be authenticated.
  31. * @param {URL} locationURL - The current location URL.
  32. *
  33. * @returns {Promise<string|undefined>} - The URL pointing to JWT login service or
  34. * <tt>undefined</tt> if the pattern stored in config is not a string and the URL can not be
  35. * constructed.
  36. */
  37. export const getTokenAuthUrl = (
  38. config: IConfig,
  39. roomName: string | undefined,
  40. tenant: string | undefined,
  41. skipPrejoin: boolean | undefined = false,
  42. // eslint-disable-next-line max-params
  43. locationURL: URL): Promise<string | undefined> => {
  44. let url = config.tokenAuthUrl;
  45. if (!url || !roomName) {
  46. return Promise.resolve(undefined);
  47. }
  48. if (url.indexOf('{state}')) {
  49. const state = _getTokenAuthState(roomName, tenant, skipPrejoin, locationURL);
  50. if (browser.isElectron()) {
  51. // @ts-ignore
  52. state.electron = true;
  53. }
  54. url = url.replace('{state}', encodeURIComponent(JSON.stringify(state)));
  55. }
  56. url = url.replace('{room}', roomName);
  57. if (url.indexOf('{code_challenge}')) {
  58. let codeVerifier = '';
  59. // random string
  60. for (let i = 0; i < 64; i++) {
  61. codeVerifier += POSSIBLE_CHARS.charAt(Math.floor(_cryptoRandom() * POSSIBLE_CHARS.length));
  62. }
  63. window.sessionStorage.setItem('code_verifier', codeVerifier);
  64. return window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(codeVerifier))
  65. .then(digest => {
  66. // prepare code challenge - base64 encoding without padding as described in:
  67. // https://datatracker.ietf.org/doc/html/rfc7636#appendix-A
  68. const codeChallenge = base64js.fromByteArray(new Uint8Array(digest))
  69. .replace(/=/g, '')
  70. .replace(/\+/g, '-')
  71. .replace(/\//g, '_');
  72. return url ? url.replace('{code_challenge}', codeChallenge) : undefined;
  73. });
  74. }
  75. return Promise.resolve(url);
  76. };