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 4.3KB

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