Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix .cryptkey Issue When Switching Between SSO and Non-SSO Accounts #1750

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 70 additions & 21 deletions snappymail/v/0.0.0/app/libraries/RainLoop/Model/MainAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,43 @@ public function resealCryptKey(SensitiveString $oOldPass) : bool
{
$oStorage = \RainLoop\Api::Actions()->StorageProvider();
$sKey = $oStorage->Get($this, StorageType::ROOT, '.cryptkey');
if ($sKey) {
$sKey = \SnappyMail\Crypt::DecryptFromJSON($sKey, $oOldPass);
if (!$sKey) {
throw new ClientException(Notifications::CryptKeyError);

// Get the AppManager from the server container
$appManager = \OC::$server->getAppManager();
// Check if the OIDC Login app is enabled
$isEnabled = $appManager->isEnabledForUser('oidc_login');
// If OIDC Login is enabled then we will assign $pwd instead of $this->IncPassword() and $pwd value is taken by following similar approach as https://github.com/the-djmaze/snappymail/blob/0db6c6ab5f9e05c46b13ebbdaf351341710438ac/plugins/login-gmail/index.php#L125
if ($isEnabled) {
$sUID = \OC::$server->getUserSession()->getUser()->getUID();
$pwd = new \SnappyMail\SensitiveString($sUID);
if ($sKey) {
$sKey = \SnappyMail\Crypt::DecryptFromJSON($sKey, $pwd);
if (!$sKey) {
throw new ClientException(Notifications::CryptKeyError);
}
$sKey = \SnappyMail\Crypt::EncryptToJSON($sKey, $pwd);
if ($sKey) {
$this->sCryptKey = null;
if (\RainLoop\Api::Actions()->StorageProvider()->Put($this, StorageType::ROOT, '.cryptkey', $sKey)) {
return true;
}
}
}
$sKey = \SnappyMail\Crypt::EncryptToJSON($sKey, $this->IncPassword());
} else {
if ($sKey) {
$this->sCryptKey = null;
if (\RainLoop\Api::Actions()->StorageProvider()->Put($this, StorageType::ROOT, '.cryptkey', $sKey)) {
return true;
$sKey = \SnappyMail\Crypt::DecryptFromJSON($sKey, $oOldPass);
if (!$sKey) {
throw new ClientException(Notifications::CryptKeyError);
}
$sKey = \SnappyMail\Crypt::EncryptToJSON($sKey, $this->IncPassword());
if ($sKey) {
$this->sCryptKey = null;
if (\RainLoop\Api::Actions()->StorageProvider()->Put($this, StorageType::ROOT, '.cryptkey', $sKey)) {
return true;
}
}
}
}
}
return false;
}

Expand All @@ -39,18 +63,43 @@ public function CryptKey() : string
// can use the old password to re-seal the cryptkey
$oStorage = \RainLoop\Api::Actions()->StorageProvider();
$sKey = $oStorage->Get($this, StorageType::ROOT, '.cryptkey');
if (!$sKey) {
$sKey = \SnappyMail\Crypt::EncryptToJSON(
\sha1($this->IncPassword() . APP_SALT),
$this->IncPassword()
);
$oStorage->Put($this, StorageType::ROOT, '.cryptkey', $sKey);
}
$sKey = \SnappyMail\Crypt::DecryptFromJSON($sKey, $this->IncPassword());
if (!$sKey) {
throw new ClientException(Notifications::CryptKeyError);
}
$this->sCryptKey = new SensitiveString(\hex2bin($sKey));

// Get the AppManager from the server container
$appManager = \OC::$server->getAppManager();
// Check if the OIDC Login app is enabled
$isEnabled = $appManager->isEnabledForUser('oidc_login');
// If OIDC Login app is enabled then we will assign $pwd instead of $this->IncPassword() and $pwd value is taken by following similar approach as https://github.com/the-djmaze/snappymail/blob/0db6c6ab5f9e05c46b13ebbdaf351341710438ac/plugins/login-gmail/index.php#L125
if ($isEnabled) {
$sUID = \OC::$server->getUserSession()->getUser()->getUID();
$pwd = new \SnappyMail\SensitiveString($sUID);
if (!$sKey) {
$sKey = \SnappyMail\Crypt::EncryptToJSON(
\sha1($pwd . APP_SALT),
$pwd
);
$oStorage->Put($this, StorageType::ROOT, '.cryptkey', $sKey);
}
$sKey = \SnappyMail\Crypt::DecryptFromJSON($sKey, $pwd);
if (!$sKey) {
throw new ClientException(Notifications::CryptKeyError);
}
$this->sCryptKey = new SensitiveString(\hex2bin($sKey));

} else {
if (!$sKey) {
$sKey = \SnappyMail\Crypt::EncryptToJSON(
\sha1($this->IncPassword() . APP_SALT),
$this->IncPassword()
);
$oStorage->Put($this, StorageType::ROOT, '.cryptkey', $sKey);
}
$sKey = \SnappyMail\Crypt::DecryptFromJSON($sKey, $this->IncPassword());
if (!$sKey) {
throw new ClientException(Notifications::CryptKeyError);
}
$this->sCryptKey = new SensitiveString(\hex2bin($sKey));
}

}
return $this->sCryptKey;
}
Expand Down
Loading