2

I need to integrate a Joomla website with a third party REST API. Let me try to clarify what I'm trying to do. Ideally when a user logs on to the Joomla website once they authenticate they will also be able to access another website, likely via a link on the Joomla site without having to login to this other site again. There is a REST API for this other site that has various login methods such as a single user sign on, and that is what I'm thinking is what I would need to make a call to.

I'm figuring it would work something like this:

User logs into the Joomla site, the user is authenticated by Joomla. Now once the user is authenticated by Joomla system, go call the REST API and authenticate with it, if authentication was good, store the proper information received back from the API, like the AUTH TOKEN and anything else required for login to other site, in perhaps the session variables or on the clients machine in a cookie to used when trying to access the other site. I'm not sure what is the correct way to make this work and any insight into how this is done in theory or in practice would be great. I have been looking for information but only seem to find a lot of people discussing how to have a REST API for your Joomla website and not a whole lot on how to connect to an external REST API from a Joomla site.

Upon my searching for the proper way to go about this I assume it lies in either creating a plugin like the standard Joomla user authentication plugin and adding this functionality into it or creating a plugin that listens for the proper event. If I need to actually create a plugin that listens to events, I'm not sure yet if I need to listen for the onUserAuthenicate response or onUserLogin event. But I have a good feeling it is one of the two. I have some idea of how to use the curl library to talk to the API so I figure this how I would attempt it.

Any insight, examples or direction on how to do this would greatly be appreciated! Thanks!

2 Answers 2

1

You must use onUserAfterLogin as this triggered after successful login.

Authenticate is called to verify login credentials (for example you can use it to block login for users with specific IP, or to create login with email

1
  • Been a while since I wrote this post. Thank you for the suggestion. It was just the event that I needed to work with. I have setup a plugin that leverages this event onUserAfterLogin and runs the necessary code to make the call to the API via CURL and store the necessary authentication data back in the Joomla session. Now once logged into Joomla my user is authenticated also with the third party site. Also thank you Pierre for your link to the plugin tutorial. Cheers!
    – seamonkey
    Commented Oct 4, 2016 at 1:48
1

You have to create an authentification plugin see the documnentation :https://docs.joomla.org/J3.x:Creating_an_Authentication_Plugin_for_Joomla/en and my pseudo code below

    <?php
    /**
     * @version    $Id: myauth.php 7180 2007-04-23 16:51:53Z jinx $
     * @package    Joomla.Tutorials
     * @subpackage Plugins
     * @license    GNU/GPL
     */

    // Check to ensure this file is included in Joomla!
    defined('_JEXEC') or die();

    /**
     * Example Authentication Plugin.  Based on the example.php plugin in the Joomla! Core installation
     *
     * @package    Joomla.Tutorials
     * @subpackage Plugins
     * @license    GNU/GPL
     */
    class plgAuthenticationMyauth extends JPlugin
    {
        /**
         * This method should handle any authentication and report back to the subject
         * This example uses simple authentication - it checks if the password is the reverse
         * of the username (and the user exists in the database).
         *
         * @access    public
         * @param     array     $credentials    Array holding the user credentials ('username' and 'password')
         * @param     array     $options        Array of extra options
         * @param     object    $response       Authentication response object
         * @return    boolean
         * @since 1.5
         */
        function onUserAuthenticate( $credentials, $options, &$response )
        {
            /*
             * Here you would do whatever you need for an authentication routinr with the credentials.Do your api call here with curl or an other method. Assume you API return an $result->success variable
 */

if (!$result->success) {
            $response->status = STATUS_FAILURE;
            $response->error_message = 'User does not exist';
        }

            $email = JUser::getInstance($result->username); // Bring this in line with the rest of the system
            $response->email = $email->email;
            $response->status = JAuthentication::STATUS_SUCCESS;

        }
    }
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.