mirror of
https://github.com/certd/certd.git
synced 2026-07-09 14:17:37 +08:00
perf: 所有授权增加测试按钮
This commit is contained in:
@@ -7,6 +7,9 @@ import { AccessInput, BaseAccess, IsAccess } from '@certd/pipeline';
|
||||
icon: 'clarity:plugin-line',
|
||||
})
|
||||
export class CacheflyAccess extends BaseAccess {
|
||||
|
||||
|
||||
|
||||
@AccessInput({
|
||||
title: 'username',
|
||||
component: {
|
||||
@@ -32,6 +35,62 @@ export class CacheflyAccess extends BaseAccess {
|
||||
encrypt: true,
|
||||
})
|
||||
otpkey = '';
|
||||
|
||||
|
||||
@AccessInput({
|
||||
title: "测试",
|
||||
component: {
|
||||
name: "api-test",
|
||||
action: "TestRequest"
|
||||
},
|
||||
helper: "测试授权是否正确"
|
||||
})
|
||||
testRequest = true;
|
||||
|
||||
async onTestRequest() {
|
||||
await this.login();
|
||||
return "ok";
|
||||
}
|
||||
|
||||
|
||||
async login(){
|
||||
let otp = null;
|
||||
if (this.otpkey) {
|
||||
const response = await this.ctx.http.request<any, any>({
|
||||
url: `https://cn-api.my-api.cn/api/totp/?key=${this.otpkey}`,
|
||||
method: 'get',
|
||||
});
|
||||
otp = response;
|
||||
this.ctx.logger.info('获取到otp:', otp);
|
||||
}
|
||||
const loginResponse = await this.doRequestApi(`/api/2.6/auth/login`, {
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
...(otp && { otp }),
|
||||
});
|
||||
const token = loginResponse.token;
|
||||
this.ctx.logger.info('Token 获取成功');
|
||||
return token;
|
||||
}
|
||||
|
||||
async doRequestApi(url: string, data: any = null, method = 'post', token: string | null = null) {
|
||||
|
||||
const baseApi = 'https://api.cachefly.com';
|
||||
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...(token ? { 'x-cf-authorization': `Bearer ${token}` } : {}),
|
||||
};
|
||||
const res = await this.ctx.http.request<any, any>({
|
||||
url,
|
||||
baseURL: baseApi,
|
||||
method,
|
||||
data,
|
||||
headers,
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
new CacheflyAccess();
|
||||
|
||||
+7
-33
@@ -35,47 +35,21 @@ export class CacheFlyPlugin extends AbstractTaskPlugin {
|
||||
required: true,
|
||||
})
|
||||
accessId!: string;
|
||||
private readonly baseApi = 'https://api.cachefly.com';
|
||||
|
||||
|
||||
async onInstance() {}
|
||||
|
||||
private async doRequestApi(url: string, data: any = null, method = 'post', token: string | null = null) {
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...(token ? { 'x-cf-authorization': `Bearer ${token}` } : {}),
|
||||
};
|
||||
const res = await this.http.request<any, any>({
|
||||
url,
|
||||
method,
|
||||
data,
|
||||
headers,
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
async execute(): Promise<void> {
|
||||
const { cert, accessId } = this;
|
||||
const access = (await this.getAccess(accessId)) as CacheflyAccess;
|
||||
let otp = null;
|
||||
if (access.otpkey) {
|
||||
const response = await this.http.request<any, any>({
|
||||
url: `https://cn-api.my-api.cn/api/totp/?key=${access.otpkey}`,
|
||||
method: 'get',
|
||||
});
|
||||
otp = response;
|
||||
this.logger.info('获取到otp:', otp);
|
||||
}
|
||||
const loginResponse = await this.doRequestApi(`${this.baseApi}/api/2.6/auth/login`, {
|
||||
username: access.username,
|
||||
password: access.password,
|
||||
...(otp && { otp }),
|
||||
});
|
||||
const token = loginResponse.token;
|
||||
this.logger.info('Token 获取成功');
|
||||
|
||||
const token = await access.login();
|
||||
// 更新证书
|
||||
await this.doRequestApi(
|
||||
`${this.baseApi}/api/2.6/certificates`,
|
||||
await access.doRequestApi(
|
||||
`/api/2.6/certificates`,
|
||||
{
|
||||
certificate: cert.crt,
|
||||
certificateKey: cert.key,
|
||||
|
||||
Reference in New Issue
Block a user