CoreFiles by mySites.guru
AjaxHandlerDelete.php92 lines, 2,306 bytes, from Joomla_5.2.6-Stable-Full_Package.zip
Raw History Compare with 5.2.5
md5
865d6c85fc0ad85fdc925194717dced9
sha1
54db1e7406fe419369cd095ddcd00ce0ca61fb05
sha256
024992c16cbad656c5f760b8716d4e4218477c83e9435c33272851057cb218b8
<?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\AjaxDelete;
use Joomla\CMS\User\User;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * Ajax handler for akaction=delete
 *
 * Deletes a security key
 *
 * @since  4.0.0
 */
trait AjaxHandlerDelete
{
    /**
     * Handle the callback to remove an authenticator
     *
     * @param   AjaxDelete  $event  The event we are handling
     *
     * @return  void
     * @since   4.0.0
     */
    public function onAjaxWebauthnDelete(AjaxDelete $event): void
    {
        // Load plugin language files
        $this->loadLanguage();

        // Initialize objects
        $input      = $this->getApplication()->getInput();
        $repository = $this->authenticationHelper->getCredentialsRepository();

        // Retrieve data from the request
        $credentialId = $input->getBase64('credential_id', '');

        // Is this a valid credential?
        if (empty($credentialId)) {
            $event->addResult(false);

            return;
        }

        $credentialId = base64_decode($credentialId);

        if (empty($credentialId) || !$repository->has($credentialId)) {
            $event->addResult(false);

            return;
        }

        // Make sure I am editing my own key
        try {
            $user             = $this->getApplication()->getIdentity() ?? new User();
            $credentialHandle = $repository->getUserHandleFor($credentialId);
            $myHandle         = $repository->getHandleFromUserId($user->id);
        } catch (\Exception $e) {
            $event->addResult(false);

            return;
        }

        if ($credentialHandle !== $myHandle) {
            $event->addResult(false);

            return;
        }

        // Delete the record
        try {
            $repository->remove($credentialId);
        } catch (\Exception $e) {
            $event->addResult(false);

            return;
        }

        $event->addResult(true);
    }
}

The same content is in 56 releases, Joomla 5.0.0-beta3 to 5.3.0-beta1. See them all, or every version of this file.