Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.2] Convert basic API auth plugin to services #37803

Merged
merged 16 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 3 additions & 1 deletion plugins/api-authentication/basic/basic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
<authorUrl>www.joomla.org</authorUrl>
<version>4.0.0</version>
<description>PLG_API-AUTHENTICATION_BASIC_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\ApiAuthentication\Basic</namespace>
<files>
<filename plugin="basic">basic.php</filename>
<folder plugin="basic">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_authentication_api_basic.ini</language>
Expand Down
48 changes: 48 additions & 0 deletions plugins/api-authentication/basic/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Authentication.joomla
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\ApiAuthentication\Basic\Basic;

return new class implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container)
{
$container->set(
PluginInterface::class,
function (Container $container)
{
$plugin = new Basic(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('system', 'cache'),
$container->get(UserFactoryInterface::class)
);
$plugin->setDatabase($container->get(DatabaseInterface::class));

return $plugin;
}
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,59 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Plugin\ApiAuthentication\Basic;

defined('_JEXEC') or die;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Authentication\Authentication;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\User\User;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\CMS\User\UserHelper;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Event\DispatcherInterface;

/**
* Joomla Authentication plugin
*
* @since 4.0.0
*/
class PlgApiAuthenticationBasic extends CMSPlugin
final class Basic extends CMSPlugin
{
use DatabaseAwareTrait;

/**
* The application object
*
* @var \Joomla\CMS\Application\CMSApplicationInterface
* @var CMSApplicationInterface
* @since 4.0.0
*/
protected $app;

/**
* The application object
* The user factory
*
* @var \Joomla\Database\DatabaseInterface
* @since 4.0.0
* @var UserFactoryInterface
*
* @since __DEPLOY_VERSION__
*/
protected $db;
private $userFactory;

/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param UserFactoryInterface $userFactory The user factory
*
* @since __DEPLOY_VERSION__
*/
public function __construct(DispatcherInterface $dispatcher, array $config, UserFactoryInterface $userFactory)
{
parent::__construct($dispatcher, $config);

$this->userFactory = $userFactory;
}

/**
* This method should handle any authentication and report back to the subject
Expand All @@ -59,12 +82,12 @@ public function onUserAuthenticate($credentials, $options, &$response)
if ($password === '')
{
$response->status = Authentication::STATUS_FAILURE;
$response->error_message = Text::_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED');
$response->error_message = $this->app->getLanguage()->_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED');

return;
}

$db = $this->db;
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select($db->quoteName(['id', 'password']))
->from($db->quoteName('#__users'))
Expand All @@ -81,7 +104,7 @@ public function onUserAuthenticate($credentials, $options, &$response)
if ($match === true)
{
// Bring this in line with the rest of the system
$user = User::getInstance($result->id);
$user = $this->userFactory->loadUserById($result->id);
$response->email = $user->email;
$response->fullname = $user->name;
$response->username = $username;
Expand All @@ -103,7 +126,7 @@ public function onUserAuthenticate($credentials, $options, &$response)
{
// Invalid password
$response->status = Authentication::STATUS_FAILURE;
$response->error_message = Text::_('JGLOBAL_AUTH_INVALID_PASS');
$response->error_message = $this->app->getLanguage()->_('JGLOBAL_AUTH_INVALID_PASS');
}
}
else
Expand All @@ -114,7 +137,7 @@ public function onUserAuthenticate($credentials, $options, &$response)

// Invalid user
$response->status = Authentication::STATUS_FAILURE;
$response->error_message = Text::_('JGLOBAL_AUTH_NO_USER');
$response->error_message = $this->app->getLanguage()->_('JGLOBAL_AUTH_NO_USER');
}
}
}