Files
certd/packages/ui/certd-server/src/controller/user/mine/setting-two-factor-controller.ts

78 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-04-17 00:06:49 +08:00
import { ALL, Body, Controller, Inject, Post, Provide } from "@midwayjs/core";
import { BaseController, Constants } from "@certd/lib-server";
import { UserSettingsService } from "../../../modules/mine/service/user-settings-service.js";
import { UserTwoFactorSetting } from "../../../modules/mine/service/models.js";
import { merge } from "lodash-es";
import { TwoFactorService } from "../../../modules/mine/service/two-factor-service.js";
2025-04-17 13:41:08 +08:00
import {isPlus} from "@certd/plus-core";
2026-03-15 14:01:34 +08:00
import { ApiTags } from "@midwayjs/swagger";
2025-04-17 00:06:49 +08:00
/**
*/
@Provide()
@Controller("/api/user/settings/twoFactor")
2026-03-15 14:01:34 +08:00
@ApiTags(['mine'])
2025-04-17 00:06:49 +08:00
export class UserTwoFactorSettingController extends BaseController {
@Inject()
service: UserSettingsService;
@Inject()
twoFactorService: TwoFactorService;
@Post("/get", { description: Constants.per.authOnly, summary: "获取双因子认证设置" })
2025-04-17 00:06:49 +08:00
async get() {
const userId = this.getUserId();
2026-02-13 21:28:17 +08:00
const setting = await this.service.getSetting<UserTwoFactorSetting>(userId,null, UserTwoFactorSetting);
2025-04-17 00:06:49 +08:00
return this.ok(setting);
}
@Post("/save", { description: Constants.per.authOnly, summary: "保存双因子认证设置" })
2025-04-17 00:06:49 +08:00
async save(@Body(ALL) bean: any) {
2025-04-17 13:41:08 +08:00
if (!isPlus()) {
throw new Error('本功能需要开通专业版')
}
2025-04-17 00:06:49 +08:00
const userId = this.getUserId();
const setting = new UserTwoFactorSetting();
merge(setting, bean);
// 禁用时清除
if(!setting.authenticator.enabled){
setting.authenticator.secret = null;
setting.authenticator.verified = false;
}
2026-02-13 21:28:17 +08:00
await this.service.saveSetting(userId,null, setting);
2025-04-17 00:06:49 +08:00
return this.ok({});
}
@Post("/authenticator/qrcode", { description: Constants.per.authOnly, summary: "获取验证器二维码" })
2025-04-17 00:06:49 +08:00
async authenticatorQrcode() {
const userId = this.getUserId();
2026-02-06 23:26:57 +08:00
const {qrcode,link,secret} = await this.twoFactorService.getAuthenticatorQrCode(userId);
return this.ok({qrcode,link,secret});
2025-04-17 00:06:49 +08:00
}
@Post("/authenticator/save", { description: Constants.per.authOnly, summary: "保存验证器设置" })
2025-04-17 00:06:49 +08:00
async authenticatorSave(@Body(ALL) bean: any) {
2025-04-17 13:41:08 +08:00
if (!isPlus()) {
throw new Error('本功能需要开通专业版')
}
2025-04-17 00:06:49 +08:00
const userId = this.getUserId();
await this.twoFactorService.saveAuthenticator({
userId,
verifyCode: bean.verifyCode,
});
return this.ok();
}
@Post("/authenticator/off", { description: Constants.per.authOnly, summary: "关闭验证器" })
2025-04-17 01:15:55 +08:00
async authenticatorOff() {
const userId = this.getUserId();
await this.twoFactorService.offAuthenticator(userId);
return this.ok();
}
2025-04-17 00:06:49 +08:00
}