diff --git a/dev/Stores/User/OpenPGP.js b/dev/Stores/User/OpenPGP.js index 55acd484d..a4437edc1 100644 --- a/dev/Stores/User/OpenPGP.js +++ b/dev/Stores/User/OpenPGP.js @@ -13,7 +13,7 @@ import { OpenPgpKeyPopupView } from 'View/Popup/OpenPgpKey'; import { Passphrases } from 'Storage/Passphrases'; -import { baseCollator } from 'Common/Translator'; +import { baseCollator, getNotification } from 'Common/Translator'; const loaded = () => !!window.openpgp, @@ -120,6 +120,17 @@ class OpenPgpKeyModel { OpenPGPUserStore.publicKeys.remove(this); storeOpenPgpKeys(OpenPGPUserStore.publicKeys, publicKeysItem); } + Remote.request('DeletePGPKey', + (iError, oData) => { + if (oData) { + if (iError || oData.Result === false) { + alert(oData.message || getNotification(iError)); + } + } + }, { + key: this.armor + } + ); } } /* diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Pgp.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Pgp.php index 939610695..a87b6d1da 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Pgp.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Pgp.php @@ -287,6 +287,14 @@ trait Pgp return $this->DefaultResponse(($key && $keyId && Backup::PGPKey($key, $keyId))); } + public function DoDeletePGPKey(): array + { + $key = $this->GetActionParam('key', ''); + $keyId = $this->GetActionParam('keyId', ''); + $success = $key && Backup::PGPKey($key, $keyId, true); + return $this->DefaultResponse($success, ['code' => (int)!$success]); + } + /** * https://datatracker.ietf.org/doc/html/rfc3156#section-5 */ diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/pgp/backup.php b/snappymail/v/0.0.0/app/libraries/snappymail/pgp/backup.php index 18b18cb1f..f16acbd67 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/pgp/backup.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/pgp/backup.php @@ -9,7 +9,7 @@ namespace SnappyMail\PGP; class Backup { - public static function PGPKey(string $key, string $keyId = '') : bool + public static function PGPKey(string $key, string $keyId = '', $doDelete = false) : bool { $oActions = \RainLoop\Api::Actions(); $oAccount = $oActions->getMainAccountFromToken(); @@ -21,15 +21,23 @@ class Backup true ); if (\str_contains($key, 'PGP PRIVATE KEY')) { + $fileName = "{$dir}{$keyId}.key"; + if ($doDelete) { + return !file_exists($fileName) || unlink($fileName); + } $hash = $oAccount->CryptKey(); $key = \SnappyMail\Crypt::Encrypt($key, $hash); $key[1] = \base64_encode($key[1]); $key[2] = \base64_encode($key[2]); $key[3] = \hash_hmac('sha1', $key[2], $hash); - return !!\file_put_contents("{$dir}{$keyId}.key", \json_encode($key)); + return !!\file_put_contents($fileName, \json_encode($key)); } if (\str_contains($key, 'PGP PUBLIC KEY')) { - return !!\file_put_contents("{$dir}{$keyId}_public.asc", $key); + $fileName = "{$dir}{$keyId}_public.asc"; + if ($doDelete) { + return !file_exists($fileName) || unlink($fileName); + } + return !!\file_put_contents($fileName, $key); } } return false;