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ů.

TokenData.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* global getConfigParamsFromUrl, config */
  2. /**
  3. * Parses and handles JWT tokens. Sets config.token.
  4. */
  5. import * as jws from "jws";
  6. /**
  7. * Get the JWT token from the URL.
  8. */
  9. let params = getConfigParamsFromUrl("search", true);
  10. let jwt = params.jwt;
  11. /**
  12. * Implements a user of conference.
  13. */
  14. class User {
  15. /**
  16. * @param name {string} the name of the user.
  17. * @param email {string} the email of the user.
  18. * @param avatarUrl {string} the URL for the avatar of the user.
  19. */
  20. constructor(name, email, avatarUrl) {
  21. this._name = name;
  22. this._email = email;
  23. this._avatarUrl = avatarUrl;
  24. }
  25. /**
  26. * GETERS START.
  27. */
  28. /**
  29. * Returns the name property
  30. */
  31. getName() {
  32. return this._name;
  33. }
  34. /**
  35. * Returns the email property
  36. */
  37. getEmail() {
  38. return this._email;
  39. }
  40. /**
  41. * Returns the URL of the avatar
  42. */
  43. getAvatarUrl() {
  44. return this._avatarUrl;
  45. }
  46. /**
  47. * GETERS END.
  48. */
  49. }
  50. /**
  51. * Represent the data parsed from the JWT token
  52. */
  53. class TokenData{
  54. /**
  55. * @param {string} the JWT token
  56. */
  57. constructor(jwt) {
  58. this.isGuest = true;
  59. if(!jwt)
  60. return;
  61. this.isGuest = config.enableUserRolesBasedOnToken !== true;
  62. this.jwt = jwt;
  63. //External API settings
  64. this.externalAPISettings = {
  65. forceEnable: true,
  66. enabledEvents: ["video-conference-joined", "video-conference-left",
  67. "video-ready-to-close"]
  68. };
  69. this._decode();
  70. // Use JWT param as token if there is not other token set and if the
  71. // iss field is not anonymous. If you want to pass data with JWT token
  72. // but you don't want to pass the JWT token for verification the iss
  73. // field should be set to "anonymous"
  74. if(!config.token && this.payload && this.payload.iss !== "anonymous")
  75. config.token = jwt;
  76. }
  77. /**
  78. * Decodes the JWT token and sets the decoded data to properties.
  79. */
  80. _decode() {
  81. this.decodedJWT = jws.decode(jwt);
  82. if(!this.decodedJWT || !this.decodedJWT.payload)
  83. return;
  84. this.payload = this.decodedJWT.payload;
  85. if(!this.payload.context)
  86. return;
  87. this.server = this.payload.context.server;
  88. this.group = this.payload.context.group;
  89. let callerData = this.payload.context.user;
  90. let calleeData = this.payload.context.callee;
  91. if(callerData)
  92. this.caller = new User(callerData.name, callerData.email,
  93. callerData.avatarUrl);
  94. if(calleeData)
  95. this.callee = new User(calleeData.name, calleeData.email,
  96. calleeData.avatarUrl);
  97. }
  98. }
  99. /**
  100. * Stores the TokenData instance.
  101. */
  102. let data = null;
  103. /**
  104. * Returns the data variable. Creates new TokenData instance if <tt>data</tt>
  105. * variable is null.
  106. */
  107. export default function getTokenData() {
  108. if(!data)
  109. data = new TokenData(jwt);
  110. return data;
  111. }