Skip to content

Commit bea49da

Browse files
committed
chore: make use of config
1 parent 62a4fa3 commit bea49da

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

.vscode/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ keychain
2323
Keychains
2424
Keypair
2525
lockb
26+
Millis
2627
mkcert
2728
openweb
2829
outdir

src/certificate.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ export function getCertNotBefore(): Date {
3333
* @returns The Not After Date for the Certificate
3434
*/
3535
export function getCertNotAfter(notBefore: Date): Date {
36-
const ninetyDaysLater = new Date(notBefore.getTime() + 60 * 60 * 24 * 90 * 1000)
37-
const year = ninetyDaysLater.getFullYear()
38-
const month = (ninetyDaysLater.getMonth() + 1).toString().padStart(2, '0')
39-
const day = ninetyDaysLater.getDate().toString().padStart(2, '0')
36+
const validityDays = config.validityDays // defaults to 180 days
37+
const daysInMillis = validityDays * 60 * 60 * 24 * 1000
38+
const notAfterDate = new Date(notBefore.getTime() + daysInMillis)
39+
const year = notAfterDate.getFullYear()
40+
const month = (notAfterDate.getMonth() + 1).toString().padStart(2, '0')
41+
const day = notAfterDate.getDate().toString().padStart(2, '0')
4042

4143
return new Date(`${year}-${month}-${day}T23:59:59Z`)
4244
}
@@ -54,11 +56,6 @@ export function getCANotAfter(notBefore: Date): Date {
5456
return new Date(`${year}-${month}-${day}T23:59:59Z`)
5557
}
5658

57-
export const DEFAULT_C = 'US'
58-
export const DEFAULT_ST = 'California'
59-
export const DEFAULT_L = 'Playa Vista'
60-
export const DEFAULT_O: string = config?.organizationName ?? 'stacksjs.org'
61-
6259
/**
6360
* Create a new Root CA Certificate
6461
* @returns The Root CA Certificate
@@ -69,10 +66,10 @@ export async function createRootCA(): Promise<GenerateCertReturn> {
6966

7067
// Define the attributes for the new Root CA
7168
const attributes = [
72-
{ shortName: 'C', value: DEFAULT_C },
73-
{ shortName: 'ST', value: DEFAULT_ST },
74-
{ shortName: 'L', value: DEFAULT_L },
75-
{ shortName: 'CN', value: DEFAULT_O },
69+
{ shortName: 'C', value: config.countryName },
70+
{ shortName: 'ST', value: config.stateName },
71+
{ shortName: 'L', value: config.localityName },
72+
{ shortName: 'CN', value: config.commonName },
7673
]
7774

7875
const extensions = [
@@ -132,10 +129,10 @@ export async function generateCert(options?: CertOption): Promise<GenerateCertRe
132129

133130
// Define the attributes/properties for the Host Certificate
134131
const attributes = [
135-
{ shortName: 'C', value: DEFAULT_C },
136-
{ shortName: 'ST', value: DEFAULT_ST },
137-
{ shortName: 'L', value: DEFAULT_L },
138-
{ shortName: 'CN', value: DEFAULT_O },
132+
{ shortName: 'C', value: config.countryName },
133+
{ shortName: 'ST', value: config.stateName },
134+
{ shortName: 'L', value: config.localityName },
135+
{ shortName: 'CN', value: config.commonName },
139136
]
140137

141138
const extensions = [
@@ -205,15 +202,15 @@ export async function addCertToSystemTrustStoreAndSaveCerts(
205202
for (const folder of foldersWithFile) {
206203
try {
207204
// delete existing cert from system trust store
208-
await runCommand(`certutil -d sql:${folder} -D -n ${DEFAULT_O}`)
205+
await runCommand(`certutil -d sql:${folder} -D -n ${config.commonName}`)
209206
}
210207
catch (error) {
211208
// ignore error if no cert exists
212209
console.warn(`Error deleting existing cert: ${error}`)
213210
}
214211

215212
// add new cert to system trust store
216-
await runCommand(`certutil -d sql:${folder} -A -t ${args} -n ${DEFAULT_O} -i ${caCertPath}`)
213+
await runCommand(`certutil -d sql:${folder} -A -t ${args} -n ${config.commonName} -i ${caCertPath}`)
217214

218215
log.info(`Cert added to ${folder}`)
219216
}
@@ -222,13 +219,13 @@ export async function addCertToSystemTrustStoreAndSaveCerts(
222219
// `sudo cp ${certPath} /usr/local/share/ca-certificates/`,
223220

224221
// // add new cert to system trust store
225-
// `certutil -d sql:${os.homedir()}/.pki/nssdb -A -t ${args} -n ${DEFAULT_O} -i ${caCertPath}`,
222+
// `certutil -d sql:${os.homedir()}/.pki/nssdb -A -t ${args} -n ${config.commonName} -i ${caCertPath}`,
226223

227224
// // add new cert to system trust store for Brave
228-
// `certutil -d sql:${os.homedir()}/snap/brave/411/.pki/nssdb -A -t ${args} -n ${DEFAULT_O} -i ${caCertPath}`,
225+
// `certutil -d sql:${os.homedir()}/snap/brave/411/.pki/nssdb -A -t ${args} -n ${config.commonName} -i ${caCertPath}`,
229226

230227
// // add new cert to system trust store for Firefox
231-
// `certutil -d sql:${os.homedir()}/snap/firefox/common/.mozilla/firefox/3l148raz.default -A -t ${args} -n ${DEFAULT_O} -i ${caCertPath}`,
228+
// `certutil -d sql:${os.homedir()}/snap/firefox/common/.mozilla/firefox/3l148raz.default -A -t ${args} -n ${config.commonName} -i ${caCertPath}`,
232229

233230
// // reload system trust store
234231
// `sudo update-ca-certificates`,

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const config: TlsConfig = await loadConfig({
1616
stateName: 'California',
1717
localityName: 'Playa Vista',
1818
commonName: 'stacks.localhost',
19-
validityDays: 1,
19+
validityDays: 180,
2020
hostCertCN: 'stacks.localhost',
2121
domain: 'localhost',
2222
rootCAObject: { certificate: '', privateKey: '' },

0 commit comments

Comments
 (0)