您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TokenData.js 2.8KB

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