- md5
- 705848feed6e26509448c0085f5b50c0
- sha1
- b31896cff5e3679ba8d9a2ee6cfa1c60b24b8327
- sha256
- c8044646e675500c55ba76b4a7b28f1cfbda509df5be02c46146664c225c0fd0
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
<?php
/**
* @package Joomla.Plugin
* @subpackage System.Webauthn
*
* @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Plugin\System\Webauthn\PluginTraits;
// Protect from unauthorized access
\defined('_JEXEC') or die();
use Exception;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Uri\Uri;
use Joomla\Plugin\System\Webauthn\Exception\AjaxNonCmsAppException;
use Joomla\Plugin\System\Webauthn\Helper\Joomla;
use RuntimeException;
/**
* Allows the plugin to handle AJAX requests in the backend of the site, where com_ajax is not available when we are not
* logged in.
*
* @since 4.0.0
*/
trait AjaxHandler
{
/**
* Processes the callbacks from the passwordless login views.
*
* Note: this method is called from Joomla's com_ajax or, in the case of backend logins, through the special
* onAfterInitialize handler we have created to work around com_ajax usage limitations in the backend.
*
* @return void
*
* @throws Exception
*
* @since 4.0.0
*/
public function onAjaxWebauthn(): void
{
// Load the language files
$this->loadLanguage();
/** @var CMSApplication $app */
$app = Factory::getApplication();
$input = $app->input;
// Get the return URL from the session
$returnURL = Joomla::getSessionVar('returnUrl', Uri::base(), 'plg_system_webauthn');
$result = null;
try
{
Joomla::log('system', "Received AJAX callback.");
if (!($app instanceof CMSApplication))
{
throw new AjaxNonCmsAppException;
}
$akaction = $input->getCmd('akaction');
$token = Joomla::getToken();
if ($input->getInt($token, 0) != 1)
{
throw new RuntimeException(Text::_('JERROR_ALERTNOAUTHOR'));
}
// Empty action? No bueno.
if (empty($akaction))
{
throw new RuntimeException(Text::_('PLG_SYSTEM_WEBAUTHN_ERR_AJAX_INVALIDACTION'));
}
// Call the plugin event onAjaxWebauthnSomething where Something is the akaction param.
$eventName = 'onAjaxWebauthn' . ucfirst($akaction);
$results = $app->triggerEvent($eventName, []);
foreach ($results as $r)
{
if (\is_null($r))
{
continue;
}
$result = $r;
break;
}
}
catch (AjaxNonCmsAppException $e)
{
Joomla::log('system', "This is not a CMS application", Log::NOTICE);
}
catch (Exception $e)
{
Joomla::log('system', "Callback failure, redirecting to $returnURL.");
Joomla::setSessionVar('returnUrl', null, 'plg_system_webauthn');
$app->enqueueMessage($e->getMessage(), 'error');
$app->redirect($returnURL);
return;
}
if (!\is_null($result))
{
switch ($input->getCmd('encoding', 'json'))
{
case 'jsonhash':
Joomla::log('system', "Callback complete, returning JSON inside ### markers.");
echo '###' . json_encode($result) . '###';
break;
case 'raw':
Joomla::log('system', "Callback complete, returning raw response.");
echo $result;
break;
case 'redirect':
$modifiers = '';
if (isset($result['message']))
{
$type = isset($result['type']) ? $result['type'] : 'info';
$app->enqueueMessage($result['message'], $type);
$modifiers = " and setting a system message of type $type";
}
if (isset($result['url']))
{
Joomla::log('system', "Callback complete, performing redirection to {$result['url']}{$modifiers}.");
$app->redirect($result['url']);
}
Joomla::log('system', "Callback complete, performing redirection to {$result}{$modifiers}.");
$app->redirect($result);
return;
default:
Joomla::log('system', "Callback complete, returning JSON.");
echo json_encode($result);
break;
}
$app->close(200);
}
Joomla::log('system', "Null response from AJAX callback, redirecting to $returnURL");
Joomla::setSessionVar('returnUrl', null, 'plg_system_webauthn');
$app->redirect($returnURL);
}
}
The same content is in 5 releases, Joomla 4.0.6-rc1 to 4.1.0-beta3. See them all, or every version of this file.