- md5
- 8baa87ffc7d473feaed9fb8ac7741473
- sha1
- 81b7bb831b5c5c379109f2e04f6f6635dbde856f
- sha256
- 967b0e8464198ff95839a96aac9461f4b51a1b3277d52eb035f55a1ee65b6d86
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
<?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;
use Joomla\CMS\Event\Plugin\System\Webauthn\AjaxChallenge;
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\User\User;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\CMS\User\UserHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Ajax handler for akaction=challenge
*
* Generates the public key and challenge which is used by the browser when logging in with Webauthn. This is the bit
* which prevents tampering with the login process and replay attacks.
*
* @since 4.0.0
*/
trait AjaxHandlerChallenge
{
/**
* Returns the public key set for the user and a unique challenge in a Public Key Credential Request encoded as
* JSON.
*
* @param AjaxChallenge $event The event we are handling
*
* @return void
*
* @throws \Exception
* @since 4.0.0
*/
public function onAjaxWebauthnChallenge(AjaxChallenge $event): void
{
// Initialize objects
$session = $this->getApplication()->getSession();
$input = $this->getApplication()->getInput();
// Retrieve data from the request
$username = $input->getUsername('username', '');
$returnUrl = base64_encode(
$session->get('plg_system_webauthn.returnUrl', Uri::current())
);
$returnUrl = $input->getBase64('returnUrl', $returnUrl);
$returnUrl = base64_decode($returnUrl);
// For security reasons the post-login redirection URL must be internal to the site.
if (!Uri::isInternal($returnUrl)) {
// If the URL wasn't internal redirect to the site's root.
$returnUrl = Uri::base();
}
$session->set('plg_system_webauthn.returnUrl', $returnUrl);
// Do I have a username?
if (empty($username)) {
$event->addResult(false);
return;
}
// Is the username valid?
try {
$userId = UserHelper::getUserId($username);
} catch (\Exception $e) {
$userId = 0;
}
if ($userId <= 0) {
$event->addResult(false);
return;
}
try {
$myUser = Factory::getContainer()->get(UserFactoryInterface::class)->loadUserById($userId);
} catch (\Exception $e) {
$myUser = new User();
}
if ($myUser->id != $userId || $myUser->guest) {
$event->addResult(false);
return;
}
$publicKeyCredentialRequestOptions = $this->authenticationHelper->getPubkeyRequestOptions($myUser);
$session->set('plg_system_webauthn.userId', $userId);
// Return the JSON encoded data to the caller
$event->addResult(json_encode($publicKeyCredentialRequestOptions, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}
}
The same content is in 37 releases, Joomla 4.4.0-alpha3 to 5.0.0-beta1. See them all, or every version of this file.