Skip to content

Commit

Permalink
Resolve #561
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed Nov 1, 2022
1 parent e3ffad8 commit c199b63
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
14 changes: 5 additions & 9 deletions integrations/nextcloud/snappymail/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,13 @@ public function boot(IBootContext $context): void
// $userSession->listen('\OC\User', 'postRememberedLogin', function($loginName, $password) {
$userSession->listen('\OC\User', 'postLogin', function($user, $loginName, $password, $isTokenLogin) {
$config = \OC::$server->getConfig();
$sEmail = '';
// Only store the user's password in the current session if they have
// enabled auto-login using Nextcloud username or email address.
if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) {
$sEmail = $user->getUID();
} else if ($config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) {
$sEmail = $config->getUserValue($user->getUID(), 'settings', 'email', '');
}
if ($sEmail) {
\OC::$server->getSession()['snappymail-email'] = $sEmail;
\OC::$server->getSession()['snappymail-password'] = SnappyMailHelper::encodePassword($password, \md5($sEmail));
if ($config->getAppValue('snappymail', 'snappymail-autologin', false)
|| $config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) {
$sUID = $user->getUID();
\OC::$server->getSession()['snappymail-nc-uid'] = $sUID;
\OC::$server->getSession()['snappymail-password'] = SnappyMailHelper::encodePassword($password, $sUID);
}
});

Expand Down
56 changes: 36 additions & 20 deletions integrations/nextcloud/snappymail/lib/Util/SnappyMailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ public static function loadApp() : void
}
});

$_ENV['SNAPPYMAIL_NEXTCLOUD'] = true;
$_ENV['SNAPPYMAIL_NEXTCLOUD'] = true; // Obsolete
$_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true;

// define('APP_VERSION', '0.0.0');
// define('APP_INDEX_ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
// include APP_INDEX_ROOT_PATH.'snappymail/v/'.APP_VERSION.'/include.php';
// define('APP_DATA_FOLDER_PATH', \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/').'/appdata_snappymail/');

require_once \dirname(\dirname(__DIR__)) . '/app/index.php';

$oConfig = \RainLoop\Api::Config();
Expand Down Expand Up @@ -96,15 +101,20 @@ public static function startApp(bool $handle = false) : void
}
}
} else {
$aCredentials = SnappyMailHelper::getLoginCredentials();
if ($oActions->getMainAccountFromToken(false)) {
if (!$aCredentials[0] || !$aCredentials[1]) {
$oActions->Logout(true);
}
} else if ($aCredentials[0] && $aCredentials[1]) {
$oActions->Logger()->AddSecret($aCredentials[1]);
$oAccount = $oActions->LoginProcess($aCredentials[0], $aCredentials[1], false);
$aCredentials = static::getLoginCredentials();
$ocSession = \OC::$server->getSession();
$doLogin = !$oActions->getMainAccountFromToken(false);
if (!$doLogin && $ocSession['snappymail-uid'] && $ocSession['snappymail-uid'] != $aCredentials[0]) {
// UID changed, Impersonate plugin probably active
$ocSession['snappymail-uid'] = false;
$oActions->Logout(true);
$doLogin = true;
}
if ($doLogin && $aCredentials[1] && $aCredentials[2]) {
$oActions->Logger()->AddSecret($aCredentials[2]);
$oAccount = $oActions->LoginProcess($aCredentials[1], $aCredentials[2], false);
if ($oAccount) {
$ocSession['snappymail-uid'] = $aCredentials[0];
$oActions->Plugins()->RunHook('login.success', array($oAccount));
$oActions->SetAuthToken($oAccount);
}
Expand All @@ -127,17 +137,20 @@ public static function getLoginCredentials() : array
$sPassword = '';
$config = \OC::$server->getConfig();
$sUID = \OC::$server->getUserSession()->getUser()->getUID();
// Only store the user's password in the current session if they have
$ocSession = \OC::$server->getSession();
// Only use the user's password in the current session if they have
// enabled auto-login using Nextcloud username or email address.
if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) {
$sEmail = $sUID;
$sPassword = \OC::$server->getSession()['snappymail-password'];
} else if ($config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) {
$sEmail = $config->getUserValue($sUID, 'settings', 'email', '');
$sPassword = \OC::$server->getSession()['snappymail-password'];
}
if (\OC::$server->getSession()['snappymail-email'] != $sEmail) {
$sPassword = '';
if ($ocSession['snappymail-nc-uid'] == $sUID) {
if ($config->getAppValue('snappymail', 'snappymail-autologin', false)) {
$sEmail = $sUID;
$sPassword = $ocSession['snappymail-password'];
} else if ($config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) {
$sEmail = $config->getUserValue($sUID, 'settings', 'email', '');
$sPassword = $ocSession['snappymail-password'];
}
if ($sPassword) {
$sPassword = static::decodePassword($sPassword, $sUID);
}
}

// If the user has set credentials for SnappyMail in their personal
Expand All @@ -146,8 +159,11 @@ public static function getLoginCredentials() : array
if ($sCustomEmail) {
$sEmail = $sCustomEmail;
$sPassword = $config->getUserValue($sUID, 'snappymail', 'snappymail-password', '');
if ($sPassword) {
$sPassword = static::decodePassword($sPassword, \md5($sEmail));
}
}
return [$sEmail, $sPassword ? SnappyMailHelper::decodePassword($sPassword, \md5($sEmail)) : ''];
return [$sUID, $sEmail, $sPassword ?: ''];
}

public static function getAppUrl() : string
Expand Down

0 comments on commit c199b63

Please sign in to comment.