decrypt notes
This commit is contained in:
24
package-lock.json
generated
24
package-lock.json
generated
@@ -24,6 +24,7 @@
|
|||||||
"@types/marked": "^4.0.8",
|
"@types/marked": "^4.0.8",
|
||||||
"@vueuse/components": "^10.1.0",
|
"@vueuse/components": "^10.1.0",
|
||||||
"@vueuse/core": "^10.1.2",
|
"@vueuse/core": "^10.1.2",
|
||||||
|
"crypto-js": "^4.1.1",
|
||||||
"cytoscape": "^3.25.0",
|
"cytoscape": "^3.25.0",
|
||||||
"daisyui": "^2.51.6",
|
"daisyui": "^2.51.6",
|
||||||
"date-fns": "^2.29.3",
|
"date-fns": "^2.29.3",
|
||||||
@@ -40,6 +41,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rushstack/eslint-patch": "^1.2.0",
|
"@rushstack/eslint-patch": "^1.2.0",
|
||||||
"@tsconfig/node18": "^2.0.1",
|
"@tsconfig/node18": "^2.0.1",
|
||||||
|
"@types/crypto-js": "^4.1.1",
|
||||||
"@types/dompurify": "^3.0.2",
|
"@types/dompurify": "^3.0.2",
|
||||||
"@types/lodash-es": "^4.17.7",
|
"@types/lodash-es": "^4.17.7",
|
||||||
"@types/node": "^20.2.1",
|
"@types/node": "^20.2.1",
|
||||||
@@ -2002,6 +2004,12 @@
|
|||||||
"integrity": "sha512-UqdfvuJK0SArA2CxhKWwwAWfnVSXiYe63bVpMutc27vpngCntGUZQETO24pEJ46zU6XM+7SpqYoMgcO3bM11Ew==",
|
"integrity": "sha512-UqdfvuJK0SArA2CxhKWwwAWfnVSXiYe63bVpMutc27vpngCntGUZQETO24pEJ46zU6XM+7SpqYoMgcO3bM11Ew==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/crypto-js": {
|
||||||
|
"version": "4.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-4.1.1.tgz",
|
||||||
|
"integrity": "sha512-BG7fQKZ689HIoc5h+6D2Dgq1fABRa0RbBWKBd9SP/MVRVXROflpm5fhwyATX5duFmbStzyzyycPB8qUYKDH3NA==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"node_modules/@types/cytoscape": {
|
"node_modules/@types/cytoscape": {
|
||||||
"version": "3.19.9",
|
"version": "3.19.9",
|
||||||
"resolved": "https://registry.npmjs.org/@types/cytoscape/-/cytoscape-3.19.9.tgz",
|
"resolved": "https://registry.npmjs.org/@types/cytoscape/-/cytoscape-3.19.9.tgz",
|
||||||
@@ -4324,6 +4332,11 @@
|
|||||||
"node": ">= 8"
|
"node": ">= 8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/crypto-js": {
|
||||||
|
"version": "4.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.1.1.tgz",
|
||||||
|
"integrity": "sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw=="
|
||||||
|
},
|
||||||
"node_modules/crypto-random-string": {
|
"node_modules/crypto-random-string": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz",
|
||||||
@@ -13838,6 +13851,12 @@
|
|||||||
"integrity": "sha512-UqdfvuJK0SArA2CxhKWwwAWfnVSXiYe63bVpMutc27vpngCntGUZQETO24pEJ46zU6XM+7SpqYoMgcO3bM11Ew==",
|
"integrity": "sha512-UqdfvuJK0SArA2CxhKWwwAWfnVSXiYe63bVpMutc27vpngCntGUZQETO24pEJ46zU6XM+7SpqYoMgcO3bM11Ew==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"@types/crypto-js": {
|
||||||
|
"version": "4.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-4.1.1.tgz",
|
||||||
|
"integrity": "sha512-BG7fQKZ689HIoc5h+6D2Dgq1fABRa0RbBWKBd9SP/MVRVXROflpm5fhwyATX5duFmbStzyzyycPB8qUYKDH3NA==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"@types/cytoscape": {
|
"@types/cytoscape": {
|
||||||
"version": "3.19.9",
|
"version": "3.19.9",
|
||||||
"resolved": "https://registry.npmjs.org/@types/cytoscape/-/cytoscape-3.19.9.tgz",
|
"resolved": "https://registry.npmjs.org/@types/cytoscape/-/cytoscape-3.19.9.tgz",
|
||||||
@@ -15593,6 +15612,11 @@
|
|||||||
"which": "^2.0.1"
|
"which": "^2.0.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"crypto-js": {
|
||||||
|
"version": "4.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.1.1.tgz",
|
||||||
|
"integrity": "sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw=="
|
||||||
|
},
|
||||||
"crypto-random-string": {
|
"crypto-random-string": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/crypto-random-string/-/crypto-random-string-2.0.0.tgz",
|
||||||
|
|||||||
@@ -31,6 +31,7 @@
|
|||||||
"@types/marked": "^4.0.8",
|
"@types/marked": "^4.0.8",
|
||||||
"@vueuse/components": "^10.1.0",
|
"@vueuse/components": "^10.1.0",
|
||||||
"@vueuse/core": "^10.1.2",
|
"@vueuse/core": "^10.1.2",
|
||||||
|
"crypto-js": "^4.1.1",
|
||||||
"cytoscape": "^3.25.0",
|
"cytoscape": "^3.25.0",
|
||||||
"daisyui": "^2.51.6",
|
"daisyui": "^2.51.6",
|
||||||
"date-fns": "^2.29.3",
|
"date-fns": "^2.29.3",
|
||||||
@@ -47,6 +48,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rushstack/eslint-patch": "^1.2.0",
|
"@rushstack/eslint-patch": "^1.2.0",
|
||||||
"@tsconfig/node18": "^2.0.1",
|
"@tsconfig/node18": "^2.0.1",
|
||||||
|
"@types/crypto-js": "^4.1.1",
|
||||||
"@types/dompurify": "^3.0.2",
|
"@types/dompurify": "^3.0.2",
|
||||||
"@types/lodash-es": "^4.17.7",
|
"@types/lodash-es": "^4.17.7",
|
||||||
"@types/node": "^20.2.1",
|
"@types/node": "^20.2.1",
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ const emit = defineEmits<{
|
|||||||
const results = computed<Note[]>(() => {
|
const results = computed<Note[]>(() => {
|
||||||
return (
|
return (
|
||||||
props.autocompleteText ? findNotesByByTitle(props.autocompleteText) : notes.value
|
props.autocompleteText ? findNotesByByTitle(props.autocompleteText) : notes.value
|
||||||
).filter((note) => note.id !== activeNote.value?.id)
|
).filter((note) => note.id !== activeNote.value?.id).slice(10)
|
||||||
})
|
})
|
||||||
|
|
||||||
const activeResult = ref<Note>()
|
const activeResult = ref<Note>()
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ const props = defineProps<{
|
|||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
goToNote: []
|
goToNote: []
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
|
console.log(props.activeResult)
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
<li class="flex flex-row">
|
<li class="flex flex-row">
|
||||||
|
|||||||
@@ -57,7 +57,8 @@ const renderMindmap = () => {
|
|||||||
// A function that determines whether the node should be animated
|
// A function that determines whether the node should be animated
|
||||||
// All nodes animated by default on animate enabled
|
// All nodes animated by default on animate enabled
|
||||||
// Non-animated nodes are positioned immediately when the layout starts
|
// Non-animated nodes are positioned immediately when the layout starts
|
||||||
animateFilter: function (node, i) {
|
// animateFilter: function (node, i) {
|
||||||
|
animateFilter: function () {
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -88,7 +89,8 @@ const renderMindmap = () => {
|
|||||||
componentSpacing: 40,
|
componentSpacing: 40,
|
||||||
|
|
||||||
// Node repulsion (non overlapping) multiplier
|
// Node repulsion (non overlapping) multiplier
|
||||||
nodeRepulsion: function (node) {
|
// nodeRepulsion: function (node) {
|
||||||
|
nodeRepulsion: function () {
|
||||||
return 2048
|
return 2048
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -96,12 +98,14 @@ const renderMindmap = () => {
|
|||||||
nodeOverlap: 4,
|
nodeOverlap: 4,
|
||||||
|
|
||||||
// Ideal edge (non nested) length
|
// Ideal edge (non nested) length
|
||||||
idealEdgeLength: function (edge) {
|
// idealEdgeLength: function (edge) {
|
||||||
|
idealEdgeLength: function () {
|
||||||
return 32
|
return 32
|
||||||
},
|
},
|
||||||
|
|
||||||
// Divisor to compute edge forces
|
// Divisor to compute edge forces
|
||||||
edgeElasticity: function (edge) {
|
// edgeElasticity: function (edge) {
|
||||||
|
edgeElasticity: function () {
|
||||||
return 32
|
return 32
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
44
src/composables/useEncryption.ts
Normal file
44
src/composables/useEncryption.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { doc, getDoc } from 'firebase/firestore'
|
||||||
|
import { user, db } from '@/composables/useFirebase'
|
||||||
|
import { decrypt, calculateClientKey } from '@/utils/crypto'
|
||||||
|
|
||||||
|
function getClientKeysFromLocalStorage() {
|
||||||
|
try {
|
||||||
|
return JSON.parse(localStorage.getItem('clientKeys') || '{}')
|
||||||
|
} catch (e) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getClientKey = (): ClientKey | void => {
|
||||||
|
if (!user.value) return
|
||||||
|
const clientKeys = getClientKeysFromLocalStorage()
|
||||||
|
const clientKey = clientKeys[user.value?.uid] || calculateClientKey('test')
|
||||||
|
return clientKey
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getEncryptionKey(): Promise<EncryptionKey | void> {
|
||||||
|
if (!user.value) return
|
||||||
|
const clientKey = getClientKey()
|
||||||
|
if (!db.value || !clientKey) return
|
||||||
|
const data = (await getDoc(doc(db.value, 'encryptionKeys', user.value?.uid || ''))).data()
|
||||||
|
if (!data) return
|
||||||
|
const { key } = data
|
||||||
|
const encryptionKey: EncryptionKey = decrypt(key, clientKey)
|
||||||
|
return encryptionKey
|
||||||
|
}
|
||||||
|
|
||||||
|
const decryptNote = (note: BaseNote, key: EncryptionKey) => {
|
||||||
|
return {
|
||||||
|
...note,
|
||||||
|
title: decrypt(note.title, key),
|
||||||
|
content: decrypt(note.content, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const decryptNotes = (notes: BaseNotes, encryptionKey: EncryptionKey) => {
|
||||||
|
const decryptedNotes = Object.fromEntries(
|
||||||
|
Object.entries(notes).map(([noteId, note]) => [noteId, { ...decryptNote(note, encryptionKey) }])
|
||||||
|
)
|
||||||
|
return decryptedNotes
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
// import { initializeApp } from 'firebase/app'
|
// import { initializeApp } from 'firebase/app'
|
||||||
import firebase from 'firebase/compat/app'
|
import firebase from 'firebase/compat/app'
|
||||||
import type { User } from '@firebase/auth-types'
|
import type { User } from '@firebase/auth-types'
|
||||||
|
import { getFirestore } from 'firebase/firestore'
|
||||||
|
import type { Firestore } from 'firebase/firestore'
|
||||||
|
|
||||||
// import { getAnalytics } from "firebase/analytics";
|
// import { getAnalytics } from "firebase/analytics";
|
||||||
// TODO: Add SDKs for Firebase products that you want to use
|
// TODO: Add SDKs for Firebase products that you want to use
|
||||||
@@ -21,14 +23,17 @@ const firebaseConfig = {
|
|||||||
|
|
||||||
export const user = ref<User | null>()
|
export const user = ref<User | null>()
|
||||||
|
|
||||||
// Initialize Firebase
|
|
||||||
export const initializeFirebase = () => {
|
|
||||||
firebase.initializeApp(firebaseConfig)
|
|
||||||
firebase.auth().onAuthStateChanged((firebaseUser) => {
|
|
||||||
user.value = firebaseUser
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export const initialized = computed<boolean>(() => user.value !== undefined)
|
export const initialized = computed<boolean>(() => user.value !== undefined)
|
||||||
|
|
||||||
export const signOut = () => firebase.auth().signOut()
|
export const signOut = () => firebase.auth().signOut()
|
||||||
|
|
||||||
|
export const db = ref<Firestore>()
|
||||||
|
|
||||||
|
// Initialize Firebase
|
||||||
|
export const initializeFirebase = () => {
|
||||||
|
const app = firebase.initializeApp(firebaseConfig)
|
||||||
|
firebase.auth().onAuthStateChanged((firebaseUser) => {
|
||||||
|
user.value = firebaseUser
|
||||||
|
})
|
||||||
|
db.value = getFirestore(app)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { defaultNotes } from '@/utils/defaultNotes'
|
import shortid from 'shortid'
|
||||||
import { viewModes, activeViewMode } from '@/composables/useViewMode'
|
|
||||||
import { useTitle } from '@vueuse/core'
|
import { useTitle } from '@vueuse/core'
|
||||||
|
import { doc, getDoc } from 'firebase/firestore'
|
||||||
|
import { viewModes, activeViewMode } from '@/composables/useViewMode'
|
||||||
|
import { initialized, user, db } from '@/composables/useFirebase'
|
||||||
|
import { decryptNotes, getEncryptionKey } from '@/composables/useEncryption'
|
||||||
|
import { defaultNotes } from '@/utils/defaultNotes'
|
||||||
import { mdToHtml } from '@/utils/markdown'
|
import { mdToHtml } from '@/utils/markdown'
|
||||||
import { getAllMatches } from '@/utils/helpers'
|
import { getAllMatches } from '@/utils/helpers'
|
||||||
import { initialized, user } from '@/composables/useFirebase'
|
|
||||||
import shortid from 'shortid'
|
|
||||||
|
|
||||||
const notesSources = computed(() => ({
|
const notesSources = computed(() => ({
|
||||||
local: true,
|
local: true,
|
||||||
@@ -188,21 +190,45 @@ export function getNoteReferences(note: Note) {
|
|||||||
: []
|
: []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const parseBaseNotes = (notes: BaseNotes): BaseNotes => {
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(notes).map(([noteId, note]) => {
|
||||||
|
return [
|
||||||
|
noteId,
|
||||||
|
{
|
||||||
|
id: noteId,
|
||||||
|
title: note.title,
|
||||||
|
content: note.content,
|
||||||
|
created: note.created,
|
||||||
|
modified: note.modified,
|
||||||
|
isRoot: note.isRoot
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
activeNotesSource,
|
activeNotesSource,
|
||||||
() => {
|
async () => {
|
||||||
if (!activeNotesSource.value) return
|
if (!activeNotesSource.value) return
|
||||||
baseNotes.value = {}
|
baseNotes.value = {}
|
||||||
|
let notes: BaseNotes = {}
|
||||||
if (activeNotesSource.value === 'local') {
|
if (activeNotesSource.value === 'local') {
|
||||||
try {
|
try {
|
||||||
const localNotes = JSON.parse(localStorage.getItem('notes') || '{}')
|
notes = JSON.parse(localStorage.getItem('notes') || '{}')
|
||||||
baseNotes.value = localNotes
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
}
|
}
|
||||||
} else if (activeNotesSource.value === 'firebase') {
|
} else if (activeNotesSource.value === 'firebase' && db.value) {
|
||||||
console.log('get notes from firebase')
|
const firebaseNotes = (
|
||||||
|
await getDoc(doc(db.value, 'pages', user.value?.uid || ''))
|
||||||
|
).data() as { [noteId: string]: any }
|
||||||
|
const encryptionKey = await getEncryptionKey()
|
||||||
|
notes = encryptionKey ? decryptNotes(firebaseNotes, encryptionKey) : firebaseNotes
|
||||||
|
console.log('get notes from firebase', notes)
|
||||||
}
|
}
|
||||||
|
baseNotes.value = parseBaseNotes(notes)
|
||||||
if (!rootNote.value) insertDefaultNotes(defaultNotes)
|
if (!rootNote.value) insertDefaultNotes(defaultNotes)
|
||||||
|
|
||||||
setActiveNote(rootNote.value?.id)
|
setActiveNote(rootNote.value?.id)
|
||||||
|
|||||||
7
src/types.d.ts
vendored
7
src/types.d.ts
vendored
@@ -12,6 +12,10 @@ declare global {
|
|||||||
wordCount: number
|
wordCount: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface BaseNotes {
|
||||||
|
[noteId: string]: BaseNote
|
||||||
|
}
|
||||||
|
|
||||||
interface ViewMode {
|
interface ViewMode {
|
||||||
name: string
|
name: string
|
||||||
icon: string
|
icon: string
|
||||||
@@ -36,5 +40,8 @@ declare global {
|
|||||||
from: string[]
|
from: string[]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ClientKey = string
|
||||||
|
type EncryptionKey = string
|
||||||
}
|
}
|
||||||
export {}
|
export {}
|
||||||
|
|||||||
15
src/utils/crypto.ts
Normal file
15
src/utils/crypto.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import CryptoJS from 'crypto-js'
|
||||||
|
|
||||||
|
const encryptionPrefix = 'contexted|'
|
||||||
|
const salt = 'salt'
|
||||||
|
|
||||||
|
export const calculateClientKey = (passphrase: string): ClientKey => {
|
||||||
|
return CryptoJS.PBKDF2(passphrase, salt, { keySize: 256 / 32 }).toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
export const decrypt = (encryptedMessage: string, key: string): string => {
|
||||||
|
const decryptedMessage = CryptoJS.AES.decrypt(encryptedMessage, key).toString(CryptoJS.enc.Utf8)
|
||||||
|
if (!decryptedMessage.startsWith(encryptionPrefix))
|
||||||
|
throw new Error("Message doesn't have valid encryption")
|
||||||
|
return decryptedMessage.substring(encryptionPrefix.length)
|
||||||
|
}
|
||||||
@@ -5,8 +5,7 @@ export const formatDate = (date: Date | number): string => {
|
|||||||
? new Date(date)
|
? new Date(date)
|
||||||
: date
|
: date
|
||||||
const dateDistanceInWords = formatDistance(dateToFormat, new Date()) + ' ago'
|
const dateDistanceInWords = formatDistance(dateToFormat, new Date()) + ' ago'
|
||||||
|
return isToday(date) ? dateDistanceInWords : format(date, 'd MMMM \'at\' HH:mm ')
|
||||||
return isToday(date) ? dateDistanceInWords : format(date, 'D MMMM [at] HH:mm ')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getAllMatches = (regex: RegExp, input: string): RegExpExecArray[] => {
|
export const getAllMatches = (regex: RegExp, input: string): RegExpExecArray[] => {
|
||||||
|
|||||||
Reference in New Issue
Block a user