From e79ebc242993c1ee5083efc0e9b0bc9ebbe5c999 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 3 Feb 2023 16:45:13 +0000 Subject: [PATCH 1/8] Add helper utility to compress data. --- package.json | 1 + pnpm-lock.yaml | 7 +++++++ src/utils/compressionUtils.js | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 src/utils/compressionUtils.js diff --git a/package.json b/package.json index 8519e11d..504d6b1b 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "linkify-html": "4.1.1", "linkifyjs": "4.1.1", "mux.js": "6.3.0", + "pako": "2.1.0", "qrcode": "^1.5.3", "shaka-player": "4.4.1", "stream-browserify": "3.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5375c102..4ecea5c9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,9 @@ dependencies: mux.js: specifier: 6.3.0 version: 6.3.0 + pako: + specifier: 2.1.0 + version: 2.1.0 qrcode: specifier: ^1.5.3 version: 1.5.3 @@ -4177,6 +4180,10 @@ packages: engines: {node: '>=6'} dev: false + /pako@2.1.0: + resolution: {integrity: sha512-w+eufiZ1WuJYgPXbV/PO3NCMEc3xqylkKHzp8bxp1uW4qaSNQUkwmLLEc3kKsfz8lpV1F8Ht3U1Cm+9Srog2ug==} + dev: false + /parent-module@1.0.1: resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==} engines: {node: '>=6'} diff --git a/src/utils/compressionUtils.js b/src/utils/compressionUtils.js new file mode 100644 index 00000000..5f482c2e --- /dev/null +++ b/src/utils/compressionUtils.js @@ -0,0 +1,18 @@ +export const compressGzip = async data => { + // Firefox does not support CompressionStream yet + if (typeof CompressionStream !== "undefined") { + let bytes = new TextEncoder().encode(data); + // eslint-disable-next-line no-undef + const cs = new CompressionStream("gzip"); + const writer = cs.writable.getWriter(); + writer.write(bytes); + writer.close(); + const compAb = await new Response(cs.readable).arrayBuffer(); + bytes = new Uint8Array(compAb); + + return bytes; + } else { + const pako = require("pako"); + return pako.gzip(data); + } +}; From bd7de9f94eb6e681610505eb484e90eb68acd5af Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 3 Feb 2023 18:18:53 +0000 Subject: [PATCH 2/8] Add utilities to encrypt text. --- src/utils/encryptionUtils.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/utils/encryptionUtils.js diff --git a/src/utils/encryptionUtils.js b/src/utils/encryptionUtils.js new file mode 100644 index 00000000..20ebd3a5 --- /dev/null +++ b/src/utils/encryptionUtils.js @@ -0,0 +1,19 @@ +// These functions accept and return Uint8Arrays + +export async function encryptAESGCM(plaintext, key) { + const iv = crypto.getRandomValues(new Uint8Array(12)); + const algorithm = { name: "AES-GCM", iv: iv }; + const keyMaterial = await crypto.subtle.importKey("raw", key, algorithm, false, ["encrypt"]); + const ciphertext = await crypto.subtle.encrypt(algorithm, keyMaterial, plaintext); + + return new Uint8Array([...iv, ...new Uint8Array(ciphertext)]); +} + +export async function decryptAESGCM(ciphertextArray, key) { + const iv = new Uint8Array(ciphertextArray.slice(0, 12)); + const algorithm = { name: "AES-GCM", iv: iv }; + const keyMaterial = await crypto.subtle.importKey("raw", key, algorithm, false, ["decrypt"]); + const decrypted = await crypto.subtle.decrypt(algorithm, keyMaterial, new Uint8Array(ciphertextArray.slice(12))); + + return decrypted; +} From 89e8a3b80e427b2a4791e9d69768f2e27065199c Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Thu, 9 Feb 2023 01:05:54 +0000 Subject: [PATCH 3/8] Add key generation and base64 encoding and decoding utils. --- src/utils/encryptionUtils.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/utils/encryptionUtils.js b/src/utils/encryptionUtils.js index 20ebd3a5..a75cbb41 100644 --- a/src/utils/encryptionUtils.js +++ b/src/utils/encryptionUtils.js @@ -17,3 +17,20 @@ export async function decryptAESGCM(ciphertextArray, key) { return decrypted; } + +export async function generateKey() { + const algorithm = { name: "AES-GCM", length: 256 }; + const key = await crypto.subtle.generateKey(algorithm, true, ["encrypt", "decrypt"]); + + return await crypto.subtle.exportKey("raw", key); +} + +export function encodeArrayToBase64(array) { + const chars = String.fromCharCode.apply(null, array); + return btoa(chars); +} + +export function decodeBase64ToArray(base64) { + const chars = atob(base64); + return new Uint8Array(chars.split("").map(c => c.charCodeAt(0))); +} From 8901a97fcbb8cb78c38729a9281a1ef5c026313d Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 20 Feb 2023 11:46:33 +0000 Subject: [PATCH 4/8] Add optional allowQuery parameter. --- src/main.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main.js b/src/main.js index 37148852..8bcade0c 100644 --- a/src/main.js +++ b/src/main.js @@ -125,10 +125,10 @@ const mixin = { if (!disableAlert) alert(this.$t("info.local_storage")); } }, - getPreferenceBoolean(key, defaultVal) { + getPreferenceBoolean(key, defaultVal, allowQuery = true) { var value; if ( - (value = new URLSearchParams(window.location.search).get(key)) !== null || + (allowQuery && (value = new URLSearchParams(window.location.search).get(key)) !== null) || (this.testLocalStorage && (value = localStorage.getItem(key)) !== null) ) { switch (String(value).toLowerCase()) { @@ -142,29 +142,29 @@ const mixin = { } } else return defaultVal; }, - getPreferenceString(key, defaultVal) { + getPreferenceString(key, defaultVal, allowQuery = true) { var value; if ( - (value = new URLSearchParams(window.location.search).get(key)) !== null || + (allowQuery && (value = new URLSearchParams(window.location.search).get(key)) !== null) || (this.testLocalStorage && (value = localStorage.getItem(key)) !== null) ) { return value; } else return defaultVal; }, - getPreferenceNumber(key, defaultVal) { + getPreferenceNumber(key, defaultVal, allowQuery = true) { var value; if ( - (value = new URLSearchParams(window.location.search).get(key)) !== null || + (allowQuery && (value = new URLSearchParams(window.location.search).get(key)) !== null) || (this.testLocalStorage && (value = localStorage.getItem(key)) !== null) ) { const num = Number(value); return isNaN(num) ? defaultVal : num; } else return defaultVal; }, - getPreferenceJSON(key, defaultVal) { + getPreferenceJSON(key, defaultVal, allowQuery = true) { var value; if ( - (value = new URLSearchParams(window.location.search).get(key)) !== null || + (allowQuery && (value = new URLSearchParams(window.location.search).get(key)) !== null) || (this.testLocalStorage && (value = localStorage.getItem(key)) !== null) ) { return JSON.parse(value); From 12650448543fc3d3ebf6dc63754b68b6090e81e5 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 20 Feb 2023 12:01:28 +0000 Subject: [PATCH 5/8] Move frontend config loading to App.vue. --- src/App.vue | 11 ++++++++++- src/components/FooterComponent.vue | 29 ++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/App.vue b/src/App.vue index 551267a5..49004ade 100644 --- a/src/App.vue +++ b/src/App.vue @@ -9,7 +9,7 @@ - + @@ -27,6 +27,7 @@ export default { data() { return { theme: "dark", + config: null, }; }, mounted() { @@ -35,6 +36,14 @@ export default { this.setTheme(); }); + this.fetchJson(this.authApiUrl() + "/config") + .then(config => { + this.config = config; + }) + .then(() => { + this.onConfigLoaded(); + }); + if ("indexedDB" in window) { const request = indexedDB.open("piped-db", 5); request.onupgradeneeded = ev => { diff --git a/src/components/FooterComponent.vue b/src/components/FooterComponent.vue index 38212347..c267b440 100644 --- a/src/components/FooterComponent.vue +++ b/src/components/FooterComponent.vue @@ -29,6 +29,15 @@ diff --git a/src/components/FooterComponent.vue b/src/components/FooterComponent.vue index c267b440..f0441678 100644 --- a/src/components/FooterComponent.vue +++ b/src/components/FooterComponent.vue @@ -29,15 +29,12 @@