Pass Data Variable From REST API to Joomla Plugin

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
teojerah
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Mar 06, 2009 1:47 am

Pass Data Variable From REST API to Joomla Plugin

Post by teojerah » Sat May 29, 2021 2:05 pm

I have created a REST Api to check if user account is valid in LDAP. When the REST API is call, it will received 2 variables username and pwd which i will verify with my authentication plugin LDAP. If the account is valid it will return a true and false if the account is not valid.
Below is my code from REST Api plugin ;

Code: Select all

<?php
defined('_JEXEC') or die;
class LdapapiApiResourceQuery extends ApiResource
{
    public function post() {
        // Instantiate the application. 
        $app = JFactory::getApplication();
        jimport('joomla.plugin.helper');
        $app =  JFactory::getApplication();
        $data = json_decode(file_get_contents('php://input'), true);
        $username = $data["username"];
        $password = $data["pwd"];
        if ($app->login(array('username'=>$username,'password'=>$password))) {
            $this->plugin->setResponse(true);
        }else{
            $this->plugin->setResponse(false);
        }
    }
    
}
When i $app->login(array('username'=>$username,'password'=>$password)) from the above api plugin, it will go to the below LDAP Authentication plugin.

Doing a var_dump($credentials), the array return from $username and $password string is empty.

Code: Select all

<?php

defined('_JEXEC') or die;

use Joomla\Ldap\LdapClient;

/**
 * LDAP Authentication Plugin
 *
 * @since  1.5
 */
class plgAuthenticationStmLDAP extends JPlugin
{
    public function onUserAuthenticate($credentials, $options, &$response)
    {
        var_dump($credentials);
        exit;
        
[code]

Can someone help me understand what went wrong with the above implementation.

Thank you in advance

alexandreelise
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Fri Jun 22, 2018 3:14 pm

Re: Pass Data Variable From REST API to Joomla Plugin

Post by alexandreelise » Wed Jan 05, 2022 4:15 am

Hello @teojerah,

I saw one possible issue with your code on this part.

Code: Select all

$app->login(array('username'=>$username,'password'=>$password, 'remember' => true, 'silent' => true))
The important part here is silent => true it means that rather than throwing an Exception on failure it return a boolean true/false true on login success and false on failure which is what you want.

One side note about security. I hope that you hash the password with a unique salt using something like password_hash function in php. Otherwise with a clear text password you might have security issues.

Anyway, hope it helps. Take care Super Joomler.


Locked

Return to “Joomla! 3.x Coding”