AuthClass.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  14. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  15. return new (P || (P = Promise))(function (resolve, reject) {
  16. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  17. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  18. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  19. step((generator = generator.apply(thisArg, _arguments || [])).next());
  20. });
  21. };
  22. var __generator = (this && this.__generator) || function (thisArg, body) {
  23. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  24. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  25. function verb(n) { return function (v) { return step([n, v]); }; }
  26. function step(op) {
  27. if (f) throw new TypeError("Generator is already executing.");
  28. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  29. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  30. if (y = 0, t) op = [op[0] & 2, t.value];
  31. switch (op[0]) {
  32. case 0: case 1: t = op; break;
  33. case 4: _.label++; return { value: op[1], done: false };
  34. case 5: _.label++; y = op[1]; op = [0]; continue;
  35. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  36. default:
  37. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  38. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  39. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  40. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  41. if (t[2]) _.ops.pop();
  42. _.trys.pop(); continue;
  43. }
  44. op = body.call(thisArg, _);
  45. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  46. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  47. }
  48. };
  49. Object.defineProperty(exports, "__esModule", { value: true });
  50. var restApi_1 = require("../apis/restApi");
  51. var auth_1 = require("../config/auth");
  52. var config_1 = require("../config/config");
  53. var dataUtil_1 = require("../utils/dataUtil");
  54. var logger_1 = require("../utils/logger");
  55. var AuthClass = (function () {
  56. function AuthClass() {
  57. this.init();
  58. }
  59. AuthClass.createInstance = function () {
  60. if (!AuthClass.instance) {
  61. AuthClass.instance = new AuthClass();
  62. }
  63. return AuthClass.instance;
  64. };
  65. AuthClass.prototype.init = function () {
  66. return __awaiter(this, void 0, void 0, function () {
  67. var auths, _a, _b, _c, _i, origin_1, auth, tmp, error_1;
  68. return __generator(this, function (_d) {
  69. switch (_d.label) {
  70. case 0:
  71. if (config_1.debugMode) {
  72. this.curAuth = auth_1.HaToken;
  73. return [2];
  74. }
  75. if (config_1.isSupervisor) {
  76. this.curAuth = process.env.SUPERVISOR_TOKEN;
  77. return [2];
  78. }
  79. _d.label = 1;
  80. case 1:
  81. _d.trys.push([1, 6, , 7]);
  82. auths = (0, dataUtil_1.getDataSync)('auth.json', []);
  83. _a = auths;
  84. _b = [];
  85. for (_c in _a)
  86. _b.push(_c);
  87. _i = 0;
  88. _d.label = 2;
  89. case 2:
  90. if (!(_i < _b.length)) return [3, 5];
  91. _c = _b[_i];
  92. if (!(_c in _a)) return [3, 4];
  93. origin_1 = _c;
  94. auth = JSON.parse(auths[origin_1]);
  95. if (!(auth && Date.now() < +auth.expires_time)) return [3, 4];
  96. AuthClass.AuthMap.set(origin_1, auth);
  97. return [4, this.refresh(origin_1)];
  98. case 3:
  99. tmp = _d.sent();
  100. if (tmp) {
  101. this.curAuth = auth.access_token;
  102. }
  103. _d.label = 4;
  104. case 4:
  105. _i++;
  106. return [3, 2];
  107. case 5: return [3, 7];
  108. case 6:
  109. error_1 = _d.sent();
  110. logger_1.logger.warn("AuthClass init error: ".concat(error_1));
  111. return [3, 7];
  112. case 7: return [2];
  113. }
  114. });
  115. });
  116. };
  117. AuthClass.prototype.isValid = function (host) {
  118. var auth = AuthClass.AuthMap.get(host);
  119. if (auth && auth.expires_time > Date.now()) {
  120. this.curAuth = auth.access_token;
  121. return true;
  122. }
  123. return false;
  124. };
  125. AuthClass.prototype.setAuth = function (origin, clientId, auth) {
  126. var _this = this;
  127. var data = __assign(__assign({}, auth), { cliend_id: clientId, expires_time: Date.now() + auth.expires_in * 1000 });
  128. this.curAuth = auth.access_token;
  129. AuthClass.AuthMap.set(origin, data);
  130. (0, dataUtil_1.appendData)('auth.json', [origin], JSON.stringify(data));
  131. setTimeout(function () {
  132. _this.refresh(origin);
  133. }, (auth.expires_in - 300) * 1000);
  134. };
  135. AuthClass.prototype.refresh = function (origin) {
  136. return __awaiter(this, void 0, void 0, function () {
  137. var auth, cliend_id, refresh_token, res;
  138. return __generator(this, function (_a) {
  139. switch (_a.label) {
  140. case 0:
  141. auth = AuthClass.AuthMap.get(origin);
  142. if (!auth) return [3, 2];
  143. cliend_id = auth.cliend_id, refresh_token = auth.refresh_token;
  144. if (config_1.isSupervisor) {
  145. AuthClass.AuthMap.delete(origin);
  146. return [2];
  147. }
  148. logger_1.logger.info('Start refresh the HA token');
  149. return [4, (0, restApi_1.refreshAuth)(cliend_id, refresh_token)];
  150. case 1:
  151. res = _a.sent();
  152. logger_1.logger.info('Refresh HA token success!');
  153. if (res && res.status === 200) {
  154. this.setAuth(origin, cliend_id, __assign(__assign({}, auth), res.data));
  155. }
  156. return [2, res.data];
  157. case 2: return [2];
  158. }
  159. });
  160. });
  161. };
  162. AuthClass.AuthMap = new Map();
  163. return AuthClass;
  164. }());
  165. var instance = AuthClass.createInstance();
  166. instance.init();
  167. exports.default = instance;