Files
certd/packages/ui/certd-server/src/controller/user/open/open-key-controller.ts
T

110 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-12-22 14:00:46 +08:00
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { Constants, CrudController } from '@certd/lib-server';
2025-01-15 01:05:34 +08:00
import { AuthService } from '../../../modules/sys/authority/service/auth-service.js';
import { OpenKeyService } from '../../../modules/open/service/open-key-service.js';
2026-03-15 14:01:34 +08:00
import { ApiTags } from '@midwayjs/swagger';
2024-12-22 14:00:46 +08:00
/**
*/
@Provide()
2025-01-15 01:05:34 +08:00
@Controller('/api/open/key')
2026-03-15 14:01:34 +08:00
@ApiTags(['open'])
2025-01-15 01:05:34 +08:00
export class OpenKeyController extends CrudController<OpenKeyService> {
2024-12-22 14:00:46 +08:00
@Inject()
2025-01-15 01:05:34 +08:00
service: OpenKeyService;
2024-12-22 14:00:46 +08:00
@Inject()
authService: AuthService;
2025-01-15 01:05:34 +08:00
getService(): OpenKeyService {
2024-12-22 14:00:46 +08:00
return this.service;
}
2026-03-15 18:26:49 +08:00
@Post('/page', { description: Constants.per.authOnly, summary: "查询开放API密钥分页列表" })
2024-12-22 14:00:46 +08:00
async page(@Body(ALL) body: any) {
2026-02-13 21:28:17 +08:00
const {projectId,userId} = await this.getProjectUserIdRead();
2024-12-22 14:00:46 +08:00
body.query = body.query ?? {};
2026-02-13 21:28:17 +08:00
body.query.projectId = projectId;
body.query.userId = userId;
2024-12-22 14:00:46 +08:00
const res = await this.service.page({
query: body.query,
page: body.page,
sort: body.sort,
});
2026-03-15 18:26:49 +08:00
this.keySecretDesensitization(res.records);
2024-12-22 14:00:46 +08:00
return this.ok(res);
}
2026-03-15 18:26:49 +08:00
private keySecretDesensitization(list: any[]) {
for (const item of list) {
item.keySecret = item.keySecret?.substring(0, 4) + '*********************************' + item.keySecret?.substring(item.keySecret.length - 4);
}
}
@Post('/list', { description: Constants.per.authOnly, summary: "查询开放API密钥列表" })
2024-12-22 14:00:46 +08:00
async list(@Body(ALL) body: any) {
2026-02-13 21:28:17 +08:00
const {projectId,userId} = await this.getProjectUserIdRead();
2024-12-22 14:00:46 +08:00
body.query = body.query ?? {};
2026-02-13 21:28:17 +08:00
body.query.projectId = projectId;
body.query.userId = userId;
2026-03-15 18:26:49 +08:00
const res = await this.service.list(body);
this.keySecretDesensitization(res);
return this.ok(res);
2024-12-22 14:00:46 +08:00
}
2026-03-15 18:26:49 +08:00
@Post('/add', { description: Constants.per.authOnly, summary: "添加开放API密钥" })
2025-01-19 00:33:34 +08:00
async add(@Body(ALL) body: any) {
2026-02-13 21:28:17 +08:00
const {projectId,userId} = await this.getProjectUserIdRead();
body.projectId = projectId;
body.userId = userId;
2025-01-19 00:33:34 +08:00
const res = await this.service.add(body);
2024-12-24 01:12:12 +08:00
return this.ok(res);
2024-12-22 14:00:46 +08:00
}
2026-03-15 18:26:49 +08:00
@Post('/update', { description: Constants.per.authOnly, summary: "更新开放API密钥" })
2024-12-22 14:00:46 +08:00
async update(@Body(ALL) bean) {
2026-02-13 21:28:17 +08:00
await this.checkOwner(this.getService(), bean.id, "write");
2024-12-22 14:00:46 +08:00
delete bean.userId;
2026-02-13 21:28:17 +08:00
delete bean.projectId;
2024-12-24 01:12:12 +08:00
await this.service.update(bean);
return this.ok();
2024-12-22 14:00:46 +08:00
}
2026-03-15 18:26:49 +08:00
@Post('/info', { description: Constants.per.authOnly, summary: "查询开放API密钥详情" })
2024-12-22 14:00:46 +08:00
async info(@Query('id') id: number) {
2026-03-15 18:26:49 +08:00
const info = await this.checkPermission(id);
return this.ok(info);
}
private async checkPermission(id: number) {
const info = await this.service.info(id);
if (!info) {
throw new Error('密钥不存在');
}
if (info.scope === 'user'){
await this.checkOwner(this.getService(), id, "write");;
}else{
await this.checkOwner(this.getService(), id, "read");
}
return info
2024-12-22 14:00:46 +08:00
}
2026-03-15 18:26:49 +08:00
2024-12-22 14:00:46 +08:00
2026-03-15 18:26:49 +08:00
@Post('/delete', { description: Constants.per.authOnly, summary: "删除开放API密钥" })
2024-12-22 14:00:46 +08:00
async delete(@Query('id') id: number) {
2026-02-13 21:28:17 +08:00
await this.checkOwner(this.getService(), id, "write");
2024-12-22 14:00:46 +08:00
return await super.delete(id);
}
2025-01-15 22:58:11 +08:00
2026-03-15 18:26:49 +08:00
@Post('/getApiToken', { description: Constants.per.authOnly, summary: "获取API测试令牌" })
2025-01-19 01:21:58 +08:00
async getApiToken(@Body('id') id: number) {
2026-03-15 18:26:49 +08:00
await this.checkPermission(id);
2025-01-15 22:58:11 +08:00
const token = await this.service.getApiToken(id);
return this.ok(token);
}
2026-03-15 18:26:49 +08:00
@Post('/getSecret', { description: Constants.per.authOnly, summary: "获取密钥" })
async getSecret(@Body('id') id: number) {
const info = await this.checkPermission(id);
return this.ok(info.keySecret);
}
2024-12-22 14:00:46 +08:00
}