Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
use OCA\Mail\Service\UserPreferenceService;
use OCA\Mail\SetupChecks\MailConnectionPerformance;
use OCA\Mail\SetupChecks\MailTransport;
use OCA\Mail\UserMigration\MailAccountMigrator;
use OCA\Mail\Vendor\Favicon\Favicon;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand All @@ -87,6 +88,9 @@

include_once __DIR__ . '/../../vendor/autoload.php';

/**
* @codeCoverageIgnore
*/
final class Application extends App implements IBootstrap {
public const APP_ID = 'mail';

Expand Down Expand Up @@ -164,6 +168,8 @@ public function register(IRegistrationContext $context): void {
$context->registerSetupCheck(MailTransport::class);
$context->registerSetupCheck(MailConnectionPerformance::class);

$context->registerUserMigrator(MailAccountMigrator::class);

// bypass Horde Translation system
Horde_Translation::setHandler('Horde_Imap_Client', new HordeTranslationHandler());
Horde_Translation::setHandler('Horde_Mime', new HordeTranslationHandler());
Expand Down
12 changes: 11 additions & 1 deletion lib/Controller/AccountsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
Expand All @@ -52,7 +53,8 @@ class AccountsController extends Controller {
private IRemoteHostValidator $hostValidator;
private MailboxSync $mailboxSync;

public function __construct(string $appName,
public function __construct(
string $appName,
IRequest $request,
AccountService $accountService,
$UserId,
Expand All @@ -66,6 +68,7 @@ public function __construct(string $appName,
IConfig $config,
IRemoteHostValidator $hostValidator,
MailboxSync $mailboxSync,
private ITimeFactory $timeFactory,
) {
parent::__construct($appName, $request);
$this->accountService = $accountService;
Expand Down Expand Up @@ -386,6 +389,13 @@ public function create(string $accountName,
}
try {
$account = $this->setup->createNewAccount($accountName, $emailAddress, $imapHost, $imapPort, $imapSslMode, $imapUser, $imapPassword, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $smtpPassword, $this->currentUserId, $authMethod, null, $classificationEnabled);
// Set initial heartbeat
$this->config->setUserValue(
$account->getUserId(),
Application::APP_ID,
'ui-heartbeat',
(string)$this->timeFactory->getTime(),
);
} catch (CouldNotConnectException $e) {
$data = [
'error' => $e->getReason(),
Expand Down
7 changes: 4 additions & 3 deletions lib/Db/MailAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@
* @method void setAuthMethod(string $method)
* @method int getSignatureMode()
* @method void setSignatureMode(int $signatureMode)
* @method string getOauthAccessToken()
* @method string|null getOauthAccessToken()
* @method void setOauthAccessToken(string $token)
* @method string getOauthRefreshToken()
* @method string|null getOauthRefreshToken()
* @method void setOauthRefreshToken(string $token)
* @method int|null getOauthTokenTtl()
* @method void setOauthTokenTtl(int $ttl)
* @method void setOauthTokenTtl(int|null $ttl)
* @method int|null getSmimeCertificateId()
* @method void setSmimeCertificateId(int|null $smimeCertificateId)
* @method int|null getQuotaPercentage()
Expand Down Expand Up @@ -302,6 +302,7 @@ public function toJson() {
'name' => $this->getName(),
'order' => $this->getOrder(),
'emailAddress' => $this->getEmail(),
'authMethod' => $this->getAuthMethod() ?? 'password',
'imapHost' => $this->getInboundHost(),
'imapPort' => $this->getInboundPort(),
'imapUser' => $this->getInboundUser(),
Expand Down
8 changes: 6 additions & 2 deletions lib/IMAP/IMAPClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ public function getClient(Account $account, bool $useCache = true): Horde_Imap_C
];
if ($account->getMailAccount()->getAuthMethod() === 'xoauth2') {
try {
$decryptedAccessToken = $this->crypto->decrypt($account->getMailAccount()->getOauthAccessToken());
$oauthAccessToken = $account->getMailAccount()->getOauthAccessToken();
if ($oauthAccessToken === null) {
throw new ServiceException('Missing access token for xoauth2 account');
}
$decryptedAccessToken = $this->crypto->decrypt($oauthAccessToken);
} catch (Exception $e) {
throw new ServiceException('Could not decrypt account password: ' . $e->getMessage(), 0, $e);
throw new ServiceException('Could not decrypt account access token: ' . $e->getMessage(), 0, $e);
}

$params['password'] = $decryptedAccessToken; // Not used, but Horde wants this
Expand Down
5 changes: 3 additions & 2 deletions lib/Integration/GoogleIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public function finishConnect(Account $account,
}

public function refresh(Account $account): Account {
if ($account->getMailAccount()->getOauthTokenTtl() === null || $account->getMailAccount()->getOauthRefreshToken() === null) {
$oauthRefreshToken = $account->getMailAccount()->getOauthRefreshToken();
if ($account->getMailAccount()->getOauthTokenTtl() === null || $oauthRefreshToken === null) {
// Account is not authorized yet
return $account;
}
Expand All @@ -137,7 +138,7 @@ public function refresh(Account $account): Account {
return $account;
}

$refreshToken = $this->crypto->decrypt($account->getMailAccount()->getOauthRefreshToken());
$refreshToken = $this->crypto->decrypt($oauthRefreshToken);
$clientSecret = $this->crypto->decrypt($encryptedClientSecret);
$httpClient = $this->clientService->newClient();
try {
Expand Down
5 changes: 3 additions & 2 deletions lib/Integration/MicrosoftIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public function finishConnect(Account $account,
}

public function refresh(Account $account): Account {
if ($account->getMailAccount()->getOauthTokenTtl() === null || $account->getMailAccount()->getOauthRefreshToken() === null) {
$oauthRefreshToken = $account->getMailAccount()->getOauthRefreshToken();
if ($account->getMailAccount()->getOauthTokenTtl() === null || $oauthRefreshToken === null) {
// Account is not authorized yet
return $account;
}
Expand All @@ -152,7 +153,7 @@ public function refresh(Account $account): Account {
return $account;
}

$refreshToken = $this->crypto->decrypt($account->getMailAccount()->getOauthRefreshToken());
$refreshToken = $this->crypto->decrypt($oauthRefreshToken);
$clientSecret = $this->crypto->decrypt($encryptedClientSecret);
$httpClient = $this->clientService->newClient();
try {
Expand Down
12 changes: 11 additions & 1 deletion lib/SMTP/SmtpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

namespace OCA\Mail\SMTP;

use Exception;
use Horde_Mail_Transport;
use Horde_Mail_Transport_Smtphorde;
use Horde_Smtp_Password_Xoauth2;
use OCA\Mail\Account;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Support\HostNameFactory;
use OCP\IConfig;
use OCP\Security\ICrypto;
Expand Down Expand Up @@ -64,7 +66,15 @@ public function create(Account $account): Horde_Mail_Transport {
],
];
if ($account->getMailAccount()->getAuthMethod() === 'xoauth2') {
$decryptedAccessToken = $this->crypto->decrypt($account->getMailAccount()->getOauthAccessToken());
try {
$oauthAccessToken = $account->getMailAccount()->getOauthAccessToken();
if ($oauthAccessToken === null) {
throw new ServiceException('Missing access token for xoauth2 account');
}
$decryptedAccessToken = $this->crypto->decrypt($oauthAccessToken);
} catch (Exception $e) {
throw new ServiceException('Could not decrypt account access token: ' . $e->getMessage(), 0, $e);
}

$params['password'] = $decryptedAccessToken; // Not used, but Horde wants this
$params['xoauth2_token'] = new Horde_Smtp_Password_Xoauth2(
Expand Down
9 changes: 0 additions & 9 deletions lib/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace OCA\Mail\Service;

use OCA\Mail\Account;
use OCA\Mail\AppInfo\Application;
use OCA\Mail\BackgroundJob\PreviewEnhancementProcessingJob;
use OCA\Mail\BackgroundJob\QuotaJob;
use OCA\Mail\BackgroundJob\RepairSyncJob;
Expand Down Expand Up @@ -176,14 +175,6 @@ public function save(MailAccount $newAccount): MailAccount {
// Insert background jobs for this account
$this->scheduleBackgroundJobs($newAccount->getId());

// Set initial heartbeat
$this->config->setUserValue(
$newAccount->getUserId(),
Application::APP_ID,
'ui-heartbeat',
(string)$this->timeFactory->getTime(),
);

return $newAccount;
}

Expand Down
Loading