-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor revoke method, add examples, and update README
- Refactor `revoke` method for improved clarity and efficiency. - Add usage examples to the codebase for better understanding. - Update README with new instructions and examples. - General code refactoring for better maintainability and readability.
- Loading branch information
Showing
7 changed files
with
878 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,14 +34,16 @@ This library is designed to facilitate the generation of certificates using the | |
- [x] Create a new certificate | ||
- [x] Renew a certificate | ||
- [x] Revoke a certificate | ||
- [ ] Get certificate information | ||
- [ ] Support for wildcard domains | ||
- [ ] Scheduled certificate renewal | ||
|
||
## 🚀 Tecnologies | ||
|
||
The following tools were used in the construction of the project: | ||
|
||
- [Node.js](https://nodejs.org/en/) | ||
- [TypeScript](https://www.typescriptlang.org/) | ||
- [Node-Forge](https://www.npmjs.com/package/node-forge) | ||
|
||
## 📦 Install | ||
|
||
|
@@ -62,44 +64,56 @@ yarn add @geisonjr/certfy | |
|
||
> [!TIP] | ||
> You can use the `.env` file to set the environment variables. | ||
> | ||
> - `DIRECTORY_PATH`: The path where the certificates will be saved. | ||
> | ||
> - `CERTFY_DIR`: The path where the certificates will be saved. | ||
```bash | ||
DIRECTORY_PATH=/Users/<username>/certificates | ||
CERTFY_DIR=/Users/<username>/certificates | ||
# or | ||
DIRECTORY_PATH=C:\Users\<username>\certificates | ||
CERTFY_DIR=C:\Users\<username>\certificates | ||
# or | ||
DIRECTORY_PATH=./certificates | ||
CERTFY_DIR=./certificates | ||
``` | ||
|
||
### Example | ||
|
||
```typescript | ||
import { Certfy } from '@geisonjr/certfy'; | ||
import { Certificate } from "@geisonjr/certfy"; | ||
|
||
const certfy = new Certfy() | ||
const cert = new Certificate(); | ||
|
||
// Create a new certificate | ||
certfy.obtainCertificate({ | ||
domains: ['example.com', 'www.example.com'], | ||
email: [ | ||
'[email protected]' | ||
] | ||
}) | ||
await cert.obtain({ | ||
domains: ["www.example.com", "example.com"], | ||
email: ["[email protected]"], // Optional | ||
}); | ||
|
||
// Renew a certificate | ||
certfy.renewCertificate({ | ||
domains: ['example.com', 'www.example.com'], | ||
email: [ | ||
'[email protected]' | ||
] | ||
}) | ||
|
||
const certificate = fs.readFileSync('fullchain.pem') | ||
await cert.renew({ | ||
domains: ["www.example.com", "example.com"], | ||
email: ["[email protected]"], // Optional | ||
force: true, // Optional | ||
revoke: true, // Optional | ||
reason: REASON.unspecified, // Optional | ||
}); | ||
|
||
// Revoke a certificate | ||
certfy.revokeCertificate(certificate) | ||
const certificate: string = "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----"; | ||
const privateKey: string = "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----"; | ||
|
||
await cert.revoke(certificate, privateKey, { | ||
reason: REASON.unspecified, // Optional | ||
}); | ||
``` | ||
|
||
#### Can you see a complete example [here](./example/index.ts), to run the example use the following commands: | ||
|
||
```bash | ||
npm run example | ||
``` | ||
|
||
```bash | ||
yarn example | ||
``` | ||
|
||
## 📚 References | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Certificate } from '../src' | ||
import { readFile } from '../src/util' | ||
|
||
(async () => { | ||
const cert = new Certificate({ | ||
staging: true // Change this to false or remove it to use the production environment | ||
}) | ||
|
||
try { | ||
|
||
// The domains to request a certificate for (wildcards are not supported) | ||
const domains = ['example.dev', 'www.example.dev'] // Caution: The first domain in the array gets used as the folder name | ||
|
||
// The email address to use for the ACME account | ||
const email = ['[email protected]'] | ||
|
||
console.log('Obtaining certificate...') | ||
|
||
// Obtaining a new certificate | ||
await cert.obtain({ | ||
domains, | ||
}) | ||
|
||
console.log('Certificate obtained') | ||
|
||
console.log('Renewing certificate...') | ||
|
||
// Renew the certificate, forcing the renewal even if the certificate is not expired | ||
await cert.renew({ | ||
domains, | ||
email, | ||
force: true, // Force the renewal, even if the certificate is not expired | ||
revoke: true // Revoke the old certificate after renewal | ||
}) | ||
|
||
console.log('Certificate renewed') | ||
|
||
console.log('Revoking certificate...') | ||
|
||
// Revoke the issued certificate | ||
const certificate = readFile({ | ||
filename: 'cert.pem', | ||
folder: domains[0] | ||
}) | ||
|
||
const privateKey = readFile({ | ||
filename: 'privkey.pem', | ||
folder: domains[0] | ||
}) | ||
|
||
await cert.revoke(certificate, privateKey) | ||
|
||
console.log('Certificate revoked') | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.