make sure we delete invalid keys if they fail validation.

This commit is contained in:
Jason Kulatunga 2022-10-17 22:40:47 -07:00
parent 53c0c66ceb
commit 0d7d64e089
2 changed files with 17 additions and 10 deletions

View File

@ -129,13 +129,12 @@ export class EncryptionManagerComponent implements OnInit {
.then((content) => {
let cryptoConfig = JSON.parse(content) as PouchdbCryptConfig
if(cryptoConfig.key && cryptoConfig.config){
return PouchdbCrypto.StoreCryptConfig(cryptoConfig)
} else {
//throw an error & notify user
if(!cryptoConfig.key || !cryptoConfig.config){
this.importCustomFileError = "Invalid crypto configuration file"
throw new Error(this.importCustomFileError)
}
return PouchdbCrypto.StoreCryptConfig(cryptoConfig)
})
.then(() => {
//go to step 2
@ -156,13 +155,17 @@ export class EncryptionManagerComponent implements OnInit {
})
.catch((err) => {
console.error(err)
//an error occurred while importing credential
const toastNotification = new ToastNotification()
toastNotification.type = ToastType.Error
toastNotification.message = "Provided encryption key does not match. Please try a different key"
toastNotification.autohide = false
this.toastService.show(toastNotification)
// 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. Please try a different key"
toastNotification.autohide = false
this.toastService.show(toastNotification)
})
})
}

View File

@ -69,6 +69,10 @@ export class PouchdbCrypto {
}
return JSON.parse(cryptConfigStr) as PouchdbCryptConfig
}
public static async DeleteCryptConfig(currentUser: string): Promise<void>{
const localDb = await PouchdbCrypto.localIdb()
return await localDb.delete('crypto',`encryption_data_${currentUser}`)
}
public static async crypto(db, cryptConfig: PouchdbCryptConfig, options: PouchdbCryptoOptions = {}) {