token.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * 服务端token。token用于校验客户端权限。
  3. *
  4. * 此类暂时未使用,V1版本API暂时未传递此参数,V2版本API可计划增加此功能,提高系统安全性。
  5. *
  6. * HMAC: keyed-Hash Message Authentication Code
  7. *
  8. * author: Sand
  9. * since: 2016/11/21
  10. */
  11. "use strict";
  12. let crypto = require('crypto');
  13. let hmac = crypto.createHmac('sha1', 'theskyisblue');
  14. class Token{
  15. constructor(userId, clientId, platform){
  16. if(!userId || !clientId || !platform) throw {message: "Parameters must not be null."};
  17. hmac.update(userId + "->" + clientId + "->" + platform);
  18. let cipherText = hmac.digest('hex');
  19. this._userId = userId;
  20. this._clientId = clientId;
  21. this._platform = platform;
  22. this._value = cipherText;
  23. }
  24. get userId(){
  25. return this._userId;
  26. }
  27. get clientId(){
  28. return this._clientId;
  29. }
  30. get platform(){
  31. return this._platform;
  32. }
  33. get value(){
  34. return this._value;
  35. }
  36. }
  37. module.exports = Token;