- md5
- 01bfcff16e199c043e0c4b3b622e1a99
- sha1
- 4eb379ffcb72d2023b7f5933d329f22d5fc3c81f
- sha256
- 27000f81d6419b52d983b575dff74008326dc89d4f111766fb819c54b5e7b4ea
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
<?php
/**
* @package Joomla.Administrator
* @subpackage com_config
*
* @copyright (C) 2026 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Config\Administrator\Controller;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Response\JsonResponse;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\User\UserHelper;
use Joomla\OAuth2\Client;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Mail Controller
*
* @since 6.2.0
*/
class MailController extends BaseController
{
/**
* Redirects an authorized administrator to the provider authorization endpoint.
*
* @return void
*
* @since 6.2.0
*/
public function oauth2auth(): void
{
if (!$this->app->getIdentity()->authorise('core.admin')) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::_('JERROR_ALERTNOAUTHOR'),
'error',
);
return;
}
$providerConfig = $this->resolveProviderConfig();
if (empty($providerConfig['authorize_url']) || empty($providerConfig['client_id'])) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::_('COM_CONFIG_MAIL_OAUTH2_AUTHORIZE_CONFIG_MISSING'),
'warning',
);
return;
}
$state = UserHelper::genRandomPassword(32);
$this->app->getSession()->set('com_config.oauth2_state', $state);
try {
$oauth2 = $this->createOAuth2Client($providerConfig, $state);
$this->setRedirect($oauth2->createUrl());
} catch (\Throwable $e) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
$e->getMessage(),
'error',
);
}
}
/**
* Processes the OAuth2 callback and stores the received refresh token.
*
* @return void
*
* @since 6.2.0
*/
public function oauth2callback(): void
{
if (!$this->app->getIdentity()->authorise('core.admin')) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::_('JERROR_ALERTNOAUTHOR'),
'error',
);
return;
}
$session = $this->app->getSession();
$stateFromProvider = $this->input->getString('state');
$stateFromSession = $session->get('com_config.oauth2_state');
if (
!$stateFromProvider
|| !$stateFromSession
|| !hash_equals($stateFromSession, $stateFromProvider)
) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::_('JINVALID_TOKEN'),
'error',
);
return;
}
$session->set('com_config.oauth2_state', null);
$error = $this->input->getString('error', '');
if ($error !== '') {
$description = $this->input->getString('error_description');
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
$description ?: $error,
'error'
);
return;
}
if (!$this->input->getString('code')) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::_('JERROR_AN_ERROR_HAS_OCCURRED'),
'error',
);
return;
}
$providerConfig = $this->resolveProviderConfig();
if (
empty($providerConfig['token_url'])
|| empty($providerConfig['client_id'])
|| empty($providerConfig['client_secret'])
) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::_('COM_CONFIG_MAIL_OAUTH2_TOKEN_CONFIG_MISSING'),
'warning',
);
return;
}
try {
$oauth2 = $this->createOAuth2Client($providerConfig);
$token = $oauth2->authenticate();
if (
!\is_array($token)
|| empty($token['refresh_token'])
) {
throw new \RuntimeException(
Text::_('COM_CONFIG_MAIL_OAUTH2_TOKEN_CREATE_FAILED'),
);
}
$refreshToken = (string)$token['refresh_token'];
} catch (\Throwable $e) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
$e->getMessage(),
'error',
);
return;
}
$issuedAt = gmdate('Y-m-d H:i:s') . ' UTC';
$formData = (array)$this->app->getUserState(
'com_config.config.global.data',
[],
);
$formData['smtp_oauth2_refresh_token'] = $refreshToken;
$formData['smtp_oauth2_token_issued_at'] = $issuedAt;
$this->app->setUserState(
'com_config.config.global.data',
$formData,
);
try {
$model = $this->getModel('Application', 'Administrator');
$saveData = $model->getData();
$saveData['smtp_oauth2_refresh_token'] = $refreshToken;
$saveData['smtp_oauth2_token_issued_at'] = $issuedAt;
if (!$model->save($saveData)) {
throw new \RuntimeException(
Text::_('COM_CONFIG_ERROR_WRITE_FAILED'),
);
}
} catch (\Throwable $e) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::sprintf(
'COM_CONFIG_MAIL_OAUTH2_TOKEN_SAVE_FAILED',
$e->getMessage(),
),
'warning',
);
return;
}
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::_('COM_CONFIG_MAIL_OAUTH2_TOKEN_SAVED'),
'message',
);
}
/**
* Checks whether the configured OAuth2 refresh token can be exchanged successfully.
*
* @return void
*
* @since 6.2.0
*/
public function oauth2checktoken(): void
{
if (!$this->app->getIdentity()->authorise('core.admin')) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::_('JERROR_ALERTNOAUTHOR'),
'error',
);
return;
}
$providerConfig = $this->resolveProviderConfig();
if (
empty($providerConfig['token_url'])
|| empty($providerConfig['client_id'])
|| empty($providerConfig['client_secret'])
|| empty($providerConfig['refresh_token'])
) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::_('COM_CONFIG_MAIL_OAUTH2_TOKEN_CHECK_MISSING'),
'warning',
);
return;
}
try {
$oauth2 = $this->createOAuth2Client($providerConfig);
$token = $oauth2->refreshToken(
$providerConfig['refresh_token'],
);
if (
!\is_array($token)
|| empty($token['access_token'])
) {
throw new \RuntimeException(
Text::_('COM_CONFIG_MAIL_OAUTH2_TOKEN_CHECK_INVALID'),
);
}
$expiresIn = isset($token['expires_in'])
? (int)$token['expires_in']
: 0;
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::sprintf(
'COM_CONFIG_MAIL_OAUTH2_TOKEN_CHECK_VALID',
$expiresIn,
),
'message',
);
} catch (\Throwable $e) {
$this->setRedirect(
Route::_('index.php?option=com_config&view=application', false),
Text::sprintf(
'COM_CONFIG_MAIL_OAUTH2_TOKEN_CHECK_FAILED',
$e->getMessage(),
),
'error',
);
}
}
/**
* Creates the OAuth2 client.
*
* @param array<string, string> $providerConfig OAuth2 provider configuration.
* @param string $state OAuth2 state value.
*
* @return Client
*
* @since 6.2.0
*/
private function createOAuth2Client(array $providerConfig, string $state = ''): Client
{
$requestParams = [
'response_mode' => 'query',
'prompt' => 'consent',
'access_type' => 'offline',
];
$options = [
'authurl' => $providerConfig['authorize_url'],
'tokenurl' => $providerConfig['token_url'],
'clientid' => $providerConfig['client_id'],
'clientsecret' => $providerConfig['client_secret'],
'redirecturi' => Uri::base() . 'index.php?option=com_config&task=mail.oauth2callback&format=raw',
'scope' => $providerConfig['scope'],
'state' => $state,
'requestparams' => $requestParams,
'userefresh' => true,
'sendheaders' => false,
];
return new Client(
$options,
null,
$this->input,
);
}
/**
* Build OAuth2 configuration from global configuration.
*
* @return array<string, string>
*
* @since 6.2.0
*/
private function resolveProviderConfig(): array
{
$params = $this->app->getConfig();
return [
'client_id' => $params->get('smtp_oauth2_client_id', ''),
'client_secret' => $params->get('smtp_oauth2_client_secret', ''),
'refresh_token' => $params->get('smtp_oauth2_refresh_token', ''),
'scope' => $params->get('smtp_oauth2_scope', ''),
'authorize_url' => $params->get('smtp_oauth2_authorize_url', ''),
'token_url' => $params->get('smtp_oauth2_token_url', ''),
];
}
/**
* Method to send the test mail.
*
* @return void
*
* @since 6.2.0
*/
public function sendtestmail()
{
// Send json mime type.
$this->app->mimeType = 'application/json';
$this->app->setHeader('Content-Type', $this->app->mimeType . '; charset=' . $this->app->charSet);
$this->app->sendHeaders();
// Check if user token is valid.
if (!Session::checkToken()) {
$this->app->enqueueMessage(Text::_('JINVALID_TOKEN'), 'error');
echo new JsonResponse();
$this->app->close();
}
// Check if the user is authorized to do this.
if (!$this->app->getIdentity()->authorise('core.admin')) {
$this->app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error');
echo new JsonResponse();
$this->app->close();
}
/** @var \Joomla\Component\Config\Administrator\Model\ApplicationModel $model */
$model = $this->getModel('Application', 'Administrator');
echo new JsonResponse($model->sendTestMail());
$this->app->close();
}
}
The same content is in 6 releases, Joomla 6.2.0-beta2 to 6.2.1-rc1. See them all, or every version of this file.