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.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // @flow
  2. import { VPAAS_TENANT_PREFIX } from './constants';
  3. import logger from './logger';
  4. /**
  5. * Returns the full vpaas tenant if available, given a path.
  6. *
  7. * @param {string} path - The meeting url path.
  8. * @returns {string}
  9. */
  10. function extractVpaasTenantFromPath(path: string) {
  11. const [ , tenant ] = path.split('/');
  12. if (tenant.startsWith(VPAAS_TENANT_PREFIX)) {
  13. return tenant;
  14. }
  15. return '';
  16. }
  17. /**
  18. * Returns the vpaas tenant.
  19. *
  20. * @param {Object} state - The global state.
  21. * @returns {string}
  22. */
  23. export function getVpaasTenant(state: Object) {
  24. return extractVpaasTenantFromPath(state['features/base/connection'].locationURL.pathname);
  25. }
  26. /**
  27. * Returns true if the current meeting is a vpaas one.
  28. *
  29. * @param {Object} state - The state of the app.
  30. * @returns {boolean}
  31. */
  32. export function isVpaasMeeting(state: Object) {
  33. const connection = state['features/base/connection'];
  34. if (connection?.locationURL?.pathname) {
  35. return Boolean(
  36. extractVpaasTenantFromPath(connection?.locationURL?.pathname)
  37. );
  38. }
  39. return false;
  40. }
  41. /**
  42. * Sends a request for retrieving jaas customer details.
  43. *
  44. * @param {Object} reqData - The request info.
  45. * @param {string} reqData.appId - The client appId.
  46. * @param {string} reqData.baseUrl - The base url for the request.
  47. * @returns {void}
  48. */
  49. export async function sendGetDetailsRequest({ appId, baseUrl }: {
  50. appId: string,
  51. baseUrl: string,
  52. }) {
  53. const fullUrl = `${baseUrl}/v1/public/tenants/${encodeURIComponent(appId)}`;
  54. try {
  55. const res = await fetch(fullUrl);
  56. if (res.ok) {
  57. return res.json();
  58. }
  59. throw new Error('Request not successful');
  60. } catch (err) {
  61. throw new Error(err);
  62. }
  63. }
  64. /**
  65. * Returns the billing id for vpaas meetings.
  66. *
  67. * @param {Object} state - The state of the app.
  68. * @param {string} feature - Feature to be looked up for disable state.
  69. * @returns {boolean}
  70. */
  71. export function isFeatureDisabled(state: Object, feature: string) {
  72. return state['features/jaas'].disabledFeatures.includes(feature);
  73. }
  74. /**
  75. * Sends a request for retrieving jaas JWT.
  76. *
  77. * @param {Object} reqData - The request info.
  78. * @param {string} reqData.appId - The client appId.
  79. * @param {string} reqData.baseUrl - The base url for the request.
  80. * @returns {void}
  81. */
  82. export async function sendGetJWTRequest({ appId, baseUrl }: {
  83. appId: string,
  84. baseUrl: string
  85. }) {
  86. const fullUrl = `${baseUrl}/v1/public/token/${encodeURIComponent(appId)}`;
  87. try {
  88. const res = await fetch(fullUrl, {
  89. method: 'GET'
  90. });
  91. if (res.ok) {
  92. return res.json();
  93. }
  94. throw new Error('Request not successful');
  95. } catch (err) {
  96. throw new Error(err);
  97. }
  98. }
  99. /**
  100. * Gets a jaas JWT.
  101. *
  102. * @param {Object} state - Redux state.
  103. * @returns {string} The JWT.
  104. */
  105. export async function getJaasJWT(state: Object) {
  106. const baseUrl = state['features/base/config'].jaasTokenUrl;
  107. const appId = getVpaasTenant(state);
  108. const shouldSendRequest = Boolean(baseUrl && appId);
  109. if (shouldSendRequest) {
  110. try {
  111. const jwt = await sendGetJWTRequest({
  112. appId,
  113. baseUrl
  114. });
  115. return jwt.token;
  116. } catch (err) {
  117. logger.error('Could not send request', err);
  118. }
  119. }
  120. }