Skip to content

Commit

Permalink
Fix persistence of hasMailCrypt attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
y3n4 committed Oct 18, 2024
1 parent 51afa90 commit d46b34f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Command/UsersCheckPasswordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
false === $userDbLookup &&
false === $user->hasMailCrypt() &&
null === $user->getMailCryptPublicKey()) {
$this->mailCryptKeyHandler->create($user, $password);
$this->mailCryptKeyHandler->create($user, $password, true);
}

// Optionally set mail_crypt environment variables for checkpassword-reply command
Expand Down
7 changes: 6 additions & 1 deletion src/Handler/MailCryptKeyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ public function toPkcs8(string $privateKey): string

/**
* @throws Exception
*
*/
public function create(User $user, string $password): void
public function create(User $user, string $password, ?bool $mailCryptEnable = false): void
{
$pKey = openssl_pkey_new([
'private_key_type' => self::MAIL_CRYPT_PRIVATE_KEY_TYPE,
Expand All @@ -80,6 +81,10 @@ public function create(User $user, string $password): void
// Clear variables with confidential content from memory
$keyPair->erase();

if (true === $mailCryptEnable) {
$user->setMailCrypt(true);
}

$this->manager->flush();
}

Expand Down
9 changes: 3 additions & 6 deletions src/Handler/RegistrationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ public function handle(Registration $registration): void
$user = $this->buildUser($registration);

// Update password, generate MailCrypt keys, generate recovery token
// key material for mailCrypt is always generated, but only enabled if MAIL_CRYPT >= 2
$mailCryptEnable = $this->mailCrypt >= 2;
$this->passwordUpdater->updatePassword($user, $registration->getPlainPassword());
$this->mailCryptKeyHandler->create($user, $registration->getPlainPassword());
$this->mailCryptKeyHandler->create($user, $registration->getPlainPassword(), $mailCryptEnable);
$this->recoveryTokenHandler->create($user);

// Enable mailbox encryption
if ($this->mailCrypt >= 2) {
$user->setMailCrypt(true);
}

// We used to erase sensitive data here, but it's now done in RegistrationController
// as we need to print the plainRecoveryToken beforehand

Expand Down

0 comments on commit d46b34f

Please sign in to comment.