<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=278116885016877&amp;ev=PageView&amp;noscript=1">

,

Oct 14, 2022 | 3 Minute Read

Managing Third-Party Authentications In Mautic Plugins

RAHUL SHINDE, SENIOR DRUPAL ENGINEER

Table of Contents

Introduction

The crucial part of any integration bundle is handling the authentication mechanism. Given that the basic setup and configuration for Mautic plugins that leverage the integrations bundle has already been covered. The next step is to use authentication types in the plugins.

Authentications In Mautic Plugins

The integration bundle from Mautic Core supports multiple authentication provider methods like API-based authentication, Basic Auth, OAuth1a, OAuth2, OAuth2 Two-Legged, and OAuth2 Three-Legged. It also provides authentication protocols that can be used as a Guzzle HTTP Client.

The focus of this blog is to implement Basic Auth authentication with a third-party service. For ease of understanding, the plugins developed in the previous blog will be used.

Start by enabling the plugin to have Basic Auth authentication. This can be done by following the steps mentioned below.

  • Have a form with fields for storing the basic auth credentials
  • Form/Type/AuthType.php.
  • Prepare a ‘credentials’ class to be used by the Client class.
  • Prepare a ‘client’ service class to be used by a dedicated APIConsumer class.
  • Use Client service and implement API call-related methods in APIConsumer service class.

Step 1

The plugin depends on third-party APIs to have data to manipulate. And these APIs are gated with authentication and authorization mechanisms. Basic Auth has been chosen as the authentication method for the course of this post.

Basic Auth needs a username and password to communicate with the API. This is why a form that accepts the username and password as the key is required. And this key will come in handy while connecting with API endpoints.

In this step, create a form and name it 'ConfigAuthType.php' under the 'MauticPlugin\HelloWorldBundle\Form\Type' namespace. This class extends the AbstractType class of Symfony. Implement the 'buildForm()' method to add the required field. The example code should look like this:

View the full version here.

Now, tell Mautic to pick up this form during configuration. To do so, we have to define an Integration service class implementing ConfigFormInterface, and ConfigFormAuthInterface. The ConfigFormAuthInterface is the interface that lets you specify the configuration form using the getAuthConfigFormName method.

You should name this class 'ConfigSupport' and place it under the 'MauticPlugin\HelloWorldBundle\Integration\Support.' Here are the snippets from the class ConfigSupport.

Find the complete ConfigSupport class here.

After that, let the IntegrationBunddle know about the ‘ConfigSupport’ class. To do so, add a service as integration or create a service listing with the mautic.config_integration tag. The following is the code snippet of Config.php (the plugin configuration file).

Now, at this point, you must have the following.

  • A service class to register the configuration support class.
  • A class to provide the configuration.
  • All the code changes for step 1 till here in this commit.

Step 2

The Integrations bundle uses the 'HttpFactory' class for the Basic Auth to build the HTTP client. This class needs an object called 'credentials,' consisting of all the required keys for authentication.

If you notice the 'getClient()' method of HttpFactory class under the 'Mautic\IntegrationsBundle\Auth\Provider\BasicAuth\' namespace, it needs an object of 'AuthCredentialsInterface.' This is why the next step is to create a separate class for credentials and create a new custom client to use those credentials.

For that, create a new class called 'Credentials' under MauticPlugin\HelloWorldBundle\Connection. The class should be similar to the following:

This is a trimmed version of the class. Find the full version here.

Next, you need to create a client who will make HTTP requests. Typically, this only requires a single client class if additional logic is needed. In such cases, you can call HttpFactory class and get the client like:

Apart from fetching data, you need to cache it and polish it to be easily used for Mautic's Lead entity. This is why you need to create a new class called 'client' under the namespace MauticPlugin\HelloWorldBundle\Connection. The job of the 'client' class is to get the object of ClientInterface (\GuzzleHttp\ClientInterface). If you need the full class details, follow this link here.

Here in the 'getClient()' method, you need to call the 'getCredentials()' method, which creates a 'credentials' object using API keys. You will get the client via the HttpFactory service call using the credentials object. At the end of this phase, you will have the following:

  • Credentials object to pass into the getClient() method.
  • New Client class to manipulate get() method and fetch other configuration
  • New Config.php files inside the "HelloWorldBundle/Integrations" folder to create the configuration and different integration settings.
  • Commit

Step 3

You are now ready with the entire setup to store credentials and send the request. The current plugin created a separate class called 'ApiConsumer.' This was done because there are several other get methods and API calls, so consolidating all the API methods into a single class makes it easier to manage everything.

To use the client service created via Client.php, create a service that can use this class. That way, you can reuse this class without worrying about anything else. Also, create a new service called "helloworld.connection.client" and add it to the Config.php in the other services section.

Similarly, add additional services for the ApiConsumer class to call from other services. Refer to the source code to view the entire ApiConsumer class. Here is a snippet of the get() method.

The Client service’s reference and call the get method from the Client.php are directly being used. Refer to the commit to see the code for this step.

The Conclusion

At last, you have successfully got the plugin ready to communicate with third-party API and churn out more leads. There is a lot more that you can learn about different authentication supports and mautic development, so keep exploring.

About the Author
Mohit Aghera, Axelerant Alumni
About the Author

Mohit Aghera, Axelerant Alumni


Rahul_Shinde

Rahul Shinde, Senior Drupal Engineer

Rahul’s favorite things are reading, taking long walks, and cycling. When not on his computer, he loves spending time with his family or teaching underprivileged children about technology at local events.

Leave us a comment

Back to Top