authentication oauth oauth 2.0 Developer

Extract token informations from OAuth 2.0 authentication using Jahia 8 and authentication modules

Question

How to extract token informations (or any other data available in the connection results) from OAuth 2.0 authentication using jahia-authentication and jahia-oauth modules in Jahia 8 ?

Answer

It is possible to setup advance authentication with OAuth and social logins by following this documentation: Configuring OAuth modules and social login

In case you would like to access the data of user's connections like extracting useful information from the authentication token or any other data coming from the authentication system, we introduced a new extension point in jahia-authentication and jahia-oauth modules.

Assuming that you already completed the setup of OAuth and social logins on your Jahia server, here is the required module versions for the next steps (ensure they are correctly installed and started):

  • jahia-authentication (1.7.0 or higher)
  • jahia-oauth (3.3.0 or higher)

Next steps will be dedicated to code samples, our goal is to create piece of code in a separate Jahia modules that will react on OAuth user connections.

Add a dependency to your module:

<dependencies>
    <dependency>
        <groupId>org.jahia.modules</groupId>
        <artifactId>jahia-authentication</artifactId>
        <version>1.7.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Create a class implementing the interface org.jahia.modules.jahiaauth.service.ConnectorResultProcessor

package org.jahia.modules.sample;

import org.jahia.modules.jahiaauth.service.ConnectorConfig;
import org.jahia.modules.jahiaauth.service.ConnectorResultProcessor;

import java.util.Map;

public class MyCustomConnectorResultProcessor implements ConnectorResultProcessor {
    @Override
    public void execute(ConnectorConfig connectorConfig, Map<String, Object> results) {
        // TODO
    }
}

Expose the new class as an OSGI service (Option 1: using Spring):

<bean id="myCustomConnectorResultProcessor" class="org.jahia.modules.sample.MyCustomConnectorResultProcessor"/>
<osgi:service ref="myCustomConnectorResultProcessor" interface="org.jahia.modules.jahiaauth.service.ConnectorResultProcessor"/>

Expose the new class as an OSGI service (Option 2: using Annotations):

package org.jahia.modules.sample;

import org.jahia.modules.jahiaauth.service.ConnectorConfig;
import org.jahia.modules.jahiaauth.service.ConnectorResultProcessor;

import java.util.Map;

@Component(service = {ConnectorResultProcessor.class}, immediate = true)
public class MyCustomConnectorResultProcessor implements ConnectorResultProcessor {
    @Override
    public void execute(ConnectorConfig connectorConfig, Map<String, Object> results) {
        // TODO
    }
}

And that's it, our class will now be called each time a user is connecting using jahia-authentication and jahia-oauth modules.

The following data is available:

  • connectorConfig: the configuration of the connector used to establish the connection.
  • results: all the information available for the given connection:
    • tokenData: The token information
    • all the user fields available from the connector and specific to the connector used like (for GoogleApi20): email, givenName, familyName, gender, etc... 

Hint, if you want your code to be only triggered for a specific connector, you can add a condition like this:

package org.jahia.modules.sample;

import org.jahia.modules.jahiaauth.service.ConnectorConfig;
import org.jahia.modules.jahiaauth.service.ConnectorResultProcessor;

import java.util.Map;

@Component(service = {ConnectorResultProcessor.class}, immediate = true)
public class MyCustomConnectorResultProcessor implements ConnectorResultProcessor {
    @Override
    public void execute(ConnectorConfig connectorConfig, Map<String, Object> results) {
        if ("GoogleApi20".equals(connectorConfig.getConnectorName())) {
            // read token data and do stuff here ...
        }
    }
}