mirror of
https://github.com/certd/certd.git
synced 2026-04-24 04:17:25 +08:00
chore:
This commit is contained in:
@@ -6,7 +6,7 @@ export class BaseOpenController extends BaseController {
|
||||
const openKey: OpenKey = this.ctx.openKey;
|
||||
if (openKey.encrypt) {
|
||||
const data = JSON.stringify(res);
|
||||
const encryptor = new Encryptor(openKey.keySecret);
|
||||
const encryptor = new Encryptor(openKey.keySecret, 'hex');
|
||||
const encrypted = encryptor.encrypt(data);
|
||||
return this.ok(encrypted);
|
||||
}
|
||||
|
||||
@@ -37,7 +37,8 @@ export class OpenKeyController extends CrudController<OpenKeyService> {
|
||||
}
|
||||
|
||||
@Post('/add', { summary: Constants.per.authOnly })
|
||||
async add(@Body(ALL) bean: any) {
|
||||
async add() {
|
||||
const bean: any = {};
|
||||
bean.userId = this.getUserId();
|
||||
const res = await this.service.add(bean);
|
||||
return this.ok(res);
|
||||
|
||||
@@ -27,30 +27,20 @@ export class OpenKeyService extends BaseService<OpenKeyEntity> {
|
||||
return await super.page(pageReq);
|
||||
}
|
||||
|
||||
async getKey(userId: number) {
|
||||
let entity = await this.getByUserId(userId);
|
||||
if (entity) {
|
||||
return {
|
||||
keyId: entity.keyId,
|
||||
keySecret: entity.keySecret,
|
||||
};
|
||||
}
|
||||
const keyId = utils.id.simpleNanoId(12) + '_key';
|
||||
async add(bean: OpenKeyEntity) {
|
||||
return await this.generate(bean.userId);
|
||||
}
|
||||
|
||||
async generate(userId: number) {
|
||||
const keyId = utils.id.simpleNanoId(18) + '_key';
|
||||
const secretKey = crypto.randomBytes(32);
|
||||
const keySecret = secretKey.toString('base64');
|
||||
entity = new OpenKeyEntity();
|
||||
const keySecret = Buffer.from(secretKey).toString('hex');
|
||||
const entity = new OpenKeyEntity();
|
||||
entity.userId = userId;
|
||||
entity.keyId = keyId;
|
||||
entity.keySecret = keySecret;
|
||||
await this.repository.save(entity);
|
||||
return {
|
||||
keyId: entity.keyId,
|
||||
keySecret: entity.keySecret,
|
||||
};
|
||||
}
|
||||
|
||||
private getByUserId(userId: number) {
|
||||
return this.repository.findOne({ where: { userId } });
|
||||
return entity;
|
||||
}
|
||||
|
||||
async getByKeyId(keyId: string) {
|
||||
|
||||
Reference in New Issue
Block a user