選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

functions.ts 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // @ts-ignore
  2. import jwtDecode from 'jwt-decode';
  3. import { IState } from '../../app/types';
  4. import { getLocalParticipant } from '../participants/functions';
  5. import { parseURLParams } from '../util/parseURLParams';
  6. import { MEET_FEATURES } from './constants';
  7. /**
  8. * Retrieves the JSON Web Token (JWT), if any, defined by a specific
  9. * {@link URL}.
  10. *
  11. * @param {URL} url - The {@code URL} to parse and retrieve the JSON Web Token
  12. * (JWT), if any, from.
  13. * @returns {string} The JSON Web Token (JWT), if any, defined by the specified
  14. * {@code url}; otherwise, {@code undefined}.
  15. */
  16. export function parseJWTFromURLParams(url: URL | Location = window.location) {
  17. // @ts-ignore
  18. return parseURLParams(url, true, 'search').jwt;
  19. }
  20. /**
  21. * Returns the user name after decoding the jwt.
  22. *
  23. * @param {IState} state - The app state.
  24. * @returns {string}
  25. */
  26. export function getJwtName(state: IState) {
  27. const { user } = state['features/base/jwt'];
  28. return user?.name;
  29. }
  30. /**
  31. * Check if the given JWT feature is enabled.
  32. *
  33. * @param {IState} state - The app state.
  34. * @param {string} feature - The feature we want to check.
  35. * @param {boolean} ifNoToken - Default value if there is no token.
  36. * @returns {string}
  37. */
  38. export function isJwtFeatureEnabled(state: IState, feature: string, ifNoToken = false) {
  39. const { jwt } = state['features/base/jwt'];
  40. if (!jwt) {
  41. return ifNoToken;
  42. }
  43. const { features } = getLocalParticipant(state) || {};
  44. // If `features` is undefined, act as if everything is enabled.
  45. if (typeof features === 'undefined') {
  46. return true;
  47. }
  48. return String(features[feature as keyof typeof features]) === 'true';
  49. }
  50. /**
  51. * Checks whether a given timestamp is a valid UNIX timestamp in seconds.
  52. * We convert to milliseconds during the check since `Date` works with milliseconds for UNIX timestamp values.
  53. *
  54. * @param {any} timestamp - A UNIX timestamp in seconds as stored in the jwt.
  55. * @returns {boolean} - Whether the timestamp is indeed a valid UNIX timestamp or not.
  56. */
  57. function isValidUnixTimestamp(timestamp: number | string) {
  58. return typeof timestamp === 'number' && timestamp * 1000 === new Date(timestamp * 1000).getTime();
  59. }
  60. /**
  61. * Returns a list with all validation errors for the given jwt.
  62. *
  63. * @param {string} jwt - The jwt.
  64. * @returns {Array<string>} - An array containing all jwt validation errors.
  65. */
  66. export function validateJwt(jwt: string) {
  67. const errors: string[] = [];
  68. if (!jwt) {
  69. return errors;
  70. }
  71. const currentTimestamp = new Date().getTime();
  72. try {
  73. const header = jwtDecode(jwt, { header: true });
  74. const payload = jwtDecode(jwt);
  75. if (!header || !payload) {
  76. errors.push('- Missing header or payload');
  77. return errors;
  78. }
  79. const {
  80. aud,
  81. context,
  82. exp,
  83. iss,
  84. nbf,
  85. sub
  86. } = payload;
  87. // JaaS only
  88. if (sub?.startsWith('vpaas-magic-cookie')) {
  89. const { kid } = header;
  90. // if Key ID is missing, we return the error immediately without further validations.
  91. if (!kid) {
  92. errors.push('- Key ID(kid) missing');
  93. return errors;
  94. }
  95. if (kid.substring(0, kid.indexOf('/')) !== sub) {
  96. errors.push('- Key ID(kid) does not match sub');
  97. }
  98. if (aud !== 'jitsi') {
  99. errors.push('- invalid `aud` value. It should be `jitsi`');
  100. }
  101. if (iss !== 'chat') {
  102. errors.push('- invalid `iss` value. It should be `chat`');
  103. }
  104. if (!context?.features) {
  105. errors.push('- `features` object is missing from the payload');
  106. }
  107. }
  108. if (!isValidUnixTimestamp(nbf)) {
  109. errors.push('- invalid `nbf` value');
  110. } else if (currentTimestamp < nbf * 1000) {
  111. errors.push('- `nbf` value is in the future');
  112. }
  113. if (!isValidUnixTimestamp(exp)) {
  114. errors.push('- invalid `exp` value');
  115. } else if (currentTimestamp > exp * 1000) {
  116. errors.push('- token is expired');
  117. }
  118. if (!context) {
  119. errors.push('- `context` object is missing from the payload');
  120. } else if (context.features) {
  121. const { features } = context;
  122. const meetFeatures = Object.values(MEET_FEATURES);
  123. Object.keys(features).forEach(feature => {
  124. if (meetFeatures.includes(feature)) {
  125. const featureValue = features[feature];
  126. // cannot use truthy or falsy because we need the exact value and type check.
  127. if (
  128. featureValue !== true
  129. && featureValue !== false
  130. && featureValue !== 'true'
  131. && featureValue !== 'false'
  132. ) {
  133. errors.push(`- Invalid value for feature: ${feature}`);
  134. }
  135. } else {
  136. errors.push(`- Invalid feature: ${feature}`);
  137. }
  138. });
  139. }
  140. } catch (e: any) {
  141. errors.push(e ? e.message : '- unspecified jwt error');
  142. }
  143. return errors;
  144. }