decrypt notes
This commit is contained in:
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)
|
||||
|
||||
Reference in New Issue
Block a user