Skip to content

Commit db88b66

Browse files
authored
LetsEncryptDaemon ES6 Class Default Export (#4)
class LetsEncryptDaemon is the default export
1 parent 60e69a8 commit db88b66

File tree

5 files changed

+578
-489
lines changed

5 files changed

+578
-489
lines changed

README.md

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,36 @@ Automatically Create and Renew `LetsEncrypt! SSL Certificates`, including `Wildc
44

55
### Getting Started
66

7-
This most recent version of this package is implemented in [`SSL Server`](https://github.com/FirstTimeEZ/server-ssl) and you can use that to understand how it works if the `jsdoc` isn't enough information.
7+
```javascript
8+
import LetsEncryptDaemon from 'lets-encrypt-acme-client';
9+
```
810

9-
### Wild Card Certificates
11+
#### Simple Usage Example
1012

11-
You can generate `Wild Card Certificates` if you are using a supported `DNS Provider`
13+
Create a `LetsEncryptDaemon` and then start the `Daemon`
14+
15+
```javascript
16+
const daemon = new LetsEncryptDaemon();
17+
await daemon.startLetsEncryptDaemon(...); // You can only start this once, it will configure itself to run again.
18+
daemon.checkChallengesMixin(...); // You must check the HTTP-01 Challenges for each LetsEncryptDaemon
19+
```
20+
21+
#### Complete Example Usage
22+
23+
This most recent version of this package is implemented in [`SSL Server`](https://github.com/FirstTimeEZ/server-ssl)
24+
25+
You can use [`SSL Server`](https://github.com/FirstTimeEZ/server-ssl) to understand how it works if the `jsdoc` isn't enough information.
26+
27+
--------
28+
29+
### Wild Card Certificates
1230

1331
| Supported DNS Providers |
1432
|-------------------------|
1533
| Cloud Flare |
1634

35+
You can generate `Wild Card Certificates` if you are using a supported `DNS Provider`
36+
1737
```
1838
let dnsProvider = {
1939
name: "Cloud Flare",
@@ -28,11 +48,21 @@ let dnsProvider = {
2848

2949
### LetsEncrypt! Daemon
3050

51+
`LetsEncryptDaemon` is the default exported class
52+
53+
```javascript
54+
const daemon = new LetsEncryptDaemon();
55+
```
56+
57+
### Daemon
58+
3159
The `Daemon` runs periodically to `Create` or `Renew` the `Certificate`
3260

61+
### Jsdoc
62+
3363
```javascript
3464
/**
35-
* Starts the LetsEncrypt! Daemon to Manage the SSL Certificate for the Server
65+
* Starts the LetsEncrypt! Daemon to Manage a SSL Certificate
3666
*
3767
* @param {Array<string>} fqdns - The fully qualified domain names as a SAN (e.g., ["example.com", "www.example.com"]), You must use a `dnsProvider` if you include a wild card
3868
* @param {string} sslPath - The path where your acme account, keys and generated certificate will be stored or loaded from
@@ -50,7 +80,12 @@ The `Daemon` runs periodically to `Create` or `Renew` the `Certificate`
5080
* @note
5181
* If you start this more than once nothing will happen
5282
*/
53-
export async function startLetsEncryptDaemon(fqdns, sslPath, certificateCallback, optGenerateAnyway = false, optStaging = false, dnsProvider = undefined)
83+
```
84+
85+
#### Usage
86+
87+
```javascript
88+
await daemon.startLetsEncryptDaemon(fqdns, sslPath, certificateCallback, optGenerateAnyway = false, optStaging = false, dnsProvider = undefined)
5489
```
5590

5691
### HTTP Mixin for `HTTP-01`
@@ -59,17 +94,24 @@ export async function startLetsEncryptDaemon(fqdns, sslPath, certificateCallback
5994

6095
This is not required if you are using a `DNS Provider`
6196

97+
### Jsdoc
98+
6299
```javascript
63100
/**
64-
* Node.js Middleware function to check and respond to ACME HTTP-01 challenges inside the HTTP Server.
101+
* Node.js Middleware function to check and respond to ACME HTTP-01 challenges issued by this LetsEncryptDaemon inside the HTTP Server.
65102
*
66103
* @example
67104
* createServerHTTP(async (req, res) => {
68-
* if (STATE.optLetsEncrypt && await checkChallengesMixin(req, res)) { return; }
105+
* if (STATE.optLetsEncrypt && checkChallengesMixin(req, res)) { return; }
69106
* // normal request redirect etc
70107
* }).listen(80);
71108
*/
72-
export async function checkChallengesMixin(req, res)
109+
```
110+
111+
#### Usage
112+
113+
```javascript
114+
if (daemon.checkChallengesMixin(req, res)) { return; } // Inside the HTTP Server
73115
```
74116

75117
--------

ext/ext.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright © 2024 FirstTimeEZ
3+
* https://github.com/FirstTimeEZ
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* Checks if the given certificate text is valid.
20+
* A valid certificate text starts with "-----BEGIN CERTIFICATE-----"
21+
* and ends with "-----END CERTIFICATE-----" (with or without a newline).
22+
*
23+
* @param {string} certificateText - The certificate text to validate.
24+
* @returns {boolean} True if the certificate text is valid, false otherwise.
25+
*/
26+
export function checkCertificateTextValid(certificateText) {
27+
return certificateText.startsWith("-----BEGIN CERTIFICATE-----") && (certificateText.endsWith("-----END CERTIFICATE-----\n") || certificateText.endsWith("-----END CERTIFICATE-----") || certificateText.endsWith("-----END CERTIFICATE----- "));
28+
}
29+
30+
/**
31+
* Checks if the given private key is valid.
32+
* A valid private key starts with "-----BEGIN PRIVATE KEY-----"
33+
* and ends with "-----END PRIVATE KEY-----" (with or without a newline).
34+
*
35+
* @param {string} privateKey - The private key to validate.
36+
* @returns {boolean} True if the private key is valid, false otherwise.
37+
*/
38+
export function checkPrivateKeyValid(privateKey) {
39+
return privateKey.startsWith("-----BEGIN PRIVATE KEY-----") && (privateKey.endsWith("-----END PRIVATE KEY-----") || privateKey.endsWith("-----END PRIVATE KEY-----\n") || privateKey.endsWith("-----END PRIVATE KEY----- "))
40+
}
41+
42+
/**
43+
* Extracts challenges of a specific type from a list of authorizations.
44+
* Each challenge is marked as unanswered and includes its associated domain
45+
* and wildcard status.
46+
*
47+
* @param {Array} list - The list of authorizations containing challenges.
48+
* @param {string} challengeType - The type of challenge to extract.
49+
* @returns {Array} An array of challenges of the specified type.
50+
*/
51+
export function extractChallengeType(list, challengeType) {
52+
const chals = [];
53+
54+
for (let index = 0; index < list.length; index++) {
55+
const auth = list[index];
56+
57+
for (let i1 = 0; i1 < auth.get.challenges.length; i1++) {
58+
const challenge = auth.get.challenges[i1];
59+
challenge.type == challengeType && (challenge.answered = false, challenge.domain = auth.get.identifier.value, challenge.wildcard = auth.get.wildcard ? auth.get.wildcard : false, chals.push(challenge));
60+
}
61+
}
62+
63+
return chals;
64+
}

0 commit comments

Comments
 (0)