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.ts 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // @ts-ignore
  2. import jwtDecode from 'jwt-decode';
  3. import { IReduxState } 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 | typeof window.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 {IReduxState} state - The app state.
  24. * @returns {string}
  25. */
  26. export function getJwtName(state: IReduxState) {
  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 {IReduxState} 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. * @param {boolean} ifNotInFeatures - Default value if features prop exists but does not have the {@code feature}.
  37. * @returns {bolean}
  38. */
  39. export function isJwtFeatureEnabled(state: IReduxState, feature: string, ifNoToken = false, ifNotInFeatures = false) {
  40. const { jwt } = state['features/base/jwt'];
  41. if (!jwt) {
  42. return ifNoToken;
  43. }
  44. const { features } = getLocalParticipant(state) || {};
  45. // If `features` is undefined, act as if everything is enabled.
  46. if (typeof features === 'undefined') {
  47. return true;
  48. }
  49. if (typeof features[feature as keyof typeof features] === 'undefined') {
  50. return ifNotInFeatures;
  51. }
  52. return String(features[feature as keyof typeof features]) === 'true';
  53. }
  54. /**
  55. * Checks whether a given timestamp is a valid UNIX timestamp in seconds.
  56. * We convert to milliseconds during the check since `Date` works with milliseconds for UNIX timestamp values.
  57. *
  58. * @param {any} timestamp - A UNIX timestamp in seconds as stored in the jwt.
  59. * @returns {boolean} - Whether the timestamp is indeed a valid UNIX timestamp or not.
  60. */
  61. function isValidUnixTimestamp(timestamp: number | string) {
  62. return typeof timestamp === 'number' && timestamp * 1000 === new Date(timestamp * 1000).getTime();
  63. }
  64. /**
  65. * Returns a list with all validation errors for the given jwt.
  66. *
  67. * @param {string} jwt - The jwt.
  68. * @returns {Array<string>} - An array containing all jwt validation errors.
  69. */
  70. export function validateJwt(jwt: string) {
  71. const errors: string[] = [];
  72. if (!jwt) {
  73. return errors;
  74. }
  75. const currentTimestamp = new Date().getTime();
  76. try {
  77. const header = jwtDecode(jwt, { header: true });
  78. const payload = jwtDecode(jwt);
  79. if (!header || !payload) {
  80. errors.push('- Missing header or payload');
  81. return errors;
  82. }
  83. const {
  84. aud,
  85. context,
  86. exp,
  87. iss,
  88. nbf,
  89. sub
  90. } = payload;
  91. // JaaS only
  92. if (sub?.startsWith('vpaas-magic-cookie')) {
  93. const { kid } = header;
  94. // if Key ID is missing, we return the error immediately without further validations.
  95. if (!kid) {
  96. errors.push('- Key ID(kid) missing');
  97. return errors;
  98. }
  99. if (kid.substring(0, kid.indexOf('/')) !== sub) {
  100. errors.push('- Key ID(kid) does not match sub');
  101. }
  102. if (aud !== 'jitsi') {
  103. errors.push('- invalid `aud` value. It should be `jitsi`');
  104. }
  105. if (iss !== 'chat') {
  106. errors.push('- invalid `iss` value. It should be `chat`');
  107. }
  108. if (!context?.features) {
  109. errors.push('- `features` object is missing from the payload');
  110. }
  111. }
  112. if (!isValidUnixTimestamp(nbf)) {
  113. errors.push('- invalid `nbf` value');
  114. } else if (currentTimestamp < nbf * 1000) {
  115. errors.push('- `nbf` value is in the future');
  116. }
  117. if (!isValidUnixTimestamp(exp)) {
  118. errors.push('- invalid `exp` value');
  119. } else if (currentTimestamp > exp * 1000) {
  120. errors.push('- token is expired');
  121. }
  122. if (!context) {
  123. errors.push('- `context` object is missing from the payload');
  124. } else if (context.features) {
  125. const { features } = context;
  126. const meetFeatures = Object.values(MEET_FEATURES);
  127. Object.keys(features).forEach(feature => {
  128. if (meetFeatures.includes(feature)) {
  129. const featureValue = features[feature];
  130. // cannot use truthy or falsy because we need the exact value and type check.
  131. if (
  132. featureValue !== true
  133. && featureValue !== false
  134. && featureValue !== 'true'
  135. && featureValue !== 'false'
  136. ) {
  137. errors.push(`- Invalid value for feature: ${feature}`);
  138. }
  139. } else {
  140. errors.push(`- Invalid feature: ${feature}`);
  141. }
  142. });
  143. }
  144. } catch (e: any) {
  145. errors.push(e ? e.message : '- unspecified jwt error');
  146. }
  147. return errors;
  148. }