Load VAPID keys from configuration

This commit is contained in:
Alex Gleason
2024-10-08 19:57:46 -05:00
parent 8f437839d0
commit 198ec973b6
8 changed files with 79 additions and 28 deletions

View File

@@ -1,3 +1,5 @@
import { generateVapidKeys } from '@negrel/webpush';
import { encodeBase64 } from '@std/encoding/base64';
import { exists } from '@std/fs/exists';
import { generateSecretKey, nip19 } from 'nostr-tools';
import question from 'question-deno';
@@ -95,6 +97,15 @@ if (vars.DITTO_UPLOADER === 'local') {
vars.MEDIA_DOMAIN = `https://${mediaDomain}`;
}
const VAPID_PRIVATE_KEY = Deno.env.get('VAPID_PRIVATE_KEY');
if (VAPID_PRIVATE_KEY) {
vars.VAPID_PRIVATE_KEY = VAPID_PRIVATE_KEY;
} else {
const { privateKey } = await generateVapidKeys({ extractable: true });
const bytes = await crypto.subtle.exportKey('pkcs8', privateKey);
vars.VAPID_PRIVATE_KEY = encodeBase64(bytes);
}
console.log('Writing to .env file...');
const result = Object.entries(vars).reduce((acc, [key, value]) => {

7
scripts/vapid.ts Normal file
View File

@@ -0,0 +1,7 @@
import { generateVapidKeys } from '@negrel/webpush';
import { encodeBase64 } from '@std/encoding/base64';
const { privateKey } = await generateVapidKeys({ extractable: true });
const bytes = await crypto.subtle.exportKey('pkcs8', privateKey);
console.log(encodeBase64(bytes));