when unable to validate generated encryption key is stored correctly, retrun back to genreation page and remove incorrect encryption key.

Added mechanism to decrypt payloads when using "find" function.
This commit is contained in:
Jason Kulatunga 2022-10-19 22:31:45 -07:00
parent 0d7d64e089
commit 63c00c7821
3 changed files with 31 additions and 12 deletions

View File

@ -109,7 +109,19 @@ export class EncryptionManagerComponent implements OnInit {
//redirect user to dashboard
return this.router.navigate(['/dashboard']);
})
.catch(console.error)
.catch((err) => {
// delete invalid encryption key
this.currentStep = 1
return PouchdbCrypto.DeleteCryptConfig(this.fastenDbService.current_user)
.then(() => {
//an error occurred while importing credential
const toastNotification = new ToastNotification()
toastNotification.type = ToastType.Error
toastNotification.message = "Provided encryption key does not match. Generating new encryption key, please store it and try again."
toastNotification.autohide = false
this.toastService.show(toastNotification)
})
})
}
/////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -149,14 +149,17 @@ export class FastenDbService extends PouchdbRepository {
// summary.patients = []
summary.patients = await this.GetDB()
.then((db) => {
return db.find({
selector: {
doc_type: DocType.ResourceFhir,
source_resource_type: "Patient",
}
})
}).then((results) => {
return Promise.all((results.docs || []).map((doc) => PouchdbCrypto.decryptDocument(db, doc)))
})
})
.then((results) => results.docs)
summary.resource_type_counts = await this.findDocumentByPrefix(`${DocType.ResourceFhir}`, false)
.then((paginatedResp) => {

View File

@ -136,19 +136,23 @@ export class PouchdbCrypto {
return encrypted
},
outgoing: async (doc) => {
// if no crypt, ex: after .removeCrypto(), just return the doc
if (!db._crypt) { return doc }
let decryptedString = await db._crypt.decrypt(doc.payload)
let decrypted = JSON.parse(decryptedString)
for (let key of db._ignore) {
// patch decrypted doc with ignored fields
if (key in doc) decrypted[key] = doc[key]
}
return decrypted
return await this.decryptDocument(db, doc)
}
})
return db
}
public static async decryptDocument(db, doc):Promise<any>{
// if no crypt, ex: after .removeCrypto(), just return the doc
if (!db._crypt) { return doc }
let decryptedString = await db._crypt.decrypt(doc.payload)
let decrypted = JSON.parse(decryptedString)
for (let key of db._ignore) {
// patch decrypted doc with ignored fields
if (key in doc) decrypted[key] = doc[key]
}
return decrypted
}
public static removeCrypto(db) {
delete db._crypt
}