Reformat code

This commit is contained in:
Pierre Lannoy 2023-12-12 19:54:00 +01:00
commit 5a6e557913
Signed by: Pierre Lannoy
GPG key ID: D27231EF87D53F31
4 changed files with 58 additions and 41 deletions

View file

@ -9,6 +9,7 @@ import CompactSign from "jose/jws/compact/sign";
import pem from "pem";
import * as common from "./common.js";
import request from "./request.js";
const createCsr = promisify(pem.createCSR);
/**
@ -37,6 +38,7 @@ class Client {
this.replayNonce = '';
this.thumbprint = '';
}
/**
* Export account public and private keys to a directory.
*
@ -56,6 +58,7 @@ class Client {
common.writeKeyToFile(publicKeyFile, this.accountPublicKey)
]);
}
/**
* Generate new account public and private keys.
*
@ -69,6 +72,7 @@ class Client {
this.accountPublicKey = publicKey;
await this.initAccountJwks();
}
/**
* Generate a certificate from Let's Encrypt for your domain.
*
@ -91,6 +95,7 @@ class Client {
privateKeyData
};
}
/**
* Import account public and private keys from a directory.
*
@ -108,6 +113,7 @@ class Client {
this.accountPublicKey = common.importPublicKey(publicKeyData);
await this.initAccountJwks();
}
async authz(authzUrl) {
const data = await this.sign({
kid: this.myAccountUrl,
@ -133,10 +139,12 @@ class Client {
...rest
};
}
async completeChallenge(challenge, domain) {
await this.readyChallenge(challenge);
await this.receiveServerRequest(challenge, domain);
}
async directory() {
if (this.hasDirectory)
return false;
@ -150,6 +158,7 @@ class Client {
this.newOrderUrl = res.data.newOrder;
return true;
}
async fetchCertificate(certificateUrl) {
const data = await this.sign({
kid: this.myAccountUrl,
@ -170,11 +179,13 @@ class Client {
}
return res.data;
}
async finalizeOrder(finalizeUrl, domain) {
const {privateKey} = await generateKeyPair(common.CERTIFICATE_KEY_ALGORITHM);
// @ts-ignore
const clientKey = common.exportPrivateKey(privateKey);
let { csr
let {
csr
// @ts-ignore
} = await createCsr({
clientKey,
@ -215,6 +226,7 @@ class Client {
privateKeyData: clientKey
};
}
async initAccountJwks() {
if (this.accountPrivateKey == null || this.accountPublicKey == null) {
return Promise.reject(new Error('Account key pair not generated'));
@ -227,6 +239,7 @@ class Client {
this.accountPrivateJwk = accountPrivateJwk;
this.thumbprint = await calculateThumbprint(publicJwk);
}
async newAccount(...emails) {
const data = await this.sign({
jwk: this.accountPublicJwk,
@ -249,6 +262,7 @@ class Client {
this.myAccountUrl = res.headers.location;
return res.statusCode === 201;
}
async newNonce() {
if (this.replayNonce)
return false;
@ -261,6 +275,7 @@ class Client {
this.setReplayNonce(res);
return true;
}
async newOrder(...domains) {
const identifiers = domains.map(domain => ({
type: 'dns',
@ -293,6 +308,7 @@ class Client {
orderUrl
};
}
async pollAuthz(authzUrl) {
for (let i = 0; i < 10; i++) {
const result = await this.authz(authzUrl);
@ -307,6 +323,7 @@ class Client {
}
throw new Error('pollAuthz() timed out');
}
async readyChallenge(challenge) {
const data = await this.sign({
kid: this.myAccountUrl,
@ -325,6 +342,7 @@ class Client {
throw new Error(`readyChallenge() Status Code: ${res.statusCode} Data: ${res.data}`);
}
}
receiveServerRequest(challenge, domain) {
return new Promise((resolve, reject) => {
const time = setTimeout(() => {
@ -344,6 +362,7 @@ class Client {
};
});
}
setReplayNonce(res) {
const replayNonce = (res.headers['replay-nonce'] || '').trim();
if (!replayNonce) {
@ -351,6 +370,7 @@ class Client {
}
this.replayNonce = replayNonce;
}
/**
* @param {import("jose/types.js").JWSHeaderParameters} header
* @param {import("jose/types.js").JWTPayload | undefined} [payload]
@ -367,8 +387,7 @@ class Client {
...header
})
.sign(this.accountPrivateKey);
}
else {
} else {
// SignJWT constructor only accepts object but RFC8555 requires empty payload
// Workaround: manually pass empty Uint8Array to CompactSign constructor
const sig = new CompactSign(new Uint8Array());
@ -386,4 +405,5 @@ class Client {
});
}
}
export default Client;

View file

@ -1,5 +1,6 @@
import crypto from "crypto";
import fs from "fs";
export const ACCOUNT_KEY_ALGORITHM = 'ES256';
export const CERTIFICATE_KEY_ALGORITHM = 'RS256';
const env = (process.env.NODE_ENV || '').trim().toLowerCase();
@ -61,8 +62,7 @@ export const importPrivateKey = (privateKeyData, passphrase) => {
}
try {
return crypto.createPrivateKey(privateKeyOpts);
}
catch {
} catch {
throw new Error('Failed to import private key');
}
};
@ -79,8 +79,7 @@ export const importPublicKey = publicKeyData => {
format: PUBLIC_KEY_FORMAT,
type: PUBLIC_KEY_TYPE
});
}
catch {
} catch {
throw new Error('Failed to import public key');
}
};
@ -97,8 +96,7 @@ export const writeKeyToFile = async (filename, key, passphrase) => {
key = key.includes('PRIVATE KEY')
? importPrivateKey(key, passphrase)
: importPublicKey(key);
}
else if (!(key instanceof crypto.KeyObject)) {
} else if (!(key instanceof crypto.KeyObject)) {
throw new Error('Expected "key" to be crypto.KeyObject or string');
}
const isPrivateKey = key.type === 'private';

View file

@ -1,3 +1,4 @@
import Client from './client.js';
export * from './common.js';
export {Client};

View file

@ -10,8 +10,7 @@ const request = (url, { data = '', ...options } = {}, cb) => {
return new Promise((resolve, reject) => {
try {
url = new URL(url);
}
catch (err) {
} catch (err) {
return reject(err);
}
https.request(url, options, res => {
@ -25,8 +24,7 @@ const request = (url, { data = '', ...options } = {}, cb) => {
if (headers['content-type']?.includes('application/json')) {
try {
data = JSON.parse(data);
}
catch (err) {
} catch (err) {
reject(err);
return;
}