Skip to content

Commit

Permalink
make sure we delete invalid keys if they fail validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed Oct 18, 2022
1 parent 53c0c66 commit 0d7d64e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
})
})
}

Expand Down
4 changes: 4 additions & 0 deletions frontend/src/lib/database/plugins/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down

0 comments on commit 0d7d64e

Please sign in to comment.