chore: oidc first

This commit is contained in:
xiaojunnuo
2025-11-26 07:34:42 +08:00
parent b4c362da37
commit 5a148aa3b9
12 changed files with 107 additions and 1586 deletions
@@ -1,58 +0,0 @@
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
import { BaseController, CommonException, Constants, SysSettingsService } from "@certd/lib-server";
import { CodeService } from '../../../modules/basic/service/code-service.js';
import { UserService } from '../../../modules/sys/authority/service/user-service.js';
import { LoginService } from "../../../modules/login/service/login-service.js";
/**
*/
@Provide()
@Controller('/api')
export class LoginController extends BaseController {
@Inject()
loginService: LoginService;
@Inject()
userService: UserService;
@Inject()
codeService: CodeService;
@Inject()
sysSettingsService: SysSettingsService;
@Post('/forgotPassword', { summary: Constants.per.guest })
public async forgotPassword(
@Body(ALL)
body: any,
) {
const sysSettings = await this.sysSettingsService.getPublicSettings();
if(!sysSettings.selfServicePasswordRetrievalEnabled) {
throw new CommonException('暂未开启自助找回');
}
// 找回密码的验证码允许错误次数
const maxErrorCount = 5;
if(body.type === 'email') {
this.codeService.checkEmailCode({
verificationType: 'forgotPassword',
email: body.input,
validateCode: body.validateCode,
maxErrorCount: maxErrorCount,
throwError: true,
});
} else if(body.type === 'mobile') {
await this.codeService.checkSmsCode({
verificationType: 'forgotPassword',
mobile: body.input,
phoneCode: body.phoneCode,
smsCode: body.validateCode,
maxErrorCount: maxErrorCount,
throwError: true,
});
} else {
throw new CommonException('暂不支持的找回类型,请联系管理员找回');
}
const username = await this.userService.forgotPassword(body);
username && this.loginService.clearCacheOnSuccess(username)
return this.ok();
}
}
@@ -1,91 +0,0 @@
import { ALL, Body, Controller, Inject, Post, Provide } from "@midwayjs/core";
import { LoginService } from "../../../modules/login/service/login-service.js";
import { AddonService, BaseController, Constants, SysPublicSettings, SysSettingsService } from "@certd/lib-server";
import { CodeService } from "../../../modules/basic/service/code-service.js";
import { checkComm } from "@certd/plus-core";
import { CaptchaService } from "../../../modules/basic/service/captcha-service.js";
/**
*/
@Provide()
@Controller('/api/')
export class LoginController extends BaseController {
@Inject()
loginService: LoginService;
@Inject()
codeService: CodeService;
@Inject()
sysSettingsService: SysSettingsService;
@Inject()
addonService: AddonService;
@Inject()
captchaService: CaptchaService;
@Post('/login', { summary: Constants.per.guest })
public async login(
@Body(ALL)
body: any
) {
const settings = await this.sysSettingsService.getPublicSettings()
if (settings.captchaEnabled === true) {
await this.captchaService.doValidate({form:body.captcha,must:false,captchaAddonId:settings.captchaAddonId})
}
const token = await this.loginService.loginByPassword(body);
this.writeTokenCookie(token);
return this.ok(token);
}
private writeTokenCookie(token: { expire: any; token: any }) {
this.ctx.cookies.set("certd_token", token.token, {
maxAge: 1000 * token.expire
});
}
@Post('/loginBySms', { summary: Constants.per.guest })
public async loginBySms(
@Body(ALL)
body: any
) {
const settings = await this.sysSettingsService.getSetting<SysPublicSettings>(SysPublicSettings);
if (settings.smsLoginEnabled !== true) {
throw new Error('当前站点禁止短信验证码登录');
}
checkComm();
const token = await this.loginService.loginBySmsCode({
phoneCode: body.phoneCode,
mobile: body.mobile,
smsCode: body.smsCode,
randomStr: body.randomStr,
});
this.writeTokenCookie(token);
return this.ok(token);
}
@Post('/loginByTwoFactor', { summary: Constants.per.guest })
public async loginByTwoFactor(
@Body(ALL)
body: any
) {
const token = await this.loginService.loginByTwoFactor({
loginId: body.loginId,
verifyCode: body.verifyCode,
});
this.writeTokenCookie(token);
return this.ok(token);
}
@Post('/logout', { summary: Constants.per.authOnly })
public logout() {
this.ctx.cookies.set("certd_token", "", {
maxAge: 0
});
return this.ok();
}
}
@@ -1,97 +0,0 @@
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
import { BaseController, Constants, SysSettingsService } from '@certd/lib-server';
import { RegisterType, UserService } from '../../../modules/sys/authority/service/user-service.js';
import { CodeService } from '../../../modules/basic/service/code-service.js';
import { checkComm, checkPlus } from '@certd/plus-core';
export type RegisterReq = {
type: RegisterType;
username: string;
password: string;
mobile: string;
email: string;
phoneCode?: string;
validateCode: string;
captcha:any;
};
/**
*/
@Provide()
@Controller('/api/')
export class RegisterController extends BaseController {
@Inject()
userService: UserService;
@Inject()
codeService: CodeService;
@Inject()
sysSettingsService: SysSettingsService;
@Post('/register', { summary: Constants.per.guest })
public async register(
@Body(ALL)
body: RegisterReq
) {
const sysPublicSettings = await this.sysSettingsService.getPublicSettings();
if (sysPublicSettings.registerEnabled === false) {
throw new Error('当前站点已禁止自助注册功能');
}
if (body.username && ["admin","certd"].includes(body.username) ) {
throw new Error('用户名不能为保留字');
}
if (body.type === 'username') {
if (sysPublicSettings.usernameRegisterEnabled === false) {
throw new Error('当前站点已禁止用户名注册功能');
}
if (!body.username) {
throw new Error('用户名不能为空');
}
await this.codeService.checkCaptcha(body.captcha);
const newUser = await this.userService.register(body.type, {
username: body.username,
password: body.password,
} as any);
return this.ok(newUser);
} else if (body.type === 'mobile') {
if (sysPublicSettings.mobileRegisterEnabled === false) {
throw new Error('当前站点已禁止手机号注册功能');
}
checkComm();
//验证短信验证码
await this.codeService.checkSmsCode({
mobile: body.mobile,
phoneCode: body.phoneCode,
smsCode: body.validateCode,
throwError: true,
});
const newUser = await this.userService.register(body.type, {
username: body.username,
phoneCode: body.phoneCode,
mobile: body.mobile,
password: body.password,
} as any);
return this.ok(newUser);
} else if (body.type === 'email') {
if (sysPublicSettings.emailRegisterEnabled === false) {
throw new Error('当前站点已禁止Email注册功能');
}
checkPlus();
this.codeService.checkEmailCode({
email: body.email,
validateCode: body.validateCode,
throwError: true,
});
const newUser = await this.userService.register(body.type, {
username: body.username,
email: body.email,
password: body.password,
} as any);
return this.ok(newUser);
}
}
}