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

functions.web.ts 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. *
  32. * @returns {Promise<string|undefined>} - The URL pointing to JWT login service or
  33. * <tt>undefined</tt> if the pattern stored in config is not a string and the URL can not be
  34. * constructed.
  35. */
  36. export const getTokenAuthUrl = (
  37. config: IConfig,
  38. roomName: string | undefined,
  39. tenant: string | undefined,
  40. skipPrejoin: boolean | undefined = false): Promise<string | undefined> => {
  41. let url = config.tokenAuthUrl;
  42. if (!url || !roomName) {
  43. return Promise.resolve(undefined);
  44. }
  45. if (url.indexOf('{state}')) {
  46. const state = _getTokenAuthState(roomName, tenant, skipPrejoin);
  47. if (browser.isElectron()) {
  48. // @ts-ignore
  49. state.electron = true;
  50. }
  51. url = url.replace('{state}', encodeURIComponent(JSON.stringify(state)));
  52. }
  53. url = url.replace('{room}', roomName);
  54. if (url.indexOf('{code_challenge}')) {
  55. let codeVerifier = '';
  56. // random string
  57. for (let i = 0; i < 64; i++) {
  58. codeVerifier += POSSIBLE_CHARS.charAt(Math.floor(_cryptoRandom() * POSSIBLE_CHARS.length));
  59. }
  60. window.sessionStorage.setItem('code_verifier', codeVerifier);
  61. return window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(codeVerifier))
  62. .then(digest => {
  63. // prepare code challenge - base64 encoding without padding as described in:
  64. // https://datatracker.ietf.org/doc/html/rfc7636#appendix-A
  65. const codeChallenge = base64js.fromByteArray(new Uint8Array(digest))
  66. .replace(/=/g, '')
  67. .replace(/\+/g, '-')
  68. .replace(/\//g, '_');
  69. return url ? url.replace('{code_challenge}', codeChallenge) : undefined;
  70. });
  71. }
  72. return Promise.resolve(url);
  73. };