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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * @param {string} reqData.jwt - The JWT token.
  48. * @returns {void}
  49. */
  50. export async function sendGetDetailsRequest({ appId, baseUrl, jwt }: {
  51. appId: string,
  52. baseUrl: string,
  53. jwt: string,
  54. }) {
  55. const fullUrl = `${baseUrl}/v1/customers/${encodeURIComponent(appId)}`;
  56. const headers = {
  57. 'Authorization': `Bearer ${jwt}`
  58. };
  59. try {
  60. const res = await fetch(fullUrl, {
  61. method: 'GET',
  62. headers
  63. });
  64. if (res.ok) {
  65. return res.json();
  66. }
  67. throw new Error('Request not successful');
  68. } catch (err) {
  69. throw new Error(err);
  70. }
  71. }
  72. /**
  73. * Returns the billing id for vpaas meetings.
  74. *
  75. * @param {Object} state - The state of the app.
  76. * @param {string} feature - Feature to be looked up for disable state.
  77. * @returns {boolean}
  78. */
  79. export function isFeatureDisabled(state: Object, feature: string) {
  80. return state['features/jaas'].disabledFeatures.includes(feature);
  81. }
  82. /**
  83. * Sends a request for retrieving jaas JWT.
  84. *
  85. * @param {Object} reqData - The request info.
  86. * @param {string} reqData.appId - The client appId.
  87. * @param {string} reqData.baseUrl - The base url for the request.
  88. * @returns {void}
  89. */
  90. export async function sendGetJWTRequest({ appId, baseUrl }: {
  91. appId: string,
  92. baseUrl: string
  93. }) {
  94. const fullUrl = `${baseUrl}/v1/public/token/${encodeURIComponent(appId)}`;
  95. try {
  96. const res = await fetch(fullUrl, {
  97. method: 'GET'
  98. });
  99. if (res.ok) {
  100. return res.json();
  101. }
  102. throw new Error('Request not successful');
  103. } catch (err) {
  104. throw new Error(err);
  105. }
  106. }
  107. /**
  108. * Gets a jaas JWT.
  109. *
  110. * @param {Object} state - Redux state.
  111. * @returns {string} The JWT.
  112. */
  113. export async function getJaasJWT(state: Object) {
  114. const baseUrl = state['features/base/config'].jaasTokenUrl;
  115. const appId = getVpaasTenant(state);
  116. const shouldSendRequest = Boolean(baseUrl && appId);
  117. if (shouldSendRequest) {
  118. try {
  119. const jwt = await sendGetJWTRequest({
  120. appId,
  121. baseUrl
  122. });
  123. return jwt.token;
  124. } catch (err) {
  125. logger.error('Could not send request', err);
  126. }
  127. }
  128. }