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 //redirect user to dashboard
return this.router.navigate(['/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 = []
summary.patients = await this.GetDB() summary.patients = await this.GetDB()
.then((db) => { .then((db) => {
return db.find({ return db.find({
selector: { selector: {
doc_type: DocType.ResourceFhir, doc_type: DocType.ResourceFhir,
source_resource_type: "Patient", 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) summary.resource_type_counts = await this.findDocumentByPrefix(`${DocType.ResourceFhir}`, false)
.then((paginatedResp) => { .then((paginatedResp) => {

View File

@ -136,6 +136,12 @@ export class PouchdbCrypto {
return encrypted return encrypted
}, },
outgoing: async (doc) => { outgoing: async (doc) => {
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 no crypt, ex: after .removeCrypto(), just return the doc
if (!db._crypt) { return doc } if (!db._crypt) { return doc }
let decryptedString = await db._crypt.decrypt(doc.payload) let decryptedString = await db._crypt.decrypt(doc.payload)
@ -146,9 +152,7 @@ export class PouchdbCrypto {
} }
return decrypted return decrypted
} }
})
return db
}
public static removeCrypto(db) { public static removeCrypto(db) {
delete db._crypt delete db._crypt
} }