clientAuthController.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ClientAuthController = void 0;
  4. const clientAuth_1 = require("../models/clientAuth");
  5. const helpers_1 = require("../utils/helpers");
  6. class ClientAuthController {
  7. static async getAllClientAuth(req, res) {
  8. try {
  9. const page = Number(req.query.page) || 1;
  10. const limit = Number(req.query.limit) || 20;
  11. const offset = (page - 1) * limit;
  12. const clientAuths = await clientAuth_1.ClientAuthModel.getAll(limit, offset);
  13. const total = await clientAuth_1.ClientAuthModel.getCount();
  14. res.status(200).json({
  15. success: true,
  16. data: clientAuths,
  17. pagination: {
  18. page,
  19. limit,
  20. total,
  21. pages: Math.ceil(total / limit)
  22. }
  23. });
  24. }
  25. catch (error) {
  26. console.error('获取客户端认证列表失败:', error);
  27. res.status(500).json({
  28. success: false,
  29. message: '获取客户端认证列表失败',
  30. error: error instanceof Error ? error.message : '未知错误'
  31. });
  32. }
  33. }
  34. static async getClientAuthById(req, res) {
  35. try {
  36. const id = (0, helpers_1.toString)(req.params.id);
  37. if (!id || isNaN(Number(id))) {
  38. res.status(400).json({
  39. success: false,
  40. message: '无效的ID'
  41. });
  42. return;
  43. }
  44. const clientAuth = await clientAuth_1.ClientAuthModel.getById(Number(id));
  45. if (!clientAuth) {
  46. res.status(404).json({
  47. success: false,
  48. message: '客户端认证信息不存在'
  49. });
  50. return;
  51. }
  52. res.status(200).json({
  53. success: true,
  54. data: clientAuth
  55. });
  56. }
  57. catch (error) {
  58. console.error('获取客户端认证信息失败:', error);
  59. res.status(500).json({
  60. success: false,
  61. message: '获取客户端认证信息失败',
  62. error: error instanceof Error ? error.message : '未知错误'
  63. });
  64. }
  65. }
  66. static async getClientAuthByUsername(req, res) {
  67. try {
  68. const { username } = req.params;
  69. const usernameStr = (0, helpers_1.toString)(username);
  70. if (!usernameStr) {
  71. res.status(400).json({
  72. success: false,
  73. message: '用户名不能为空'
  74. });
  75. return;
  76. }
  77. const clientAuth = await clientAuth_1.ClientAuthModel.getByUsername(usernameStr);
  78. if (!clientAuth) {
  79. res.status(404).json({
  80. success: false,
  81. message: '客户端认证信息不存在'
  82. });
  83. return;
  84. }
  85. res.status(200).json({
  86. success: true,
  87. data: clientAuth
  88. });
  89. }
  90. catch (error) {
  91. console.error('根据用户名获取客户端认证信息失败:', error);
  92. res.status(500).json({
  93. success: false,
  94. message: '根据用户名获取客户端认证信息失败',
  95. error: error instanceof Error ? error.message : '未知错误'
  96. });
  97. }
  98. }
  99. static async getClientAuthByClientId(req, res) {
  100. try {
  101. const { clientid } = req.params;
  102. const clientidStr = (0, helpers_1.toString)(clientid);
  103. if (!clientidStr) {
  104. res.status(400).json({
  105. success: false,
  106. message: '客户端ID不能为空'
  107. });
  108. return;
  109. }
  110. const clientAuth = await clientAuth_1.ClientAuthModel.getByClientId(clientidStr);
  111. if (!clientAuth) {
  112. res.status(404).json({
  113. success: false,
  114. message: '客户端认证信息不存在'
  115. });
  116. return;
  117. }
  118. res.status(200).json({
  119. success: true,
  120. data: clientAuth
  121. });
  122. }
  123. catch (error) {
  124. console.error('根据客户端ID获取客户端认证信息失败:', error);
  125. res.status(500).json({
  126. success: false,
  127. message: '根据客户端ID获取客户端认证信息失败',
  128. error: error instanceof Error ? error.message : '未知错误'
  129. });
  130. }
  131. }
  132. static async createClientAuth(req, res) {
  133. try {
  134. const { username, clientid, password, device_type, description, is_superuser, use_salt, auth_method, auth_expiry, allowed_ip_ranges, allowed_time_ranges, auth_policy_id } = req.body;
  135. if (!username || !clientid || !password) {
  136. res.status(400).json({
  137. success: false,
  138. message: '用户名、客户端ID和密码不能为空'
  139. });
  140. return;
  141. }
  142. const existingByUsername = await clientAuth_1.ClientAuthModel.getByUsername(username);
  143. if (existingByUsername) {
  144. res.status(400).json({
  145. success: false,
  146. message: '用户名已存在'
  147. });
  148. return;
  149. }
  150. const existingByClientId = await clientAuth_1.ClientAuthModel.getByClientId(clientid);
  151. if (existingByClientId) {
  152. res.status(400).json({
  153. success: false,
  154. message: '客户端ID已存在'
  155. });
  156. return;
  157. }
  158. const shouldUseSalt = use_salt !== undefined ? Boolean(use_salt) : true;
  159. const salt = shouldUseSalt ? clientAuth_1.ClientAuthModel.generateSalt() : '';
  160. const passwordHash = clientAuth_1.ClientAuthModel.generatePasswordHash(password, salt, shouldUseSalt);
  161. const newClientAuth = await clientAuth_1.ClientAuthModel.create({
  162. username,
  163. clientid,
  164. password_hash: passwordHash,
  165. salt: salt,
  166. status: 'enabled',
  167. device_type: device_type || null,
  168. description: description || null,
  169. is_superuser: is_superuser || false,
  170. use_salt: shouldUseSalt,
  171. auth_method: auth_method || 'password',
  172. auth_expiry: auth_expiry ? new Date(auth_expiry) : null,
  173. allowed_ip_ranges: allowed_ip_ranges ? JSON.stringify(allowed_ip_ranges) : null,
  174. allowed_time_ranges: allowed_time_ranges ? JSON.stringify(allowed_time_ranges) : null,
  175. auth_policy_id: auth_policy_id || null
  176. });
  177. await clientAuth_1.ClientAuthModel.logAuthEvent(newClientAuth.clientid, newClientAuth.username, 'connect', 'success', 'Client authentication created', req.ip, undefined, auth_method, auth_policy_id);
  178. res.status(201).json({
  179. success: true,
  180. data: newClientAuth,
  181. message: '客户端认证信息创建成功'
  182. });
  183. }
  184. catch (error) {
  185. console.error('创建客户端认证信息失败:', error);
  186. res.status(500).json({
  187. success: false,
  188. message: '创建客户端认证信息失败',
  189. error: error instanceof Error ? error.message : '未知错误'
  190. });
  191. }
  192. }
  193. static async updateClientAuth(req, res) {
  194. try {
  195. const id = (0, helpers_1.toString)(req.params.id);
  196. const { username, clientid, password, device_type, description, status, is_superuser, use_salt, auth_method, auth_expiry, allowed_ip_ranges, allowed_time_ranges, auth_policy_id } = req.body;
  197. if (!id || isNaN(Number(id))) {
  198. res.status(400).json({
  199. success: false,
  200. message: '无效的ID'
  201. });
  202. return;
  203. }
  204. const existingClientAuth = await clientAuth_1.ClientAuthModel.getById(Number(id));
  205. if (!existingClientAuth) {
  206. res.status(404).json({
  207. success: false,
  208. message: '客户端认证信息不存在'
  209. });
  210. return;
  211. }
  212. if (username && username !== existingClientAuth.username) {
  213. const existingByUsername = await clientAuth_1.ClientAuthModel.getByUsername(username);
  214. if (existingByUsername) {
  215. res.status(400).json({
  216. success: false,
  217. message: '用户名已存在'
  218. });
  219. return;
  220. }
  221. }
  222. if (clientid && clientid !== existingClientAuth.clientid) {
  223. const existingByClientId = await clientAuth_1.ClientAuthModel.getByClientId(clientid);
  224. if (existingByClientId) {
  225. res.status(400).json({
  226. success: false,
  227. message: '客户端ID已存在'
  228. });
  229. return;
  230. }
  231. }
  232. const updateData = {};
  233. if (username !== undefined && username !== existingClientAuth.username) {
  234. updateData.username = username;
  235. }
  236. if (clientid !== undefined && clientid !== existingClientAuth.clientid) {
  237. updateData.clientid = clientid;
  238. }
  239. if (password !== undefined) {
  240. const shouldUseSalt = use_salt !== undefined ? Boolean(use_salt) : existingClientAuth.use_salt;
  241. const salt = shouldUseSalt ? clientAuth_1.ClientAuthModel.generateSalt() : '';
  242. const passwordHash = clientAuth_1.ClientAuthModel.generatePasswordHash(password, salt, shouldUseSalt);
  243. updateData.password_hash = passwordHash;
  244. updateData.salt = salt;
  245. }
  246. if (device_type !== undefined) {
  247. updateData.device_type = device_type;
  248. }
  249. if (description !== undefined) {
  250. updateData.description = description;
  251. }
  252. if (status !== undefined) {
  253. updateData.status = status;
  254. }
  255. if (is_superuser !== undefined) {
  256. updateData.is_superuser = is_superuser;
  257. }
  258. if (use_salt !== undefined) {
  259. updateData.use_salt = Boolean(use_salt);
  260. if (password === undefined) {
  261. const shouldUseSalt = Boolean(use_salt);
  262. const salt = shouldUseSalt ? clientAuth_1.ClientAuthModel.generateSalt() : '';
  263. updateData.salt = salt;
  264. }
  265. }
  266. if (auth_method !== undefined) {
  267. updateData.auth_method = auth_method;
  268. }
  269. if (auth_expiry !== undefined) {
  270. updateData.auth_expiry = auth_expiry ? new Date(auth_expiry) : null;
  271. }
  272. if (allowed_ip_ranges !== undefined) {
  273. updateData.allowed_ip_ranges = allowed_ip_ranges ? JSON.stringify(allowed_ip_ranges) : null;
  274. }
  275. if (allowed_time_ranges !== undefined) {
  276. updateData.allowed_time_ranges = allowed_time_ranges ? JSON.stringify(allowed_time_ranges) : null;
  277. }
  278. if (auth_policy_id !== undefined) {
  279. updateData.auth_policy_id = auth_policy_id;
  280. }
  281. const updatedClientAuth = await clientAuth_1.ClientAuthModel.update(Number(id), updateData);
  282. if (!updatedClientAuth) {
  283. res.status(500).json({
  284. success: false,
  285. message: '更新客户端认证信息失败'
  286. });
  287. return;
  288. }
  289. await clientAuth_1.ClientAuthModel.logAuthEvent(updatedClientAuth.clientid, updatedClientAuth.username, 'connect', 'success', 'Client authentication updated', req.ip, undefined, updatedClientAuth.auth_method, updatedClientAuth.auth_policy_id || undefined);
  290. res.status(200).json({
  291. success: true,
  292. data: updatedClientAuth,
  293. message: '客户端认证信息更新成功'
  294. });
  295. }
  296. catch (error) {
  297. console.error('更新客户端认证信息失败:', error);
  298. res.status(500).json({
  299. success: false,
  300. message: '更新客户端认证信息失败',
  301. error: error instanceof Error ? error.message : '未知错误'
  302. });
  303. }
  304. }
  305. static async deleteClientAuth(req, res) {
  306. try {
  307. const id = (0, helpers_1.toString)(req.params.id);
  308. if (!id || isNaN(Number(id))) {
  309. res.status(400).json({
  310. success: false,
  311. message: '无效的ID'
  312. });
  313. return;
  314. }
  315. const existingClientAuth = await clientAuth_1.ClientAuthModel.getById(Number(id));
  316. if (!existingClientAuth) {
  317. res.status(404).json({
  318. success: false,
  319. message: '客户端认证信息不存在'
  320. });
  321. return;
  322. }
  323. await clientAuth_1.ClientAuthModel.delete(Number(id));
  324. await clientAuth_1.ClientAuthModel.logAuthEvent(existingClientAuth.clientid, existingClientAuth.username, 'connect', 'success', 'Client authentication deleted', req.ip);
  325. res.status(200).json({
  326. success: true,
  327. message: '客户端认证信息删除成功'
  328. });
  329. }
  330. catch (error) {
  331. console.error('删除客户端认证信息失败:', error);
  332. res.status(500).json({
  333. success: false,
  334. message: '删除客户端认证信息失败',
  335. error: error instanceof Error ? error.message : '未知错误'
  336. });
  337. }
  338. }
  339. static async verifyClientAuth(req, res) {
  340. try {
  341. const { username, clientid, password } = req.body;
  342. if (!username || !clientid || !password) {
  343. res.status(400).json({
  344. success: false,
  345. message: '用户名、客户端ID和密码不能为空'
  346. });
  347. return;
  348. }
  349. const startTime = Date.now();
  350. const isValid = await clientAuth_1.ClientAuthModel.verifyClient(username, clientid, password);
  351. const executionTime = Date.now() - startTime;
  352. await clientAuth_1.ClientAuthModel.logAuthEvent(clientid, username, 'connect', isValid ? 'success' : 'failure', isValid ? undefined : 'Invalid credentials', req.ip, undefined, 'password', undefined, executionTime);
  353. if (isValid) {
  354. res.status(200).json({
  355. success: true,
  356. message: '客户端认证信息验证成功'
  357. });
  358. }
  359. else {
  360. res.status(401).json({
  361. success: false,
  362. message: '客户端认证信息验证失败'
  363. });
  364. }
  365. }
  366. catch (error) {
  367. console.error('验证客户端认证信息失败:', error);
  368. res.status(500).json({
  369. success: false,
  370. message: '验证客户端认证信息失败',
  371. error: error instanceof Error ? error.message : '未知错误'
  372. });
  373. }
  374. }
  375. static async mqttPasswordAuth(req, res) {
  376. try {
  377. const { username, clientid, password } = req.body;
  378. if (!username || !password) {
  379. res.status(200).json({
  380. result: false,
  381. reason: '用户名和密码不能为空'
  382. });
  383. return;
  384. }
  385. const clientAuth = await clientAuth_1.ClientAuthModel.getByUsername(username);
  386. if (!clientAuth) {
  387. res.status(200).json({
  388. result: false,
  389. reason: '用户不存在'
  390. });
  391. return;
  392. }
  393. if (clientAuth.status !== 'enabled') {
  394. res.status(200).json({
  395. result: false,
  396. reason: '用户已被禁用'
  397. });
  398. return;
  399. }
  400. const useSalt = clientAuth.use_salt !== undefined ? clientAuth.use_salt : true;
  401. const isValidPassword = clientAuth_1.ClientAuthModel.verifyPassword(password, clientAuth.salt, clientAuth.password_hash, useSalt);
  402. if (!isValidPassword) {
  403. res.status(200).json({
  404. result: false,
  405. reason: '密码无效'
  406. });
  407. return;
  408. }
  409. await clientAuth_1.ClientAuthModel.logAuthEvent(clientAuth.clientid, username, 'connect', 'success', '常规密码认证成功', req.ip);
  410. res.status(200).json({
  411. result: true,
  412. is_superuser: clientAuth.is_superuser === true,
  413. acl: []
  414. });
  415. }
  416. catch (error) {
  417. console.error('MQTT密码认证失败:', error);
  418. res.status(200).json({
  419. result: false,
  420. reason: '认证服务内部错误'
  421. });
  422. }
  423. }
  424. static async getClientAuthStats(req, res) {
  425. try {
  426. const statusStats = await clientAuth_1.ClientAuthModel.getStatusStats();
  427. const deviceTypeStats = await clientAuth_1.ClientAuthModel.getDeviceTypeStats();
  428. res.status(200).json({
  429. success: true,
  430. data: {
  431. status: statusStats,
  432. deviceType: deviceTypeStats
  433. },
  434. message: '获取客户端认证统计信息成功'
  435. });
  436. }
  437. catch (error) {
  438. console.error('获取客户端认证统计信息失败:', error);
  439. res.status(500).json({
  440. success: false,
  441. message: '获取客户端认证统计信息失败',
  442. error: error instanceof Error ? error.message : '未知错误'
  443. });
  444. }
  445. }
  446. static async getAuthMethods(req, res) {
  447. try {
  448. const methods = await clientAuth_1.ClientAuthModel.getAuthMethods();
  449. res.status(200).json({
  450. success: true,
  451. data: methods
  452. });
  453. }
  454. catch (error) {
  455. console.error('获取认证方法失败:', error);
  456. res.status(500).json({
  457. success: false,
  458. message: '获取认证方法失败',
  459. error: error instanceof Error ? error.message : '未知错误'
  460. });
  461. }
  462. }
  463. static async getAuthMethodById(req, res) {
  464. try {
  465. const id = (0, helpers_1.toString)(req.params.id);
  466. const method = await clientAuth_1.ClientAuthModel.getAuthMethodById(parseInt(id));
  467. if (!method) {
  468. res.status(404).json({
  469. success: false,
  470. message: '认证方法不存在'
  471. });
  472. return;
  473. }
  474. res.status(200).json({
  475. success: true,
  476. data: method
  477. });
  478. }
  479. catch (error) {
  480. console.error('获取认证方法失败:', error);
  481. res.status(500).json({
  482. success: false,
  483. message: '获取认证方法失败',
  484. error: error instanceof Error ? error.message : '未知错误'
  485. });
  486. }
  487. }
  488. static async createAuthMethod(req, res) {
  489. try {
  490. const { method_name, method_type, config, is_active } = req.body;
  491. if (!method_name || !method_type || !config) {
  492. res.status(400).json({
  493. success: false,
  494. message: '方法名称、类型和配置为必填项'
  495. });
  496. return;
  497. }
  498. const existingMethod = await clientAuth_1.ClientAuthModel.getAuthMethodByName(method_name);
  499. if (existingMethod) {
  500. res.status(400).json({
  501. success: false,
  502. message: '认证方法名称已存在'
  503. });
  504. return;
  505. }
  506. const authMethodData = {
  507. method_name,
  508. method_type,
  509. config: JSON.stringify(config),
  510. is_active: is_active !== undefined ? is_active : true
  511. };
  512. const newMethod = await clientAuth_1.ClientAuthModel.createAuthMethod(authMethodData);
  513. res.status(201).json({
  514. success: true,
  515. message: '认证方法创建成功',
  516. data: newMethod
  517. });
  518. }
  519. catch (error) {
  520. console.error('创建认证方法失败:', error);
  521. res.status(500).json({
  522. success: false,
  523. message: '创建认证方法失败',
  524. error: error instanceof Error ? error.message : '未知错误'
  525. });
  526. }
  527. }
  528. static async updateAuthMethod(req, res) {
  529. try {
  530. const id = (0, helpers_1.toString)(req.params.id);
  531. const updateData = req.body;
  532. if (updateData.config) {
  533. updateData.config = JSON.stringify(updateData.config);
  534. }
  535. const updatedMethod = await clientAuth_1.ClientAuthModel.updateAuthMethod(parseInt(id), updateData);
  536. if (!updatedMethod) {
  537. res.status(404).json({
  538. success: false,
  539. message: '认证方法不存在'
  540. });
  541. return;
  542. }
  543. res.status(200).json({
  544. success: true,
  545. message: '认证方法更新成功',
  546. data: updatedMethod
  547. });
  548. }
  549. catch (error) {
  550. console.error('更新认证方法失败:', error);
  551. res.status(500).json({
  552. success: false,
  553. message: '更新认证方法失败',
  554. error: error instanceof Error ? error.message : '未知错误'
  555. });
  556. }
  557. }
  558. static async deleteAuthMethod(req, res) {
  559. try {
  560. const id = (0, helpers_1.toString)(req.params.id);
  561. const success = await clientAuth_1.ClientAuthModel.deleteAuthMethod(parseInt(id));
  562. if (!success) {
  563. res.status(404).json({
  564. success: false,
  565. message: '认证方法不存在'
  566. });
  567. return;
  568. }
  569. res.status(200).json({
  570. success: true,
  571. message: '认证方法删除成功'
  572. });
  573. }
  574. catch (error) {
  575. console.error('删除认证方法失败:', error);
  576. res.status(500).json({
  577. success: false,
  578. message: '删除认证方法失败',
  579. error: error instanceof Error ? error.message : '未知错误'
  580. });
  581. }
  582. }
  583. static async getAuthPolicies(req, res) {
  584. try {
  585. const policies = await clientAuth_1.ClientAuthModel.getAuthPolicies();
  586. res.status(200).json({
  587. success: true,
  588. data: policies
  589. });
  590. }
  591. catch (error) {
  592. console.error('获取认证策略失败:', error);
  593. res.status(500).json({
  594. success: false,
  595. message: '获取认证策略失败',
  596. error: error instanceof Error ? error.message : '未知错误'
  597. });
  598. }
  599. }
  600. static async getAuthPolicyById(req, res) {
  601. try {
  602. const id = (0, helpers_1.toString)(req.params.id);
  603. const policy = await clientAuth_1.ClientAuthModel.getAuthPolicyById(parseInt(id));
  604. if (!policy) {
  605. res.status(404).json({
  606. success: false,
  607. message: '认证策略不存在'
  608. });
  609. return;
  610. }
  611. res.status(200).json({
  612. success: true,
  613. data: policy
  614. });
  615. }
  616. catch (error) {
  617. console.error('获取认证策略失败:', error);
  618. res.status(500).json({
  619. success: false,
  620. message: '获取认证策略失败',
  621. error: error instanceof Error ? error.message : '未知错误'
  622. });
  623. }
  624. }
  625. static async createAuthPolicy(req, res) {
  626. try {
  627. const { policy_name, priority, conditions, actions, is_active, description } = req.body;
  628. if (!policy_name || priority === undefined || !conditions || !actions) {
  629. res.status(400).json({
  630. success: false,
  631. message: '策略名称、优先级、条件和操作为必填项'
  632. });
  633. return;
  634. }
  635. const authPolicyData = {
  636. policy_name,
  637. priority,
  638. conditions: JSON.stringify(conditions),
  639. actions: JSON.stringify(actions),
  640. is_active: is_active !== undefined ? is_active : true,
  641. description
  642. };
  643. const newPolicy = await clientAuth_1.ClientAuthModel.createAuthPolicy(authPolicyData);
  644. res.status(201).json({
  645. success: true,
  646. message: '认证策略创建成功',
  647. data: newPolicy
  648. });
  649. }
  650. catch (error) {
  651. console.error('创建认证策略失败:', error);
  652. res.status(500).json({
  653. success: false,
  654. message: '创建认证策略失败',
  655. error: error instanceof Error ? error.message : '未知错误'
  656. });
  657. }
  658. }
  659. static async updateAuthPolicy(req, res) {
  660. try {
  661. const id = (0, helpers_1.toString)(req.params.id);
  662. const updateData = req.body;
  663. if (updateData.conditions) {
  664. updateData.conditions = JSON.stringify(updateData.conditions);
  665. }
  666. if (updateData.actions) {
  667. updateData.actions = JSON.stringify(updateData.actions);
  668. }
  669. const updatedPolicy = await clientAuth_1.ClientAuthModel.updateAuthPolicy(parseInt(id), updateData);
  670. if (!updatedPolicy) {
  671. res.status(404).json({
  672. success: false,
  673. message: '认证策略不存在'
  674. });
  675. return;
  676. }
  677. res.status(200).json({
  678. success: true,
  679. message: '认证策略更新成功',
  680. data: updatedPolicy
  681. });
  682. }
  683. catch (error) {
  684. console.error('更新认证策略失败:', error);
  685. res.status(500).json({
  686. success: false,
  687. message: '更新认证策略失败',
  688. error: error instanceof Error ? error.message : '未知错误'
  689. });
  690. }
  691. }
  692. static async deleteAuthPolicy(req, res) {
  693. try {
  694. const id = (0, helpers_1.toString)(req.params.id);
  695. const success = await clientAuth_1.ClientAuthModel.deleteAuthPolicy(parseInt(id));
  696. if (!success) {
  697. res.status(404).json({
  698. success: false,
  699. message: '认证策略不存在'
  700. });
  701. return;
  702. }
  703. res.status(200).json({
  704. success: true,
  705. message: '认证策略删除成功'
  706. });
  707. }
  708. catch (error) {
  709. console.error('删除认证策略失败:', error);
  710. res.status(500).json({
  711. success: false,
  712. message: '删除认证策略失败',
  713. error: error instanceof Error ? error.message : '未知错误'
  714. });
  715. }
  716. }
  717. static async getClientTokens(req, res) {
  718. try {
  719. const clientid = (0, helpers_1.toString)(req.params.clientid);
  720. if (!clientid) {
  721. res.status(400).json({
  722. success: false,
  723. message: '客户端ID为必填项'
  724. });
  725. return;
  726. }
  727. const tokens = await clientAuth_1.ClientAuthModel.getClientTokens(clientid);
  728. res.status(200).json({
  729. success: true,
  730. data: tokens
  731. });
  732. }
  733. catch (error) {
  734. console.error('获取客户端令牌失败:', error);
  735. res.status(500).json({
  736. success: false,
  737. message: '获取客户端令牌失败',
  738. error: error instanceof Error ? error.message : '未知错误'
  739. });
  740. }
  741. }
  742. static async createClientToken(req, res) {
  743. try {
  744. const { clientid, token_type, token_value, expires_at } = req.body;
  745. if (!clientid || !token_type || !token_value || !expires_at) {
  746. res.status(400).json({
  747. success: false,
  748. message: '客户端ID、令牌类型、令牌值和过期时间为必填项'
  749. });
  750. return;
  751. }
  752. const clientTokenData = {
  753. clientid,
  754. token_type,
  755. token_value,
  756. expires_at: new Date(expires_at),
  757. status: 'active'
  758. };
  759. const newToken = await clientAuth_1.ClientAuthModel.createClientToken(clientTokenData);
  760. await clientAuth_1.ClientAuthModel.logAuthEvent(clientid, '', 'connect', 'success', 'Client token created', req.ip, undefined, token_type, undefined);
  761. res.status(201).json({
  762. success: true,
  763. message: '客户端令牌创建成功',
  764. data: newToken
  765. });
  766. }
  767. catch (error) {
  768. console.error('创建客户端令牌失败:', error);
  769. res.status(500).json({
  770. success: false,
  771. message: '创建客户端令牌失败',
  772. error: error instanceof Error ? error.message : '未知错误'
  773. });
  774. }
  775. }
  776. static async updateClientToken(req, res) {
  777. try {
  778. const id = (0, helpers_1.toString)(req.params.id);
  779. const updateData = req.body;
  780. if (updateData.expires_at) {
  781. updateData.expires_at = new Date(updateData.expires_at);
  782. }
  783. const updatedToken = await clientAuth_1.ClientAuthModel.updateClientToken(parseInt(id), updateData);
  784. if (!updatedToken) {
  785. res.status(404).json({
  786. success: false,
  787. message: '客户端令牌不存在'
  788. });
  789. return;
  790. }
  791. await clientAuth_1.ClientAuthModel.logAuthEvent(updatedToken.clientid, '', 'connect', 'success', 'Client token updated', req.ip, undefined, updatedToken.token_type, undefined);
  792. res.status(200).json({
  793. success: true,
  794. message: '客户端令牌更新成功',
  795. data: updatedToken
  796. });
  797. }
  798. catch (error) {
  799. console.error('更新客户端令牌失败:', error);
  800. res.status(500).json({
  801. success: false,
  802. message: '更新客户端令牌失败',
  803. error: error instanceof Error ? error.message : '未知错误'
  804. });
  805. }
  806. }
  807. static async deleteClientToken(req, res) {
  808. try {
  809. const id = (0, helpers_1.toString)(req.params.id);
  810. const tokens = await clientAuth_1.ClientAuthModel.getClientTokens('');
  811. const token = tokens.find(t => t.id === parseInt(id));
  812. if (!token) {
  813. res.status(404).json({
  814. success: false,
  815. message: '客户端令牌不存在'
  816. });
  817. return;
  818. }
  819. const success = await clientAuth_1.ClientAuthModel.deleteClientToken(parseInt(id));
  820. if (!success) {
  821. res.status(404).json({
  822. success: false,
  823. message: '客户端令牌不存在'
  824. });
  825. return;
  826. }
  827. res.status(200).json({
  828. success: true,
  829. message: '客户端令牌删除成功'
  830. });
  831. }
  832. catch (error) {
  833. console.error('删除客户端令牌失败:', error);
  834. res.status(500).json({
  835. success: false,
  836. message: '删除客户端令牌失败',
  837. error: error instanceof Error ? error.message : '未知错误'
  838. });
  839. }
  840. }
  841. static async getClientCertificates(req, res) {
  842. try {
  843. const clientid = (0, helpers_1.toString)(req.params.clientid);
  844. if (!clientid) {
  845. res.status(400).json({
  846. success: false,
  847. message: '客户端ID为必填项'
  848. });
  849. return;
  850. }
  851. const certificates = await clientAuth_1.ClientAuthModel.getClientCertificates(clientid);
  852. res.status(200).json({
  853. success: true,
  854. data: certificates
  855. });
  856. }
  857. catch (error) {
  858. console.error('获取客户端证书失败:', error);
  859. res.status(500).json({
  860. success: false,
  861. message: '获取客户端证书失败',
  862. error: error instanceof Error ? error.message : '未知错误'
  863. });
  864. }
  865. }
  866. static async createClientCertificate(req, res) {
  867. try {
  868. const { clientid, certificate_pem, fingerprint, expires_at } = req.body;
  869. if (!clientid || !certificate_pem || !fingerprint || !expires_at) {
  870. res.status(400).json({
  871. success: false,
  872. message: '客户端ID、证书PEM、指纹和过期时间为必填项'
  873. });
  874. return;
  875. }
  876. const clientCertificateData = {
  877. clientid,
  878. certificate_pem,
  879. fingerprint,
  880. expires_at: new Date(expires_at),
  881. status: 'active'
  882. };
  883. const newCertificate = await clientAuth_1.ClientAuthModel.createClientCertificate(clientCertificateData);
  884. await clientAuth_1.ClientAuthModel.logAuthEvent(clientid, '', 'connect', 'success', 'Client certificate created', req.ip, undefined, 'certificate', undefined);
  885. res.status(201).json({
  886. success: true,
  887. message: '客户端证书创建成功',
  888. data: newCertificate
  889. });
  890. }
  891. catch (error) {
  892. console.error('创建客户端证书失败:', error);
  893. res.status(500).json({
  894. success: false,
  895. message: '创建客户端证书失败',
  896. error: error instanceof Error ? error.message : '未知错误'
  897. });
  898. }
  899. }
  900. static async updateClientCertificate(req, res) {
  901. try {
  902. const id = (0, helpers_1.toString)(req.params.id);
  903. const updateData = req.body;
  904. if (updateData.expires_at) {
  905. updateData.expires_at = new Date(updateData.expires_at);
  906. }
  907. const updatedCertificate = await clientAuth_1.ClientAuthModel.updateClientCertificate(parseInt(id), updateData);
  908. if (!updatedCertificate) {
  909. res.status(404).json({
  910. success: false,
  911. message: '客户端证书不存在'
  912. });
  913. return;
  914. }
  915. await clientAuth_1.ClientAuthModel.logAuthEvent(updatedCertificate.clientid, '', 'connect', 'success', 'Client certificate updated', req.ip, undefined, 'certificate', undefined);
  916. res.status(200).json({
  917. success: true,
  918. message: '客户端证书更新成功',
  919. data: updatedCertificate
  920. });
  921. }
  922. catch (error) {
  923. console.error('更新客户端证书失败:', error);
  924. res.status(500).json({
  925. success: false,
  926. message: '更新客户端证书失败',
  927. error: error instanceof Error ? error.message : '未知错误'
  928. });
  929. }
  930. }
  931. static async deleteClientCertificate(req, res) {
  932. try {
  933. const id = (0, helpers_1.toString)(req.params.id);
  934. const certificates = await clientAuth_1.ClientAuthModel.getClientCertificates('');
  935. const certificate = certificates.find(c => c.id === parseInt(id));
  936. if (!certificate) {
  937. res.status(404).json({
  938. success: false,
  939. message: '客户端证书不存在'
  940. });
  941. return;
  942. }
  943. const success = await clientAuth_1.ClientAuthModel.deleteClientCertificate(parseInt(id));
  944. if (!success) {
  945. res.status(404).json({
  946. success: false,
  947. message: '客户端证书不存在'
  948. });
  949. return;
  950. }
  951. res.status(200).json({
  952. success: true,
  953. message: '客户端证书删除成功'
  954. });
  955. }
  956. catch (error) {
  957. console.error('删除客户端证书失败:', error);
  958. res.status(500).json({
  959. success: false,
  960. message: '删除客户端证书失败',
  961. error: error instanceof Error ? error.message : '未知错误'
  962. });
  963. }
  964. }
  965. }
  966. exports.ClientAuthController = ClientAuthController;
  967. //# sourceMappingURL=clientAuthController.js.map