mirror of
https://github.com/certd/certd.git
synced 2026-07-01 17:07:32 +08:00
chore: format
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { Init, Inject, MidwayWebRouterService, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from '@midwayjs/koa';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { Constants, SysPrivateSettings, SysSettingsService } from '@certd/lib-server';
|
||||
import { logger } from '@certd/basic';
|
||||
import { AuthService } from '../modules/sys/authority/service/auth-service.js';
|
||||
import { OpenKeyService } from '../modules/open/service/open-key-service.js';
|
||||
import { RoleService } from '../modules/sys/authority/service/role-service.js';
|
||||
import { Init, Inject, MidwayWebRouterService, Provide, Scope, ScopeEnum } from "@midwayjs/core";
|
||||
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from "@midwayjs/koa";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { Constants, SysPrivateSettings, SysSettingsService } from "@certd/lib-server";
|
||||
import { logger } from "@certd/basic";
|
||||
import { AuthService } from "../modules/sys/authority/service/auth-service.js";
|
||||
import { OpenKeyService } from "../modules/open/service/open-key-service.js";
|
||||
import { RoleService } from "../modules/sys/authority/service/role-service.js";
|
||||
|
||||
/**
|
||||
* 权限校验
|
||||
@@ -40,11 +40,11 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
let permission = routeInfo.description || '';
|
||||
permission = permission.trim().split(' ')[0];
|
||||
if (permission == null || permission === '') {
|
||||
let permission = routeInfo.description || "";
|
||||
permission = permission.trim().split(" ")[0];
|
||||
if (permission == null || permission === "") {
|
||||
ctx.status = 500;
|
||||
ctx.body = Constants.res.serverError('该路由未配置权限控制:' + ctx.path);
|
||||
ctx.body = Constants.res.serverError("该路由未配置权限控制:" + ctx.path);
|
||||
return;
|
||||
}
|
||||
if (permission === Constants.per.guest) {
|
||||
@@ -58,7 +58,7 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
try {
|
||||
ctx.user = jwt.verify(token, this.secret);
|
||||
} catch (err) {
|
||||
logger.error('token verify error: ', err);
|
||||
logger.error("token verify error: ", err);
|
||||
return this.notAuth(ctx);
|
||||
}
|
||||
} else {
|
||||
@@ -74,8 +74,8 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
if (permission === Constants.per.open) {
|
||||
await next();
|
||||
return;
|
||||
} else if (openKey.scope === 'open') {
|
||||
return this.notAuth(ctx, 'open key scope error,need user scope');
|
||||
} else if (openKey.scope === "open") {
|
||||
return this.notAuth(ctx, "open key scope error,need user scope");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
|
||||
const pass = await this.authService.checkPermission(ctx, permission);
|
||||
if (!pass) {
|
||||
logger.info('not permission: ', ctx.req.url);
|
||||
logger.info("not permission: ", ctx.req.url);
|
||||
ctx.status = 200;
|
||||
ctx.body = Constants.res.permission;
|
||||
return;
|
||||
@@ -104,49 +104,49 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
ctx.body = Constants.res.auth;
|
||||
if (message) {
|
||||
// @ts-ignore
|
||||
ctx.body.message =message;
|
||||
ctx.body.message = message;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private getTokenFromRequest(ctx: IMidwayKoaContext) {
|
||||
let token = ctx.get('Authorization') || '';
|
||||
token = token.replace('Bearer ', '').trim();
|
||||
let token = ctx.get("Authorization") || "";
|
||||
token = token.replace("Bearer ", "").trim();
|
||||
if (token) {
|
||||
return token;
|
||||
}
|
||||
|
||||
const cookie = ctx.headers.cookie;
|
||||
if (cookie) {
|
||||
const items = cookie.split(';');
|
||||
const items = cookie.split(";");
|
||||
for (const item of items) {
|
||||
if (!item || !item.trim()) {
|
||||
if (!item || !item.trim()) {
|
||||
continue;
|
||||
}
|
||||
const [key, value] = item.split('=');
|
||||
if (key.trim() === 'certd_token') {
|
||||
const [key, value] = item.split("=");
|
||||
if (key.trim() === "certd_token") {
|
||||
return value.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (ctx.query.token as string) || '';
|
||||
return (ctx.query.token as string) || "";
|
||||
}
|
||||
|
||||
async doOpenHandler(ctx: IMidwayKoaContext) {
|
||||
//开放接口
|
||||
const openKey = ctx.get('x-certd-token') || '';
|
||||
const openKey = ctx.get("x-certd-token") || "";
|
||||
if (!openKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//校验 openKey
|
||||
const openKeyRes = await this.openKeyService.verifyOpenKey(openKey);
|
||||
let roles = [1]
|
||||
let roles = [1];
|
||||
if (!openKeyRes.projectId || openKeyRes.projectId <= 0) {
|
||||
roles = await this.roleService.getRoleIdsByUserId(openKeyRes.userId);
|
||||
}
|
||||
ctx.user = { id: openKeyRes.userId, roles ,projectId:openKeyRes.projectId};
|
||||
ctx.user = { id: openKeyRes.userId, roles, projectId: openKeyRes.projectId };
|
||||
ctx.openKey = openKeyRes;
|
||||
return openKeyRes;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Provide } from '@midwayjs/core';
|
||||
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from '@midwayjs/koa';
|
||||
import { logger } from '@certd/basic';
|
||||
import { Provide } from "@midwayjs/core";
|
||||
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from "@midwayjs/koa";
|
||||
import { logger } from "@certd/basic";
|
||||
import { Result, TextException } from "@certd/lib-server";
|
||||
|
||||
@Provide()
|
||||
@@ -9,20 +9,20 @@ export class GlobalExceptionMiddleware implements IWebMiddleware {
|
||||
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
|
||||
const { url } = ctx;
|
||||
const startTime = Date.now();
|
||||
logger.info('请求开始:', url);
|
||||
logger.info("请求开始:", url);
|
||||
try {
|
||||
await next();
|
||||
logger.info('请求完成:', url, Date.now() - startTime + 'ms');
|
||||
logger.info("请求完成:", url, Date.now() - startTime + "ms");
|
||||
} catch (err) {
|
||||
if(err instanceof TextException){
|
||||
delete err.stack
|
||||
if (err instanceof TextException) {
|
||||
delete err.stack;
|
||||
}
|
||||
logger.error('请求异常:', url, Date.now() - startTime + 'ms', err);
|
||||
logger.error("请求异常:", url, Date.now() - startTime + "ms", err);
|
||||
ctx.status = 200;
|
||||
if (err.code == null || typeof err.code !== 'number') {
|
||||
if (err.code == null || typeof err.code !== "number") {
|
||||
err.code = 1;
|
||||
}
|
||||
ctx.body = Result.error(err.code, err.message,err.data);
|
||||
ctx.body = Result.error(err.code, err.message, err.data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import {Inject, Provide} from '@midwayjs/core';
|
||||
import {IMidwayKoaContext, IWebMiddleware, NextFunction} from '@midwayjs/koa';
|
||||
import {hiddenStatus, SafeService} from "../modules/sys/settings/safe-service.js";
|
||||
import {SiteOffException} from "@certd/lib-server";
|
||||
|
||||
import { Inject, Provide } from "@midwayjs/core";
|
||||
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from "@midwayjs/koa";
|
||||
import { hiddenStatus, SafeService } from "../modules/sys/settings/safe-service.js";
|
||||
import { SiteOffException } from "@certd/lib-server";
|
||||
|
||||
/**
|
||||
* 隐藏环境
|
||||
@@ -14,41 +13,40 @@ export class HiddenMiddleware implements IWebMiddleware {
|
||||
|
||||
resolve() {
|
||||
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
|
||||
|
||||
async function pass() {
|
||||
hiddenStatus.updateRequestTime()
|
||||
hiddenStatus.updateRequestTime();
|
||||
await next();
|
||||
}
|
||||
|
||||
const hiddenSetting = await this.hiddenService.getHiddenSetting();
|
||||
if (!hiddenSetting || !hiddenSetting?.enabled) {
|
||||
//未开启站点隐藏,直接通过
|
||||
return await pass()
|
||||
return await pass();
|
||||
}
|
||||
|
||||
const req = ctx.request;
|
||||
if (hiddenSetting.hiddenOpenApi === false && req.url.startsWith(`/api/v1/`) ) {
|
||||
if (hiddenSetting.hiddenOpenApi === false && req.url.startsWith(`/api/v1/`)) {
|
||||
//不隐藏开放接口
|
||||
await next();
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
//判断当前是否是隐藏状态
|
||||
if (!hiddenStatus.isHidden) {
|
||||
return await pass()
|
||||
return await pass();
|
||||
}
|
||||
|
||||
//判断是否有解锁文件,如果有就返回true并删除文件
|
||||
if (hiddenStatus.hasUnHiddenFile()) {
|
||||
//临时修改为未隐藏
|
||||
hiddenStatus.isHidden = false;
|
||||
return await pass()
|
||||
return await pass();
|
||||
}
|
||||
|
||||
if (req.url === `/api/unhidden/${hiddenSetting.openPath}`) {
|
||||
return await pass();
|
||||
}
|
||||
throw new SiteOffException('此站点已关闭');
|
||||
}
|
||||
throw new SiteOffException("此站点已关闭");
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { Config, Provide } from '@midwayjs/core';
|
||||
import { IMidwayKoaContext, NextFunction, IWebMiddleware } from '@midwayjs/koa';
|
||||
import { PreviewException } from '@certd/lib-server';
|
||||
import { Config, Provide } from "@midwayjs/core";
|
||||
import { IMidwayKoaContext, NextFunction, IWebMiddleware } from "@midwayjs/koa";
|
||||
import { PreviewException } from "@certd/lib-server";
|
||||
|
||||
/**
|
||||
* 预览模式
|
||||
*/
|
||||
@Provide()
|
||||
export class PreviewMiddleware implements IWebMiddleware {
|
||||
@Config('preview.enabled')
|
||||
@Config("preview.enabled")
|
||||
private preview: boolean;
|
||||
|
||||
resolve() {
|
||||
@@ -24,16 +24,16 @@ export class PreviewMiddleware implements IWebMiddleware {
|
||||
if (id == null && roleId != null) {
|
||||
id = roleId;
|
||||
}
|
||||
if (id != null && typeof id === 'string') {
|
||||
if (id != null && typeof id === "string") {
|
||||
id = parseInt(id);
|
||||
}
|
||||
if (url.indexOf('?') !== -1) {
|
||||
url = url.substring(0, url.indexOf('?'));
|
||||
if (url.indexOf("?") !== -1) {
|
||||
url = url.substring(0, url.indexOf("?"));
|
||||
}
|
||||
const isModify = url.endsWith('update') || url.endsWith('delete') || url.endsWith('authz');
|
||||
const isModify = url.endsWith("update") || url.endsWith("delete") || url.endsWith("authz");
|
||||
const isPreviewId = id < 1000;
|
||||
if (this.preview && isModify && isPreviewId) {
|
||||
throw new PreviewException('对不起,预览环境不允许修改此数据,如需体验请添加新数据');
|
||||
throw new PreviewException("对不起,预览环境不允许修改此数据,如需体验请添加新数据");
|
||||
}
|
||||
await next();
|
||||
return;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { CommonException, SysSettingsService } from "@certd/lib-server";
|
||||
import { Autoload, Config, Init, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from '@midwayjs/koa';
|
||||
import { Autoload, Config, Init, Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
|
||||
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from "@midwayjs/koa";
|
||||
import { UserSettingsService } from "../../modules/mine/service/user-settings-service.js";
|
||||
import { UserService } from '../../modules/sys/authority/service/user-service.js';
|
||||
import { UserService } from "../../modules/sys/authority/service/user-service.js";
|
||||
|
||||
/**
|
||||
* 重置密码模式
|
||||
@@ -19,19 +19,17 @@ export class ResetPasswdMiddleware implements IWebMiddleware {
|
||||
@Inject()
|
||||
sysSettingsService: SysSettingsService;
|
||||
|
||||
@Config('system.resetAdminPasswd')
|
||||
@Config("system.resetAdminPasswd")
|
||||
private resetAdminPasswd: boolean;
|
||||
resolve() {
|
||||
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
|
||||
if (this.resetAdminPasswd === true) {
|
||||
throw new CommonException('1号管理员密码已修改为123456,当前为重置密码模式,无法响应请求,请关闭重置密码模式恢复正常服务');
|
||||
throw new CommonException("1号管理员密码已修改为123456,当前为重置密码模式,无法响应请求,请关闭重置密码模式恢复正常服务");
|
||||
}
|
||||
await next();
|
||||
};
|
||||
}
|
||||
|
||||
@Init()
|
||||
async init() {
|
||||
|
||||
}
|
||||
async init() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user