From f14b3cec97bec5a320c049b2b58b520d1b03e7ca Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 13 Oct 2023 19:17:30 +0200 Subject: [PATCH] [backend] Automatically generate vapid/web-push keys Co-authored-by: Johann150 (https://akkoma.dev/FoundKeyGang/FoundKey/commit/563f3672a95970c04850b5ae10fb23d6c1521e04) --- docs/post-install.md | 6 --- .../1697216726757-auto-generate-vapid-keys.ts | 26 ++++++++++ packages/backend/src/models/entities/meta.ts | 17 +++---- .../src/server/api/endpoints/admin/meta.ts | 6 --- .../server/api/endpoints/admin/update-meta.ts | 5 -- .../backend/src/server/api/endpoints/meta.ts | 13 ++--- packages/backend/src/server/nodeinfo.ts | 1 - .../backend/src/services/push-notification.ts | 7 --- packages/client/src/pages/admin/settings.vue | 47 ------------------- packages/iceshrimp-js/src/entities.ts | 1 - 10 files changed, 35 insertions(+), 94 deletions(-) create mode 100644 packages/backend/src/migration/1697216726757-auto-generate-vapid-keys.ts diff --git a/docs/post-install.md b/docs/post-install.md index e9a97a2fa..c7353414b 100644 --- a/docs/post-install.md +++ b/docs/post-install.md @@ -15,12 +15,6 @@ This document describes things you can do after successfully installing Iceshrim - Install [LibreTranslate](https://libretranslate.com/) - Get an API URL and API key, copy and paste them into Control Panel > General > Libre Translate -## Enabling push notifications - -- Run `npx web-push generate-vapid-keys` - - Docker Compose users: `docker compose exec web npx web-push generate-vapid-keys` -- Put public and private keys into Control Panel > General > Service Worker - ## Object Storage (S3) Recommended if using Docker diff --git a/packages/backend/src/migration/1697216726757-auto-generate-vapid-keys.ts b/packages/backend/src/migration/1697216726757-auto-generate-vapid-keys.ts new file mode 100644 index 000000000..a415e0431 --- /dev/null +++ b/packages/backend/src/migration/1697216726757-auto-generate-vapid-keys.ts @@ -0,0 +1,26 @@ +import { MigrationInterface, QueryRunner } from "typeorm" +import push from 'web-push'; + +export class AutoGenerateVapidKeys1697216726757 implements MigrationInterface { + // Based on FoundKey's 1668374092227-forceEnablePush.js + name = 'AutoGenerateVapidKeys1697216726757'; + + public async up(queryRunner: QueryRunner): Promise { + // set VAPID keys if not yet set + const { publicKey, privateKey } = push.generateVAPIDKeys(); + await queryRunner.query(`UPDATE "meta" SET "swPublicKey" = $1, "swPrivateKey" = $2 WHERE "swPublicKey" IS NULL OR "swPrivateKey" IS NULL`, [publicKey, privateKey]); + + await queryRunner.query(`ALTER TABLE "meta" DROP COLUMN "enableServiceWorker"`); + await queryRunner.query(`ALTER TABLE "meta" ALTER COLUMN "swPublicKey" SET NOT NULL`); + await queryRunner.query(`ALTER TABLE "meta" ALTER COLUMN "swPrivateKey" SET NOT NULL`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "meta" ALTER COLUMN "swPrivateKey" DROP NOT NULL`); + await queryRunner.query(`ALTER TABLE "meta" ALTER COLUMN "swPublicKey" DROP NOT NULL`); + await queryRunner.query(`ALTER TABLE "meta" ADD "enableServiceWorker" boolean NOT NULL DEFAULT false`); + // since VAPID keys are set and the service worker may have been enabled before, make sure it is now enabled + await queryRunner.query(`UPDATE "meta" SET "enableServiceWorker" = true`); + // can't unset the VAPID keys because we do not know if we set them in the migration + } +} diff --git a/packages/backend/src/models/entities/meta.ts b/packages/backend/src/models/entities/meta.ts index 530c1c677..6c0d8edb8 100644 --- a/packages/backend/src/models/entities/meta.ts +++ b/packages/backend/src/models/entities/meta.ts @@ -337,22 +337,17 @@ export class Meta { }) public smtpPass: string | null; - @Column("boolean", { - default: false, + @Column("varchar", { + length: 128, + nullable: false, }) - public enableServiceWorker: boolean; + public swPublicKey: string; @Column("varchar", { length: 128, - nullable: true, + nullable: false, }) - public swPublicKey: string | null; - - @Column("varchar", { - length: 128, - nullable: true, - }) - public swPrivateKey: string | null; + public swPrivateKey: string; @Column("boolean", { default: false, diff --git a/packages/backend/src/server/api/endpoints/admin/meta.ts b/packages/backend/src/server/api/endpoints/admin/meta.ts index 0f3a049c2..c1588f648 100644 --- a/packages/backend/src/server/api/endpoints/admin/meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/meta.ts @@ -156,11 +156,6 @@ export const meta = { optional: false, nullable: false, }, - enableServiceWorker: { - type: "boolean", - optional: false, - nullable: false, - }, translatorAvailable: { type: "boolean", optional: false, @@ -518,7 +513,6 @@ export default define(meta, paramDef, async (ps, me) => { enableTwitterIntegration: instance.enableTwitterIntegration, enableGithubIntegration: instance.enableGithubIntegration, enableDiscordIntegration: instance.enableDiscordIntegration, - enableServiceWorker: instance.enableServiceWorker, translatorAvailable: instance.deeplAuthKey != null || instance.libreTranslateApiUrl != null, pinnedPages: instance.pinnedPages, diff --git a/packages/backend/src/server/api/endpoints/admin/update-meta.ts b/packages/backend/src/server/api/endpoints/admin/update-meta.ts index 2142c7df7..2fabe681e 100644 --- a/packages/backend/src/server/api/endpoints/admin/update-meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/update-meta.ts @@ -148,7 +148,6 @@ export const paramDef = { smtpPort: { type: "integer", nullable: true }, smtpUser: { type: "string", nullable: true }, smtpPass: { type: "string", nullable: true }, - enableServiceWorker: { type: "boolean" }, swPublicKey: { type: "string", nullable: true }, swPrivateKey: { type: "string", nullable: true }, tosUrl: { type: "string", nullable: true }, @@ -463,10 +462,6 @@ export default define(meta, paramDef, async (ps, me) => { set.errorImageUrl = ps.errorImageUrl; } - if (ps.enableServiceWorker !== undefined) { - set.enableServiceWorker = ps.enableServiceWorker; - } - if (ps.swPublicKey !== undefined) { set.swPublicKey = ps.swPublicKey; } diff --git a/packages/backend/src/server/api/endpoints/meta.ts b/packages/backend/src/server/api/endpoints/meta.ts index a80fcd5f5..52319a383 100644 --- a/packages/backend/src/server/api/endpoints/meta.ts +++ b/packages/backend/src/server/api/endpoints/meta.ts @@ -266,11 +266,6 @@ export const meta = { optional: false, nullable: false, }, - enableServiceWorker: { - type: "boolean", - optional: false, - nullable: false, - }, translatorAvailable: { type: "boolean", optional: false, @@ -377,13 +372,13 @@ export const meta = { }, defaultReaction: { type: "string", - optional: "false", + optional: false, nullable: false, default: "⭐", }, donationLink: { type: "string", - optional: "true", + optional: true, nullable: true, }, }, @@ -472,8 +467,6 @@ export default define(meta, paramDef, async (ps, me) => { enableGithubIntegration: instance.enableGithubIntegration, enableDiscordIntegration: instance.enableDiscordIntegration, - enableServiceWorker: instance.enableServiceWorker, - translatorAvailable: instance.deeplAuthKey != null || instance.libreTranslateApiUrl != null, defaultReaction: instance.defaultReaction, @@ -515,7 +508,7 @@ export default define(meta, paramDef, async (ps, me) => { twitter: instance.enableTwitterIntegration, github: instance.enableGithubIntegration, discord: instance.enableDiscordIntegration, - serviceWorker: instance.enableServiceWorker, + serviceWorker: true, postEditing: true, postImports: instance.experimentalFeatures?.postImports || false, miauth: true, diff --git a/packages/backend/src/server/nodeinfo.ts b/packages/backend/src/server/nodeinfo.ts index 0a042f365..6089cfb5b 100644 --- a/packages/backend/src/server/nodeinfo.ts +++ b/packages/backend/src/server/nodeinfo.ts @@ -93,7 +93,6 @@ const nodeinfo2 = async () => { enableGithubIntegration: meta.enableGithubIntegration, enableDiscordIntegration: meta.enableDiscordIntegration, enableEmail: meta.enableEmail, - enableServiceWorker: meta.enableServiceWorker, proxyAccountName: proxyAccount ? proxyAccount.username : null, themeColor: meta.themeColor || "#31748f", }, diff --git a/packages/backend/src/services/push-notification.ts b/packages/backend/src/services/push-notification.ts index a207fae39..537cf45c0 100644 --- a/packages/backend/src/services/push-notification.ts +++ b/packages/backend/src/services/push-notification.ts @@ -47,13 +47,6 @@ export async function pushNotification( ) { const meta = await fetchMeta(); - if ( - !meta.enableServiceWorker || - meta.swPublicKey == null || - meta.swPrivateKey == null - ) - return; - // アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録 push.setVapidDetails(config.url, meta.swPublicKey, meta.swPrivateKey); diff --git a/packages/client/src/pages/admin/settings.vue b/packages/client/src/pages/admin/settings.vue index a33335089..8875c2723 100644 --- a/packages/client/src/pages/admin/settings.vue +++ b/packages/client/src/pages/admin/settings.vue @@ -317,44 +317,6 @@ - - - - - - - - - - -