Delete OpenPGP keys on the server when they are deleted in the browser

This commit is contained in:
Sergey Mosin 2024-12-05 11:14:03 -05:00
parent 7eaa065d76
commit c0348893af
3 changed files with 31 additions and 4 deletions

View file

@ -13,7 +13,7 @@ import { OpenPgpKeyPopupView } from 'View/Popup/OpenPgpKey';
import { Passphrases } from 'Storage/Passphrases'; import { Passphrases } from 'Storage/Passphrases';
import { baseCollator } from 'Common/Translator'; import { baseCollator, getNotification } from 'Common/Translator';
const const
loaded = () => !!window.openpgp, loaded = () => !!window.openpgp,
@ -120,6 +120,17 @@ class OpenPgpKeyModel {
OpenPGPUserStore.publicKeys.remove(this); OpenPGPUserStore.publicKeys.remove(this);
storeOpenPgpKeys(OpenPGPUserStore.publicKeys, publicKeysItem); storeOpenPgpKeys(OpenPGPUserStore.publicKeys, publicKeysItem);
} }
Remote.request('DeletePGPKey',
(iError, oData) => {
if (oData) {
if (iError || oData.Result === false) {
alert(oData.message || getNotification(iError));
}
}
}, {
key: this.armor
}
);
} }
} }
/* /*

View file

@ -287,6 +287,14 @@ trait Pgp
return $this->DefaultResponse(($key && $keyId && Backup::PGPKey($key, $keyId))); 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 * https://datatracker.ietf.org/doc/html/rfc3156#section-5
*/ */

View file

@ -9,7 +9,7 @@ namespace SnappyMail\PGP;
class Backup 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(); $oActions = \RainLoop\Api::Actions();
$oAccount = $oActions->getMainAccountFromToken(); $oAccount = $oActions->getMainAccountFromToken();
@ -21,15 +21,23 @@ class Backup
true true
); );
if (\str_contains($key, 'PGP PRIVATE KEY')) { if (\str_contains($key, 'PGP PRIVATE KEY')) {
$fileName = "{$dir}{$keyId}.key";
if ($doDelete) {
return !file_exists($fileName) || unlink($fileName);
}
$hash = $oAccount->CryptKey(); $hash = $oAccount->CryptKey();
$key = \SnappyMail\Crypt::Encrypt($key, $hash); $key = \SnappyMail\Crypt::Encrypt($key, $hash);
$key[1] = \base64_encode($key[1]); $key[1] = \base64_encode($key[1]);
$key[2] = \base64_encode($key[2]); $key[2] = \base64_encode($key[2]);
$key[3] = \hash_hmac('sha1', $key[2], $hash); $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')) { 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; return false;