mirror of
https://github.com/certd/certd.git
synced 2026-04-24 04:17:25 +08:00
feat: 手机号登录、邮箱验证码注册
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { IsAccess, AccessInput, BaseAccess } from "@certd/pipeline";
|
||||
|
||||
@IsAccess({
|
||||
name: "aliyun",
|
||||
title: "阿里云授权",
|
||||
desc: "",
|
||||
})
|
||||
export class AliyunAccess extends BaseAccess {
|
||||
@AccessInput({
|
||||
title: "accessKeyId",
|
||||
component: {
|
||||
placeholder: "accessKeyId",
|
||||
},
|
||||
helper: "登录阿里云控制台->AccessKey管理页面获取。",
|
||||
required: true,
|
||||
})
|
||||
accessKeyId = "";
|
||||
@AccessInput({
|
||||
title: "accessKeySecret",
|
||||
component: {
|
||||
placeholder: "accessKeySecret",
|
||||
},
|
||||
required: true,
|
||||
encrypt: true,
|
||||
helper: "注意:证书申请需要dns解析权限;其他阿里云插件,需要对应的权限,比如证书上传需要证书管理权限;嫌麻烦就用主账号的全量权限的accessKey",
|
||||
})
|
||||
accessKeySecret = "";
|
||||
}
|
||||
|
||||
new AliyunAccess();
|
||||
@@ -0,0 +1 @@
|
||||
export * from './aliyun-access.js';
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from "./lib/index.js";
|
||||
export * from "./access/index.js";
|
||||
@@ -0,0 +1,78 @@
|
||||
import { getGlobalAgents, ILogger } from "@certd/basic";
|
||||
|
||||
export class AliyunClient {
|
||||
client: any;
|
||||
logger: ILogger;
|
||||
agent: any;
|
||||
useROAClient: boolean;
|
||||
|
||||
constructor(opts: { logger: ILogger; useROAClient?: boolean }) {
|
||||
this.logger = opts.logger;
|
||||
this.useROAClient = opts.useROAClient || false;
|
||||
const agents = getGlobalAgents();
|
||||
this.agent = agents.httpsAgent;
|
||||
}
|
||||
|
||||
async getSdk() {
|
||||
if (this.useROAClient) {
|
||||
return await this.getROAClient();
|
||||
}
|
||||
const Core = await import("@alicloud/pop-core");
|
||||
return Core.default;
|
||||
}
|
||||
|
||||
async getROAClient() {
|
||||
const Core = await import("@alicloud/pop-core");
|
||||
console.log("aliyun sdk", Core);
|
||||
// @ts-ignore
|
||||
return Core.ROAClient;
|
||||
}
|
||||
|
||||
async init(opts: any) {
|
||||
const Core = await this.getSdk();
|
||||
this.client = new Core(opts);
|
||||
return this.client;
|
||||
}
|
||||
|
||||
checkRet(ret: any) {
|
||||
if (ret.code != null) {
|
||||
throw new Error("执行失败:" + ret.Message);
|
||||
}
|
||||
}
|
||||
|
||||
async request(
|
||||
name: string,
|
||||
params: any,
|
||||
requestOption: any = {
|
||||
method: "POST",
|
||||
formatParams: false,
|
||||
}
|
||||
) {
|
||||
if (!this.useROAClient) {
|
||||
requestOption.agent = this.agent;
|
||||
}
|
||||
|
||||
const getNumberFromEnv = (key: string, defValue: number) => {
|
||||
const value = process.env[key];
|
||||
if (value) {
|
||||
try {
|
||||
return parseInt(value);
|
||||
} catch (e: any) {
|
||||
this.logger.error(`环境变量${key}设置错误,应该是一个数字,当前值为${value},将使用默认值:${defValue}`);
|
||||
return defValue;
|
||||
}
|
||||
} else {
|
||||
return defValue;
|
||||
}
|
||||
};
|
||||
|
||||
// 连接超时设置,仅对当前请求有效。
|
||||
requestOption.connectTimeout = getNumberFromEnv("ALIYUN_CLIENT_CONNECT_TIMEOUT", 8000);
|
||||
// 读超时设置,仅对当前请求有效。
|
||||
requestOption.readTimeout = getNumberFromEnv("ALIYUN_CLIENT_READ_TIMEOUT", 8000);
|
||||
|
||||
const res = await this.client.request(name, params, requestOption);
|
||||
this.checkRet(res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from "./base-client.js";
|
||||
export * from "./ssl-client.js";
|
||||
@@ -0,0 +1,130 @@
|
||||
import { ILogger } from "@certd/basic";
|
||||
import { AliyunAccess } from "../access/index.js";
|
||||
import { AliyunClient } from "./index.js";
|
||||
import { CertInfo } from "@certd/plugin-cert";
|
||||
|
||||
export type AliyunSslClientOpts = {
|
||||
access: AliyunAccess;
|
||||
logger: ILogger;
|
||||
endpoint: string;
|
||||
};
|
||||
|
||||
export type AliyunSslGetResourceListReq = {
|
||||
cloudProduct: string;
|
||||
};
|
||||
|
||||
export type AliyunSslCreateDeploymentJobReq = {
|
||||
name: string;
|
||||
jobType: string;
|
||||
contactIds: string[];
|
||||
resourceIds: string[];
|
||||
certIds: string[];
|
||||
};
|
||||
|
||||
export type AliyunSslUploadCertReq = {
|
||||
name: string;
|
||||
cert: CertInfo;
|
||||
};
|
||||
|
||||
export class AliyunSslClient {
|
||||
opts: AliyunSslClientOpts;
|
||||
constructor(opts: AliyunSslClientOpts) {
|
||||
this.opts = opts;
|
||||
}
|
||||
|
||||
checkRet(ret: any) {
|
||||
if (ret.code != null) {
|
||||
throw new Error("执行失败:" + ret.Message);
|
||||
}
|
||||
}
|
||||
|
||||
async getClient() {
|
||||
const access = this.opts.access;
|
||||
const client = new AliyunClient({ logger: this.opts.logger });
|
||||
await client.init({
|
||||
accessKeyId: access.accessKeyId,
|
||||
accessKeySecret: access.accessKeySecret,
|
||||
endpoint: `https://${this.opts.endpoint || "cas.aliyuncs.com"}`,
|
||||
apiVersion: "2020-04-07",
|
||||
});
|
||||
return client;
|
||||
}
|
||||
|
||||
async uploadCert(req: AliyunSslUploadCertReq) {
|
||||
const client = await this.getClient();
|
||||
const params = {
|
||||
Name: req.name,
|
||||
Cert: req.cert.crt,
|
||||
Key: req.cert.key,
|
||||
};
|
||||
|
||||
const requestOption = {
|
||||
method: "POST",
|
||||
};
|
||||
|
||||
this.opts.logger.info("开始上传证书");
|
||||
const ret: any = await client.request("UploadUserCertificate", params, requestOption);
|
||||
this.checkRet(ret);
|
||||
this.opts.logger.info("证书上传成功:aliyunCertId=", ret.CertId);
|
||||
//output
|
||||
return ret.CertId;
|
||||
}
|
||||
|
||||
async getResourceList(req: AliyunSslGetResourceListReq) {
|
||||
const client = await this.getClient();
|
||||
const params = {
|
||||
CloudName: "aliyun",
|
||||
CloudProduct: req.cloudProduct,
|
||||
};
|
||||
|
||||
const requestOption = {
|
||||
method: "POST",
|
||||
formatParams: false,
|
||||
};
|
||||
|
||||
const res = await client.request("ListCloudResources", params, requestOption);
|
||||
this.checkRet(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
async createDeploymentJob(req: AliyunSslCreateDeploymentJobReq) {
|
||||
const client = await this.getClient();
|
||||
|
||||
const params = {
|
||||
Name: req.name,
|
||||
JobType: req.jobType,
|
||||
ContactIds: req.contactIds.join(","),
|
||||
ResourceIds: req.resourceIds.join(","),
|
||||
CertIds: req.certIds.join(","),
|
||||
};
|
||||
|
||||
const requestOption = {
|
||||
method: "POST",
|
||||
formatParams: false,
|
||||
};
|
||||
|
||||
const res = await client.request("CreateDeploymentJob", params, requestOption);
|
||||
this.checkRet(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
async getContactList() {
|
||||
const params = {};
|
||||
|
||||
const requestOption = {
|
||||
method: "POST",
|
||||
formatParams: false,
|
||||
};
|
||||
const client = await this.getClient();
|
||||
const res = await client.request("ListContact", params, requestOption);
|
||||
this.checkRet(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
async doRequest(action: string, params: any, requestOption: any) {
|
||||
const client = await this.getClient();
|
||||
const res = await client.request(action, params, requestOption);
|
||||
this.checkRet(res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user