decrypt notes
This commit is contained in:
@@ -12,7 +12,7 @@ const emit = defineEmits<{
|
||||
const results = computed<Note[]>(() => {
|
||||
return (
|
||||
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>()
|
||||
|
||||
@@ -10,6 +10,8 @@ const props = defineProps<{
|
||||
const emit = defineEmits<{
|
||||
goToNote: []
|
||||
}>()
|
||||
|
||||
console.log(props.activeResult)
|
||||
</script>
|
||||
<template>
|
||||
<li class="flex flex-row">
|
||||
|
||||
@@ -57,7 +57,8 @@ const renderMindmap = () => {
|
||||
// A function that determines whether the node should be animated
|
||||
// All nodes animated by default on animate enabled
|
||||
// Non-animated nodes are positioned immediately when the layout starts
|
||||
animateFilter: function (node, i) {
|
||||
// animateFilter: function (node, i) {
|
||||
animateFilter: function () {
|
||||
return true
|
||||
},
|
||||
|
||||
@@ -88,7 +89,8 @@ const renderMindmap = () => {
|
||||
componentSpacing: 40,
|
||||
|
||||
// Node repulsion (non overlapping) multiplier
|
||||
nodeRepulsion: function (node) {
|
||||
// nodeRepulsion: function (node) {
|
||||
nodeRepulsion: function () {
|
||||
return 2048
|
||||
},
|
||||
|
||||
@@ -96,12 +98,14 @@ const renderMindmap = () => {
|
||||
nodeOverlap: 4,
|
||||
|
||||
// Ideal edge (non nested) length
|
||||
idealEdgeLength: function (edge) {
|
||||
// idealEdgeLength: function (edge) {
|
||||
idealEdgeLength: function () {
|
||||
return 32
|
||||
},
|
||||
|
||||
// Divisor to compute edge forces
|
||||
edgeElasticity: function (edge) {
|
||||
// edgeElasticity: function (edge) {
|
||||
edgeElasticity: function () {
|
||||
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 firebase from 'firebase/compat/app'
|
||||
import type { User } from '@firebase/auth-types'
|
||||
import { getFirestore } from 'firebase/firestore'
|
||||
import type { Firestore } from 'firebase/firestore'
|
||||
|
||||
// import { getAnalytics } from "firebase/analytics";
|
||||
// TODO: Add SDKs for Firebase products that you want to use
|
||||
@@ -21,14 +23,17 @@ const firebaseConfig = {
|
||||
|
||||
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 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 { viewModes, activeViewMode } from '@/composables/useViewMode'
|
||||
import shortid from 'shortid'
|
||||
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 { getAllMatches } from '@/utils/helpers'
|
||||
import { initialized, user } from '@/composables/useFirebase'
|
||||
import shortid from 'shortid'
|
||||
|
||||
const notesSources = computed(() => ({
|
||||
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(
|
||||
activeNotesSource,
|
||||
() => {
|
||||
async () => {
|
||||
if (!activeNotesSource.value) return
|
||||
baseNotes.value = {}
|
||||
let notes: BaseNotes = {}
|
||||
if (activeNotesSource.value === 'local') {
|
||||
try {
|
||||
const localNotes = JSON.parse(localStorage.getItem('notes') || '{}')
|
||||
baseNotes.value = localNotes
|
||||
notes = JSON.parse(localStorage.getItem('notes') || '{}')
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
} else if (activeNotesSource.value === 'firebase') {
|
||||
console.log('get notes from firebase')
|
||||
} else if (activeNotesSource.value === 'firebase' && db.value) {
|
||||
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)
|
||||
|
||||
setActiveNote(rootNote.value?.id)
|
||||
|
||||
7
src/types.d.ts
vendored
7
src/types.d.ts
vendored
@@ -12,6 +12,10 @@ declare global {
|
||||
wordCount: number
|
||||
}
|
||||
|
||||
interface BaseNotes {
|
||||
[noteId: string]: BaseNote
|
||||
}
|
||||
|
||||
interface ViewMode {
|
||||
name: string
|
||||
icon: string
|
||||
@@ -36,5 +40,8 @@ declare global {
|
||||
from: string[]
|
||||
}
|
||||
}
|
||||
|
||||
type ClientKey = string
|
||||
type EncryptionKey = string
|
||||
}
|
||||
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)
|
||||
: date
|
||||
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[] => {
|
||||
|
||||
Reference in New Issue
Block a user