mirror of
https://github.com/certd/certd.git
synced 2026-07-21 10:33:36 +08:00
refactor(nginx-proxy-manager): 替换直接动态导入为运行时依赖导入
将插件中直接使用的动态导入改为通过runtimeDepsService统一处理,同时重构导入方式为runtimeImport变量,移除冗余的form-data和otplib直接导入,统一导入逻辑
This commit is contained in:
@@ -2,6 +2,7 @@ import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
|
|||||||
import { UserSettingsService } from "./user-settings-service.js";
|
import { UserSettingsService } from "./user-settings-service.js";
|
||||||
import { UserTwoFactorSetting } from "./models.js";
|
import { UserTwoFactorSetting } from "./models.js";
|
||||||
import { UserService } from "../../sys/authority/service/user-service.js";
|
import { UserService } from "../../sys/authority/service/user-service.js";
|
||||||
|
import { RuntimeDepsService } from "../../runtime-deps/runtime-deps-service.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 授权
|
* 授权
|
||||||
@@ -13,13 +14,15 @@ export class TwoFactorService {
|
|||||||
userSettingsService: UserSettingsService;
|
userSettingsService: UserSettingsService;
|
||||||
@Inject()
|
@Inject()
|
||||||
userService: UserService;
|
userService: UserService;
|
||||||
|
@Inject()
|
||||||
|
runtimeDepsService: RuntimeDepsService;
|
||||||
|
|
||||||
async getAuthenticatorQrCode(userId: any) {
|
async getAuthenticatorQrCode(userId: any) {
|
||||||
const setting = await this.getSetting(userId);
|
const setting = await this.getSetting(userId);
|
||||||
|
|
||||||
const authenticatorSetting = setting.authenticator;
|
const authenticatorSetting = setting.authenticator;
|
||||||
if (!authenticatorSetting.secret) {
|
if (!authenticatorSetting.secret) {
|
||||||
const { authenticator } = await import("otplib");
|
const { authenticator } = await this.runtimeDepsService.importRuntime("otplib");
|
||||||
|
|
||||||
authenticatorSetting.secret = authenticator.generateSecret();
|
authenticatorSetting.secret = authenticator.generateSecret();
|
||||||
await this.userSettingsService.saveSetting(userId, null, setting);
|
await this.userSettingsService.saveSetting(userId, null, setting);
|
||||||
@@ -38,7 +41,7 @@ export class TwoFactorService {
|
|||||||
|
|
||||||
async saveAuthenticator(req: { userId: any; verifyCode: any }) {
|
async saveAuthenticator(req: { userId: any; verifyCode: any }) {
|
||||||
const userId = req.userId;
|
const userId = req.userId;
|
||||||
const { authenticator } = await import("otplib");
|
const { authenticator } = await this.runtimeDepsService.importRuntime("otplib");
|
||||||
const setting = await this.getSetting(userId);
|
const setting = await this.getSetting(userId);
|
||||||
|
|
||||||
const authenticatorSetting = setting.authenticator;
|
const authenticatorSetting = setting.authenticator;
|
||||||
@@ -77,7 +80,7 @@ export class TwoFactorService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async verifyAuthenticatorCode(userId: any, verifyCode: string) {
|
async verifyAuthenticatorCode(userId: any, verifyCode: string) {
|
||||||
const { authenticator } = await import("otplib");
|
const { authenticator } = await this.runtimeDepsService.importRuntime("otplib");
|
||||||
const setting = await this.getSetting(userId);
|
const setting = await this.getSetting(userId);
|
||||||
if (!setting.authenticator.enabled) {
|
if (!setting.authenticator.enabled) {
|
||||||
throw new Error("authenticator 未开启");
|
throw new Error("authenticator 未开启");
|
||||||
|
|||||||
+9
-6
@@ -57,12 +57,12 @@ input:
|
|||||||
action: TestRequest
|
action: TestRequest
|
||||||
helper: 测试登录并拉取代理主机列表。
|
helper: 测试登录并拉取代理主机列表。
|
||||||
content: |
|
content: |
|
||||||
const { BaseAccess } = await import("@certd/pipeline");
|
const runtimeImport = import;
|
||||||
const httpsModule = await import("node:https");
|
const { BaseAccess } = await runtimeImport("@certd/pipeline");
|
||||||
const { URL } = await import("node:url");
|
const httpsModule = await runtimeImport("node:https");
|
||||||
const axiosModule = await import("axios");
|
const { URL } = await runtimeImport("node:url");
|
||||||
const formDataModule = await import("form-data");
|
const axiosModule = await runtimeImport("axios");
|
||||||
const { authenticator } = await import("otplib");
|
const formDataModule = await runtimeImport("form-data");
|
||||||
|
|
||||||
const https = httpsModule.default ?? httpsModule;
|
const https = httpsModule.default ?? httpsModule;
|
||||||
const axios = axiosModule.default ?? axiosModule;
|
const axios = axiosModule.default ?? axiosModule;
|
||||||
@@ -114,6 +114,7 @@ content: |
|
|||||||
class NginxProxyManagerClient {
|
class NginxProxyManagerClient {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
this.importFn = options.importFn;
|
||||||
this.endpoint = normalizeEndpoint(options.endpoint);
|
this.endpoint = normalizeEndpoint(options.endpoint);
|
||||||
this.apiBaseUrl = `${this.endpoint}/api`;
|
this.apiBaseUrl = `${this.endpoint}/api`;
|
||||||
this.token = undefined;
|
this.token = undefined;
|
||||||
@@ -267,6 +268,7 @@ content: |
|
|||||||
|
|
||||||
let code;
|
let code;
|
||||||
try {
|
try {
|
||||||
|
const { authenticator } = await this.importFn("otplib");
|
||||||
code = authenticator.generate(this.options.totpSecret);
|
code = authenticator.generate(this.options.totpSecret);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw describeError(error, "Generating TOTP code");
|
throw describeError(error, "Generating TOTP code");
|
||||||
@@ -327,6 +329,7 @@ content: |
|
|||||||
password: this.password,
|
password: this.password,
|
||||||
totpSecret: this.totpSecret || undefined,
|
totpSecret: this.totpSecret || undefined,
|
||||||
ignoreTls: this.ignoreTls === true,
|
ignoreTls: this.ignoreTls === true,
|
||||||
|
importFn: this.importRuntime.bind(this),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user