@push.rocks/smartacme 4.0.8 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist_ts/00_commitinfo_data.d.ts +1 -1
- package/dist_ts/00_commitinfo_data.js +4 -4
- package/dist_ts/handlers/Dns01Handler.d.ts +13 -0
- package/dist_ts/handlers/Dns01Handler.js +24 -0
- package/dist_ts/handlers/Http01Handler.d.ts +34 -0
- package/dist_ts/handlers/Http01Handler.js +32 -0
- package/dist_ts/handlers/IChallengeHandler.d.ts +22 -0
- package/dist_ts/handlers/IChallengeHandler.js +2 -0
- package/dist_ts/handlers/index.d.ts +3 -0
- package/dist_ts/handlers/index.js +4 -0
- package/dist_ts/index.d.ts +1 -1
- package/dist_ts/index.js +2 -2
- package/dist_ts/smartacme.classes.cert.d.ts +1 -1
- package/dist_ts/smartacme.classes.cert.js +14 -14
- package/dist_ts/smartacme.classes.certmanager.d.ts +4 -4
- package/dist_ts/smartacme.classes.certmanager.js +7 -7
- package/dist_ts/smartacme.classes.certmatcher.d.ts +1 -1
- package/dist_ts/smartacme.classes.certmatcher.js +2 -2
- package/dist_ts/smartacme.classes.smartacme.d.ts +39 -10
- package/dist_ts/smartacme.classes.smartacme.js +150 -48
- package/dist_ts/smartacme.plugins.d.ts +4 -2
- package/dist_ts/smartacme.plugins.js +6 -3
- package/npmextra.json +23 -3
- package/package.json +39 -27
- package/readme.hints.md +2 -0
- package/readme.md +235 -39
- package/ts/00_commitinfo_data.ts +3 -3
- package/ts/handlers/Dns01Handler.ts +40 -0
- package/ts/handlers/Http01Handler.ts +54 -0
- package/ts/handlers/IChallengeHandler.ts +22 -0
- package/ts/handlers/index.ts +4 -0
- package/ts/index.ts +1 -1
- package/ts/smartacme.classes.cert.ts +4 -4
- package/ts/smartacme.classes.certmanager.ts +9 -9
- package/ts/smartacme.classes.certmatcher.ts +1 -1
- package/ts/smartacme.classes.smartacme.ts +185 -64
- package/ts/smartacme.plugins.ts +5 -2
package/readme.md
CHANGED
|
@@ -1,35 +1,198 @@
|
|
|
1
1
|
# @push.rocks/smartacme
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
Code Style | [](https://lossless.cloud)
|
|
21
|
-
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
|
22
|
-
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
|
23
|
-
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
|
2
|
+
|
|
3
|
+
A TypeScript-based ACME client with an easy yet powerful interface for LetsEncrypt certificate management.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
To install `@push.rocks/smartacme`, you can use npm or yarn. Run one of the following commands in your project directory:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @push.rocks/smartacme --save
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
or
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add @push.rocks/smartacme
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Make sure your project is set up to use TypeScript and supports ECMAScript Modules (ESM).
|
|
24
20
|
|
|
25
21
|
## Usage
|
|
26
22
|
|
|
27
|
-
|
|
23
|
+
This guide will walk you through using `@push.rocks/smartacme` to set up and manage ACME (Automated Certificate Management Environment) certificates with a focus on the Let's Encrypt service, which provides free SSL certificates. The library provides an easy yet powerful TypeScript interface to automate the process of obtaining, renewing, and installing your SSL certificates.
|
|
24
|
+
|
|
25
|
+
### Table of Contents
|
|
26
|
+
|
|
27
|
+
1. [Setting Up Your Project](#setting-up-your-project)
|
|
28
|
+
2. [Creating a SmartAcme Instance](#creating-a-smartacme-instance)
|
|
29
|
+
3. [Initializing SmartAcme](#initializing-smartacme)
|
|
30
|
+
4. [Obtaining a Certificate for a Domain](#obtaining-a-certificate-for-a-domain)
|
|
31
|
+
5. [Automating DNS Challenges](#automating-dns-challenges)
|
|
32
|
+
6. [Managing Certificates](#managing-certificates)
|
|
33
|
+
7. [Environmental Considerations](#environmental-considerations)
|
|
34
|
+
8. [Complete Example](#complete-example)
|
|
35
|
+
|
|
36
|
+
### Setting Up Your Project
|
|
37
|
+
|
|
38
|
+
Ensure your project includes the necessary TypeScript configuration and dependencies. You'll need to have TypeScript installed and configured for ECMAScript Modules. If you are new to TypeScript, review its [documentation](https://www.typescriptlang.org/docs/) to get started.
|
|
39
|
+
|
|
40
|
+
### Creating a SmartAcme Instance
|
|
41
|
+
|
|
42
|
+
Start by importing the `SmartAcme` class and any built-in handlers you plan to use. For example, to use DNS-01 via Cloudflare:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { SmartAcme } from '@push.rocks/smartacme';
|
|
46
|
+
import * as cloudflare from '@apiclient.xyz/cloudflare';
|
|
47
|
+
import { Dns01Handler } from '@push.rocks/smartacme/ts/handlers/Dns01Handler.js';
|
|
48
|
+
|
|
49
|
+
// Create a Cloudflare account client with your API token
|
|
50
|
+
const cfAccount = new cloudflare.CloudflareAccount('YOUR_CF_TOKEN');
|
|
51
|
+
|
|
52
|
+
// Instantiate SmartAcme with one or more ACME challenge handlers
|
|
53
|
+
const smartAcmeInstance = new SmartAcme({
|
|
54
|
+
accountEmail: 'youremail@example.com',
|
|
55
|
+
mongoDescriptor: {
|
|
56
|
+
mongoDbUrl: 'mongodb://yourmongoURL',
|
|
57
|
+
mongoDbName: 'yourDbName',
|
|
58
|
+
mongoDbPass: 'yourDbPassword',
|
|
59
|
+
},
|
|
60
|
+
environment: 'integration', // 'production' to request real certificates
|
|
61
|
+
retryOptions: {}, // optional retry/backoff settings
|
|
62
|
+
challengeHandlers: [
|
|
63
|
+
new Dns01Handler(cfAccount),
|
|
64
|
+
// you can add more handlers, e.g. Http01Handler
|
|
65
|
+
],
|
|
66
|
+
challengePriority: ['dns-01'], // optional ordering of challenge types
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Initializing SmartAcme
|
|
71
|
+
|
|
72
|
+
Before proceeding to request certificates, start your SmartAcme instance:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
await smartAcmeInstance.start();
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Obtaining a Certificate for a Domain
|
|
79
|
+
|
|
80
|
+
To obtain a certificate for a specific domain, use the `getCertificateForDomain` method. This function ensures that if a valid certificate is already present, it will be reused; otherwise, a new certificate is obtained:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const myDomain = 'example.com';
|
|
84
|
+
const myCert = await smartAcmeInstance.getCertificateForDomain(myDomain);
|
|
85
|
+
console.log('Certificate:', myCert);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Automating DNS Challenges
|
|
89
|
+
|
|
90
|
+
SmartAcme uses pluggable ACME challenge handlers (see built-in handlers below) to automate domain validation. You configure handlers via the `challengeHandlers` array when creating the instance, and SmartAcme will invoke each handler’s `prepare`, optional `verify`, and `cleanup` methods during the ACME order flow.
|
|
91
|
+
|
|
92
|
+
### Managing Certificates
|
|
93
|
+
|
|
94
|
+
The library automatically handles fetching, renewing, and storing your certificates in a MongoDB database specified in your configuration. Ensure your MongoDB instance is accessible and properly configured for use with SmartAcme.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
const mongoDescriptor = {
|
|
98
|
+
mongoDbUrl: 'mongodb://yourmongoURL',
|
|
99
|
+
mongoDbName: 'yourDbName',
|
|
100
|
+
mongoDbPass: 'yourDbPassword',
|
|
101
|
+
};
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Environmental Considerations
|
|
105
|
+
|
|
106
|
+
When creating an instance of `SmartAcme`, you can specify an `environment` option. This is particularly useful for testing, as you can use the `integration` environment to avoid hitting rate limits and for testing your setup without issuing real certificates. Switch to `production` when you are ready to obtain actual certificates.
|
|
107
|
+
|
|
108
|
+
### Complete Example
|
|
109
|
+
|
|
110
|
+
Below is a complete example demonstrating how to use `@push.rocks/smartacme` to obtain and manage an ACME certificate with Let's Encrypt using a DNS-01 handler:
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { SmartAcme } from '@push.rocks/smartacme';
|
|
114
|
+
import * as cloudflare from '@apiclient.xyz/cloudflare';
|
|
115
|
+
import { Qenv } from '@push.rocks/qenv';
|
|
116
|
+
|
|
117
|
+
const qenv = new Qenv('./', './.nogit/');
|
|
118
|
+
const cloudflareAccount = new cloudflare.CloudflareAccount(qenv.getEnvVarOnDemand('CF_TOKEN'));
|
|
119
|
+
|
|
120
|
+
async function main() {
|
|
121
|
+
const smartAcmeInstance = new SmartAcme({
|
|
122
|
+
accountEmail: 'youremail@example.com',
|
|
123
|
+
mongoDescriptor: {
|
|
124
|
+
mongoDbUrl: qenv.getEnvVarRequired('MONGODB_URL'),
|
|
125
|
+
mongoDbName: qenv.getEnvVarRequired('MONGODB_DATABASE'),
|
|
126
|
+
mongoDbPass: qenv.getEnvVarRequired('MONGODB_PASSWORD'),
|
|
127
|
+
},
|
|
128
|
+
environment: 'integration',
|
|
129
|
+
challengeHandlers: [ new Dns01Handler(cloudflareAccount) ],
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
await smartAcmeInstance.start();
|
|
133
|
+
|
|
134
|
+
const myDomain = 'example.com';
|
|
135
|
+
const myCert = await smartAcmeInstance.getCertificateForDomain(myDomain);
|
|
136
|
+
console.log('Certificate:', myCert);
|
|
137
|
+
|
|
138
|
+
await smartAcmeInstance.stop();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
main().catch(console.error);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
In this example, `Qenv` is used to manage environment variables, and `cloudflare` library is used to handle DNS challenges required by Let's Encrypt ACME protocol. The `setChallenge` and `removeChallenge` methods are essential for automating the DNS challenge process, which is a key part of domain validation.
|
|
145
|
+
|
|
146
|
+
## Additional Details
|
|
147
|
+
|
|
148
|
+
### Certificate Object
|
|
149
|
+
|
|
150
|
+
The certificate object obtained from the `getCertificateForDomain` method has the following properties:
|
|
151
|
+
|
|
152
|
+
- `id`: Unique identifier for the certificate.
|
|
153
|
+
- `domainName`: The domain name for which the certificate is issued.
|
|
154
|
+
- `created`: Timestamp of when the certificate was created.
|
|
155
|
+
- `privateKey`: The private key associated with the certificate.
|
|
156
|
+
- `publicKey`: The public key or certificate itself.
|
|
157
|
+
- `csr`: Certificate Signing Request (CSR) used to obtain the certificate.
|
|
158
|
+
- `validUntil`: Timestamp indicating the expiration date of the certificate.
|
|
159
|
+
|
|
160
|
+
### Methods Summary
|
|
161
|
+
|
|
162
|
+
- **start()**: Initializes the SmartAcme instance, sets up the ACME client, and registers the account with Let's Encrypt.
|
|
163
|
+
- **stop()**: Closes the MongoDB connection and performs any necessary cleanup.
|
|
164
|
+
- **getCertificateForDomain(domainArg: string)**: Retrieves or obtains a certificate for the specified domain name. If a valid certificate exists in the database, it is returned. Otherwise, a new certificate is requested and stored.
|
|
165
|
+
- **setChallenge(dnsChallenge: any)**: Automates the process of setting DNS challenge records.
|
|
166
|
+
- **removeChallenge(dnsChallenge: any)**: Automates the process of removing DNS challenge records.
|
|
167
|
+
|
|
168
|
+
### Handling Domain Matching
|
|
169
|
+
|
|
170
|
+
The `SmartacmeCertMatcher` class is responsible for matching certificates with the broadest scope for wildcard certificates. The `getCertificateDomainNameByDomainName` method ensures that domains at various levels are correctly matched.
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import { SmartacmeCertMatcher } from '@push.rocks/smartacme';
|
|
174
|
+
|
|
175
|
+
const certMatcher = new SmartacmeCertMatcher();
|
|
176
|
+
const certDomainName = certMatcher.getCertificateDomainNameByDomainName('subdomain.example.com');
|
|
177
|
+
console.log('Certificate Domain Name:', certDomainName); // Output: example.com
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Testing
|
|
28
181
|
|
|
29
|
-
|
|
30
|
-
import { SmartAcme } from 'smartacme';
|
|
182
|
+
Automated tests can be added to ensure that the setup and functions work as expected. Using a testing framework such as `tap` and mock services for DNS providers (e.g., Cloudflare), you can simulate the process of obtaining and managing certificates without the need for actual domain ownership.
|
|
31
183
|
|
|
32
|
-
|
|
184
|
+
```typescript
|
|
185
|
+
import { tap, expect } from '@push.rocks/tapbundle';
|
|
186
|
+
import { Qenv } from '@push.rocks/qenv';
|
|
187
|
+
import * as cloudflare from '@apiclient.xyz/cloudflare';
|
|
188
|
+
import * as smartacme from '@push.rocks/smartacme';
|
|
189
|
+
|
|
190
|
+
const testQenv = new Qenv('./', './.nogit/');
|
|
191
|
+
const testCloudflare = new cloudflare.CloudflareAccount(testQenv.getEnvVarOnDemand('CF_TOKEN'));
|
|
192
|
+
|
|
193
|
+
let smartAcmeInstance: smartacme.SmartAcme;
|
|
194
|
+
|
|
195
|
+
tap.test('should create a valid instance of SmartAcme', async () => {
|
|
33
196
|
smartAcmeInstance = new smartacme.SmartAcme({
|
|
34
197
|
accountEmail: 'domains@lossless.org',
|
|
35
198
|
accountPrivateKey: null,
|
|
@@ -38,27 +201,60 @@ const run = async () => {
|
|
|
38
201
|
mongoDbPass: testQenv.getEnvVarRequired('MONGODB_PASSWORD'),
|
|
39
202
|
mongoDbUrl: testQenv.getEnvVarRequired('MONGODB_URL'),
|
|
40
203
|
},
|
|
41
|
-
removeChallenge: async (dnsChallenge) => {
|
|
42
|
-
// somehow provide a function that is able to remove the dns challenge
|
|
43
|
-
},
|
|
44
204
|
setChallenge: async (dnsChallenge) => {
|
|
45
|
-
|
|
205
|
+
await testCloudflare.convenience.acmeSetDnsChallenge(dnsChallenge);
|
|
206
|
+
},
|
|
207
|
+
removeChallenge: async (dnsChallenge) => {
|
|
208
|
+
await testCloudflare.convenience.acmeRemoveDnsChallenge(dnsChallenge);
|
|
46
209
|
},
|
|
47
210
|
environment: 'integration',
|
|
48
211
|
});
|
|
49
212
|
await smartAcmeInstance.init();
|
|
213
|
+
expect(smartAcmeInstance).toBeInstanceOf(smartacme.SmartAcme);
|
|
214
|
+
});
|
|
50
215
|
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
216
|
+
tap.test('should get a domain certificate', async () => {
|
|
217
|
+
const certificate = await smartAcmeInstance.getCertificateForDomain('example.com');
|
|
218
|
+
console.log('Certificate:', certificate);
|
|
219
|
+
expect(certificate).toHaveProperty('domainName', 'example.com');
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
tap.test('certmatcher should correctly match domains', async () => {
|
|
223
|
+
const certMatcher = new smartacme.SmartacmeCertMatcher();
|
|
224
|
+
const matchedCert = certMatcher.getCertificateDomainNameByDomainName('subdomain.example.com');
|
|
225
|
+
expect(matchedCert).toBe('example.com');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
tap.test('should stop correctly', async () => {
|
|
229
|
+
await smartAcmeInstance.stop();
|
|
230
|
+
expect(smartAcmeInstance).toHaveProperty('client', null);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
tap.start();
|
|
54
234
|
```
|
|
55
235
|
|
|
56
|
-
|
|
236
|
+
This comprehensive guide ensures you can set up, manage, and test ACME certificates efficiently and effectively using `@push.rocks/smartacme`.
|
|
57
237
|
|
|
58
|
-
|
|
238
|
+
---
|
|
59
239
|
|
|
60
|
-
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## License and Legal Information
|
|
243
|
+
|
|
244
|
+
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
|
245
|
+
|
|
246
|
+
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
|
247
|
+
|
|
248
|
+
### Trademarks
|
|
61
249
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
250
|
+
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
|
251
|
+
|
|
252
|
+
### Company Information
|
|
253
|
+
|
|
254
|
+
Task Venture Capital GmbH
|
|
255
|
+
Registered at District court Bremen HRB 35230 HB, Germany
|
|
256
|
+
|
|
257
|
+
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
|
258
|
+
|
|
259
|
+
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
|
260
|
+
```
|
package/ts/00_commitinfo_data.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* autocreated commitinfo by @
|
|
2
|
+
* autocreated commitinfo by @push.rocks/commitinfo
|
|
3
3
|
*/
|
|
4
4
|
export const commitinfo = {
|
|
5
5
|
name: '@push.rocks/smartacme',
|
|
6
|
-
version: '
|
|
7
|
-
description: '
|
|
6
|
+
version: '6.0.0',
|
|
7
|
+
description: 'A TypeScript-based ACME client for LetsEncrypt certificate management with a focus on simplicity and power.'
|
|
8
8
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as plugins from '../smartacme.plugins.js';
|
|
2
|
+
import type { IChallengeHandler } from './IChallengeHandler.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* DNS-01 challenge handler using CloudflareAccount and Smartdns.
|
|
6
|
+
*/
|
|
7
|
+
export class Dns01Handler implements IChallengeHandler<plugins.tsclass.network.IDnsChallenge> {
|
|
8
|
+
private cf: any;
|
|
9
|
+
private smartdns: plugins.smartdnsClient.Smartdns;
|
|
10
|
+
|
|
11
|
+
constructor(
|
|
12
|
+
cloudflareAccount: any,
|
|
13
|
+
smartdnsInstance?: plugins.smartdnsClient.Smartdns,
|
|
14
|
+
) {
|
|
15
|
+
this.cf = cloudflareAccount;
|
|
16
|
+
this.smartdns = smartdnsInstance ?? new plugins.smartdnsClient.Smartdns({});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public getSupportedTypes(): string[] {
|
|
20
|
+
return ['dns-01'];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public async prepare(ch: plugins.tsclass.network.IDnsChallenge): Promise<void> {
|
|
24
|
+
// set DNS TXT record
|
|
25
|
+
await this.cf.convenience.acmeSetDnsChallenge(ch);
|
|
26
|
+
// wait for DNS propagation
|
|
27
|
+
await this.smartdns.checkUntilAvailable(
|
|
28
|
+
ch.hostName,
|
|
29
|
+
'TXT',
|
|
30
|
+
ch.challenge,
|
|
31
|
+
100,
|
|
32
|
+
5000,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public async cleanup(ch: plugins.tsclass.network.IDnsChallenge): Promise<void> {
|
|
37
|
+
// remove DNS TXT record
|
|
38
|
+
await this.cf.convenience.acmeRemoveDnsChallenge(ch);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import type { IChallengeHandler } from './IChallengeHandler.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* HTTP-01 ACME challenge handler using file-system webroot.
|
|
7
|
+
* Writes and removes the challenge file under <webroot>/.well-known/acme-challenge/.
|
|
8
|
+
*/
|
|
9
|
+
export interface Http01HandlerOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Directory that serves HTTP requests for /.well-known/acme-challenge
|
|
12
|
+
*/
|
|
13
|
+
webroot: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class Http01Handler implements IChallengeHandler<{
|
|
17
|
+
type: string;
|
|
18
|
+
token: string;
|
|
19
|
+
keyAuthorization: string;
|
|
20
|
+
webPath: string;
|
|
21
|
+
}> {
|
|
22
|
+
private webroot: string;
|
|
23
|
+
|
|
24
|
+
constructor(options: Http01HandlerOptions) {
|
|
25
|
+
this.webroot = options.webroot;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public getSupportedTypes(): string[] {
|
|
29
|
+
return ['http-01'];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async prepare(ch: { token: string; keyAuthorization: string; webPath: string }): Promise<void> {
|
|
33
|
+
const relWebPath = ch.webPath.replace(/^\/+/, '');
|
|
34
|
+
const filePath = path.join(this.webroot, relWebPath);
|
|
35
|
+
const dir = path.dirname(filePath);
|
|
36
|
+
await fs.mkdir(dir, { recursive: true });
|
|
37
|
+
await fs.writeFile(filePath, ch.keyAuthorization, 'utf8');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public async verify(ch: { webPath: string; keyAuthorization: string }): Promise<void> {
|
|
41
|
+
// Optional: implement HTTP polling if desired
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public async cleanup(ch: { token: string; webPath: string }): Promise<void> {
|
|
46
|
+
const relWebPath = ch.webPath.replace(/^\/+/, '');
|
|
47
|
+
const filePath = path.join(this.webroot, relWebPath);
|
|
48
|
+
try {
|
|
49
|
+
await fs.unlink(filePath);
|
|
50
|
+
} catch {
|
|
51
|
+
// ignore missing file
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pluggable interface for ACME challenge handlers.
|
|
3
|
+
* Supports DNS-01, HTTP-01, TLS-ALPN-01, or custom challenge types.
|
|
4
|
+
*/
|
|
5
|
+
export interface IChallengeHandler<T> {
|
|
6
|
+
/**
|
|
7
|
+
* ACME challenge types this handler supports (e.g. ['dns-01']).
|
|
8
|
+
*/
|
|
9
|
+
getSupportedTypes(): string[];
|
|
10
|
+
/**
|
|
11
|
+
* Prepare the challenge: set DNS record, start HTTP/TLS server, etc.
|
|
12
|
+
*/
|
|
13
|
+
prepare(ch: T): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Optional extra verify step (HTTP GET, ALPN handshake).
|
|
16
|
+
*/
|
|
17
|
+
verify?(ch: T): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Clean up resources: remove DNS record, stop server.
|
|
20
|
+
*/
|
|
21
|
+
cleanup(ch: T): Promise<void>;
|
|
22
|
+
}
|
package/ts/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './smartacme.classes.smartacme.js';
|
|
2
|
-
export { Cert } from './smartacme.classes.cert.js';
|
|
2
|
+
export { SmartacmeCert as Cert } from './smartacme.classes.cert.js';
|
|
@@ -2,15 +2,15 @@ import * as plugins from './smartacme.plugins.js';
|
|
|
2
2
|
|
|
3
3
|
import * as interfaces from './interfaces/index.js';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { SmartacmeCertManager } from './smartacme.classes.certmanager.js';
|
|
6
6
|
|
|
7
7
|
import { Collection, svDb, unI } from '@push.rocks/smartdata';
|
|
8
8
|
|
|
9
9
|
@plugins.smartdata.Collection(() => {
|
|
10
|
-
return
|
|
10
|
+
return SmartacmeCertManager.activeDB;
|
|
11
11
|
})
|
|
12
|
-
export class
|
|
13
|
-
extends plugins.smartdata.SmartDataDbDoc<
|
|
12
|
+
export class SmartacmeCert
|
|
13
|
+
extends plugins.smartdata.SmartDataDbDoc<SmartacmeCert, plugins.tsclass.network.ICert>
|
|
14
14
|
implements plugins.tsclass.network.ICert
|
|
15
15
|
{
|
|
16
16
|
@unI()
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as plugins from './smartacme.plugins.js';
|
|
2
|
-
import {
|
|
2
|
+
import { SmartacmeCert } from './smartacme.classes.cert.js';
|
|
3
3
|
import { SmartAcme } from './smartacme.classes.smartacme.js';
|
|
4
4
|
|
|
5
5
|
import * as interfaces from './interfaces/index.js';
|
|
6
6
|
|
|
7
|
-
export class
|
|
7
|
+
export class SmartacmeCertManager {
|
|
8
8
|
// =========
|
|
9
9
|
// STATIC
|
|
10
10
|
// =========
|
|
@@ -16,13 +16,13 @@ export class CertManager {
|
|
|
16
16
|
private mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
|
17
17
|
public smartdataDb: plugins.smartdata.SmartdataDb;
|
|
18
18
|
|
|
19
|
-
public interestMap: plugins.lik.InterestMap<string,
|
|
19
|
+
public interestMap: plugins.lik.InterestMap<string, SmartacmeCert>;
|
|
20
20
|
|
|
21
21
|
constructor(
|
|
22
22
|
smartAcmeArg: SmartAcme,
|
|
23
23
|
optionsArg: {
|
|
24
24
|
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
|
25
|
-
}
|
|
25
|
+
},
|
|
26
26
|
) {
|
|
27
27
|
this.mongoDescriptor = optionsArg.mongoDescriptor;
|
|
28
28
|
}
|
|
@@ -31,7 +31,7 @@ export class CertManager {
|
|
|
31
31
|
// Smartdata DB
|
|
32
32
|
this.smartdataDb = new plugins.smartdata.SmartdataDb(this.mongoDescriptor);
|
|
33
33
|
await this.smartdataDb.init();
|
|
34
|
-
|
|
34
|
+
SmartacmeCertManager.activeDB = this.smartdataDb;
|
|
35
35
|
|
|
36
36
|
// Pending Map
|
|
37
37
|
this.interestMap = new plugins.lik.InterestMap((certName) => certName);
|
|
@@ -42,8 +42,8 @@ export class CertManager {
|
|
|
42
42
|
* @returns the Cert class or null
|
|
43
43
|
* @param certDomainNameArg the domain Name to retrieve the vcertificate for
|
|
44
44
|
*/
|
|
45
|
-
public async retrieveCertificate(certDomainNameArg: string): Promise<
|
|
46
|
-
const existingCertificate:
|
|
45
|
+
public async retrieveCertificate(certDomainNameArg: string): Promise<SmartacmeCert> {
|
|
46
|
+
const existingCertificate: SmartacmeCert = await SmartacmeCert.getInstance<SmartacmeCert>({
|
|
47
47
|
domainName: certDomainNameArg,
|
|
48
48
|
});
|
|
49
49
|
|
|
@@ -59,7 +59,7 @@ export class CertManager {
|
|
|
59
59
|
* @param optionsArg
|
|
60
60
|
*/
|
|
61
61
|
public async storeCertificate(optionsArg: plugins.tsclass.network.ICert) {
|
|
62
|
-
const cert = new
|
|
62
|
+
const cert = new SmartacmeCert(optionsArg);
|
|
63
63
|
await cert.save();
|
|
64
64
|
const interest = this.interestMap.findInterest(cert.domainName);
|
|
65
65
|
if (interest) {
|
|
@@ -69,7 +69,7 @@ export class CertManager {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
public async deleteCertificate(certDomainNameArg: string) {
|
|
72
|
-
const cert:
|
|
72
|
+
const cert: SmartacmeCert = await SmartacmeCert.getInstance<SmartacmeCert>({
|
|
73
73
|
domainName: certDomainNameArg,
|
|
74
74
|
});
|
|
75
75
|
await cert.delete();
|
|
@@ -4,7 +4,7 @@ import * as interfaces from './interfaces/index.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* certmatcher is responsible for matching certificates
|
|
6
6
|
*/
|
|
7
|
-
export class
|
|
7
|
+
export class SmartacmeCertMatcher {
|
|
8
8
|
/**
|
|
9
9
|
* creates a domainName for the certificate that will include the broadest scope
|
|
10
10
|
* for wild card certificates
|