Skip to content

Commit

Permalink
[4.2] Convert basic API auth plugin to services (#37803)
Browse files Browse the repository at this point in the history
* Convert basic api auth plugin to services

* Load user by factory

* spelling

* visibility

* Update plugins/api-authentication/basic/basic.xml

Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com>

* Update plugins/api-authentication/basic/services/provider.php

Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com>

* lang files

* Update plugins/api-authentication/basic/basic.xml

Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com>

* Update plugins/api-authentication/basic/services/provider.php

Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com>

* doc

* Update plugins/api-authentication/basic/services/provider.php

Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com>

* Update plugins/api-authentication/basic/src/Basic.php

Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com>

* Extension ns

Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com>
Co-authored-by: Roland Dalmulder <contact@rolandd.com>
  • Loading branch information
3 people authored May 21, 2022
1 parent 80a99c3 commit bf95455
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 17 deletions.
8 changes: 5 additions & 3 deletions plugins/api-authentication/basic/basic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
<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>
<language tag="en-GB">language/en-GB/plg_authentication_api_basic.sys.ini</language>
<language tag="en-GB">language/en-GB/plg_api-authentication_basic.ini</language>
<language tag="en-GB">language/en-GB/plg_api-authentication_basic.sys.ini</language>
</languages>
</extension>
49 changes: 49 additions & 0 deletions plugins/api-authentication/basic/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Apiauthentication.basic
*
* @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\Extension\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('api-authentication', 'basic'),
$container->get(UserFactoryInterface::class)
);
$plugin->setDatabase($container->get(DatabaseInterface::class));

return $plugin;
}
);
}
};
Original file line number Diff line number Diff line change
@@ -1,42 +1,64 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Authentication.joomla
* @subpackage Apiauthentication.basic
*
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Plugin\ApiAuthentication\Basic\Extension;

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__
*/
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__
*/
protected $db;
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 +81,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 +103,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 +125,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 +136,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');
}
}
}

0 comments on commit bf95455

Please sign in to comment.