Skip to content

Commit

Permalink
Refactor revoke method, add examples, and update README
Browse files Browse the repository at this point in the history
- 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
GeisonJr committed Aug 25, 2024
1 parent 9ae7596 commit 340058a
Show file tree
Hide file tree
Showing 7 changed files with 878 additions and 384 deletions.
60 changes: 37 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
58 changes: 58 additions & 0 deletions example/index.ts
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,
email
})

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)
}
})()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@geisonjr/certfy",
"version": "1.0.0",
"version": "1.0.1",
"author": "GeisonJr",
"license": "MIT",
"description": "Certfy is a simple and easy-to-use library to generate SSL certificates using Let's Encrypt.",
Expand All @@ -14,6 +14,7 @@
},
"scripts": {
"build": "npm run script:clean && npm run tsc:build",
"example": "ts-node ./example/index.ts",
"prepack": "npm run build",
"script:clean": "node ./scripts/clean.js",
"tsc:build": "tsc -p ./tsconfig.json"
Expand Down
Loading

0 comments on commit 340058a

Please sign in to comment.