mirror of
https://github.com/certd/certd.git
synced 2026-05-15 04:27:31 +08:00
chore: sdk
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
const crypto = require("crypto");
|
||||
|
||||
class CertdClient {
|
||||
constructor(keyId, keySecret, options = {}) {
|
||||
if (!keyId) {
|
||||
throw new Error("keyId is required");
|
||||
}
|
||||
if (!keySecret) {
|
||||
throw new Error("keySecret is required");
|
||||
}
|
||||
this.keyId = keyId;
|
||||
this.keySecret = keySecret;
|
||||
this.baseUrl = (options.baseUrl || "http://127.0.0.1:7001").replace(/\/$/, "");
|
||||
this.encrypt = options.encrypt === true;
|
||||
this.signType = options.signType || "md5";
|
||||
}
|
||||
|
||||
getSign(content) {
|
||||
if (this.signType !== "md5") {
|
||||
throw new Error(`Unsupported signType: ${this.signType}`);
|
||||
}
|
||||
return crypto.createHash("md5").update(content + this.keySecret, "utf8").digest("hex");
|
||||
}
|
||||
|
||||
getToken(options = {}) {
|
||||
const encrypt = options.encrypt ?? this.encrypt;
|
||||
const content = JSON.stringify({
|
||||
keyId: this.keyId,
|
||||
t: Math.floor(Date.now() / 1000),
|
||||
encrypt,
|
||||
signType: this.signType,
|
||||
});
|
||||
const sign = this.getSign(content);
|
||||
return `${Buffer.from(content, "utf8").toString("base64")}.${Buffer.from(sign, "utf8").toString("base64")}`;
|
||||
}
|
||||
|
||||
async request(path, body = {}, options = {}) {
|
||||
const response = await fetch(`${this.baseUrl}${path}`, {
|
||||
method: options.method || "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"x-certd-token": this.getToken({ encrypt: options.encrypt }),
|
||||
...(options.headers || {}),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
const text = await response.text();
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${text}`);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
getCert(params) {
|
||||
return this.request("/api/v1/cert/get", params);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { CertdClient };
|
||||
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { CertdClient } = require("./certd-client");
|
||||
|
||||
function requireEnv(name) {
|
||||
const value = process.env[name];
|
||||
if (!value) {
|
||||
throw new Error(`Missing environment variable: ${name}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function boolEnv(name, defaultValue = false) {
|
||||
const value = process.env[name];
|
||||
if (value == null || value === "") {
|
||||
return defaultValue;
|
||||
}
|
||||
return ["1", "true", "yes", "y"].includes(value.toLowerCase());
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const client = new CertdClient(requireEnv("CERTD_KEY_ID"), requireEnv("CERTD_KEY_SECRET"), {
|
||||
baseUrl: process.env.CERTD_BASE_URL,
|
||||
encrypt: boolEnv("CERTD_ENCRYPT"),
|
||||
});
|
||||
|
||||
const params = {
|
||||
autoApply: boolEnv("CERTD_AUTO_APPLY"),
|
||||
};
|
||||
if (process.env.CERTD_CERT_ID) {
|
||||
params.certId = Number(process.env.CERTD_CERT_ID);
|
||||
if (!Number.isInteger(params.certId) || params.certId <= 0) {
|
||||
throw new Error("CERTD_CERT_ID must be a positive integer");
|
||||
}
|
||||
}
|
||||
if (process.env.CERTD_DOMAINS) {
|
||||
params.domains = process.env.CERTD_DOMAINS;
|
||||
}
|
||||
if (process.env.CERTD_FORMAT) {
|
||||
params.format = process.env.CERTD_FORMAT;
|
||||
}
|
||||
if (!params.certId && !params.domains) {
|
||||
throw new Error("Set CERTD_CERT_ID or CERTD_DOMAINS");
|
||||
}
|
||||
|
||||
console.log(await client.getCert(params));
|
||||
}
|
||||
|
||||
main().catch(error => {
|
||||
console.error(error.message);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user