Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

functions.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // @flow
  2. import { getVpaasTenant } from '../billing-counter/functions';
  3. import logger from './logger';
  4. /**
  5. * Sends a request for retrieving jaas customer details.
  6. *
  7. * @param {Object} reqData - The request info.
  8. * @param {string} reqData.appId - The client appId.
  9. * @param {string} reqData.baseUrl - The base url for the request.
  10. * @param {string} reqData.jwt - The JWT token.
  11. * @returns {void}
  12. */
  13. export async function sendGetDetailsRequest({ appId, baseUrl, jwt }: {
  14. appId: string,
  15. baseUrl: string,
  16. jwt: string,
  17. }) {
  18. const fullUrl = `${baseUrl}/v1/customers/${encodeURIComponent(appId)}`;
  19. const headers = {
  20. 'Authorization': `Bearer ${jwt}`
  21. };
  22. try {
  23. const res = await fetch(fullUrl, {
  24. method: 'GET',
  25. headers
  26. });
  27. if (res.ok) {
  28. return res.json();
  29. }
  30. throw new Error('Request not successful');
  31. } catch (err) {
  32. throw new Error(err);
  33. }
  34. }
  35. /**
  36. * Returns the billing id for vpaas meetings.
  37. *
  38. * @param {Object} state - The state of the app.
  39. * @param {string} feature - Feature to be looked up for disable state.
  40. * @returns {boolean}
  41. */
  42. export function isFeatureDisabled(state: Object, feature: string) {
  43. return state['features/jaas'].disabledFeatures.includes(feature);
  44. }
  45. /**
  46. * Sends a request for retrieving jaas JWT.
  47. *
  48. * @param {Object} reqData - The request info.
  49. * @param {string} reqData.appId - The client appId.
  50. * @param {string} reqData.baseUrl - The base url for the request.
  51. * @returns {void}
  52. */
  53. export async function sendGetJWTRequest({ appId, baseUrl }: {
  54. appId: string,
  55. baseUrl: string
  56. }) {
  57. const fullUrl = `${baseUrl}/v1/public/token/${encodeURIComponent(appId)}`;
  58. try {
  59. const res = await fetch(fullUrl, {
  60. method: 'GET'
  61. });
  62. if (res.ok) {
  63. return res.json();
  64. }
  65. throw new Error('Request not successful');
  66. } catch (err) {
  67. throw new Error(err);
  68. }
  69. }
  70. /**
  71. * Gets a jaas JWT.
  72. *
  73. * @param {Object} state - Redux state.
  74. * @returns {string} The JWT.
  75. */
  76. export async function getJaasJWT(state: Object) {
  77. const baseUrl = state['features/base/config'].jaasTokenUrl;
  78. const appId = getVpaasTenant(state);
  79. const shouldSendRequest = Boolean(baseUrl && appId);
  80. if (shouldSendRequest) {
  81. try {
  82. const jwt = await sendGetJWTRequest({
  83. appId,
  84. baseUrl
  85. });
  86. return jwt.token;
  87. } catch (err) {
  88. logger.error('Could not send request', err);
  89. }
  90. }
  91. }