where to add related dependencies .jar files used in MyCustomHandler.java (WSO2 APIM 3.2.0)

Viewed 183

I have extended the Abstract handler class and written my own custom handler class in wso2 apim. I am able to get the logs for this custom handler as well. For token validation I am using some okta dependencies in my project (okta-jwt-verifier and okta-jwt-verifier-impl). I have added the myCustomHandler.jar and other 2 jars of the above mentioned dependencies in /repository/components/lib folder. After starting the server I am not getting any error but when I hit the API request I am getting following runtime error,

java.lang.IllegalStateException: No `interface com.okta.jwt.AccessTokenVerifier$Builder` implementation found on the classpath. Have you remembered to include the okta-jwt-verifier-impl.jar in your runtime classpath?
    at com.okta.jwt.JwtVerifiers.lambda$loadService$1(JwtVerifiers.java:60) ~[okta-jwt-verifier-0.5.0.jar:0.5.0]
    at java.util.Optional.orElseThrow(Optional.java:408) ~[?:?]
    at com.okta.jwt.JwtVerifiers.loadService(JwtVerifiers.java:60) ~[okta-jwt-verifier-0.5.0.jar:0.5.0]
    at com.okta.jwt.JwtVerifiers.accessTokenVerifierBuilder(JwtVerifiers.java:51) ~[okta-jwt-verifier-0.5.0.jar:0.5.0]
    at org.wso2.carbon.test.CustomAPIAuthenticationHandler.authenticate(CustomAPIAuthenticationHandler.java:44) ~[MCM_Custom_handler.jar:?]
    at org.wso2.carbon.test.CustomAPIAuthenticationHandler.handleRequest(CustomAPIAuthenticationHandler.java:24) ~[MCM_Custom_handler.jar:?]
    at org.apache.synapse.rest.API.process(API.java:373)

My code part where I am getting error,

        AccessTokenVerifier jwtVerifier = JwtVerifiers.accessTokenVerifierBuilder()
            .setIssuer("https://{{myDomain}}/oauth2/default").setAudience("api://default")
            .setConnectionTimeout(Duration.ofSeconds(1)) // defaults to 1s
            .setRetryMaxAttempts(2) // defaults to 2
            .setRetryMaxElapsed(Duration.ofSeconds(10)) // defaults to 10s
            .build();

I think somehow the wso2apim is not able to find the jar that I have provided and /lib folder and it is searching for it in some other directory as it is runtime dependencie... OR this could be some OKTA issue that I don't know :( Can you please help me with this issue ?, I have been stuck since 1 week but did not got any breakthrough thanks in advance

My pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.wso2.carbon.test</groupId>
<artifactId>org.wso2.carbon.test</artifactId>
<packaging>bundle</packaging>
<version>1.0-SNAPSHOT</version>

<name>org.wso2.carbon.test.authenticator</name>
<url>http://maven.apache.org</url>

<repositories>
    <repository>
        <id>wso2-nexus</id>
        <name>WSO2 internal Repository</name>
        <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
        <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
            <checksumPolicy>ignore</checksumPolicy>
        </releases>
    </repository>

    <repository>
        <id>wso2.releases</id>
        <name>WSO2 internal Repository</name>
        <url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
        <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
            <checksumPolicy>ignore</checksumPolicy>
        </releases>
    </repository>

    <repository>
        <id>wso2.snapshots</id>
        <name>Apache Snapshot Repository</name>
        <url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>wso2.releases</id>
        <name>WSO2 internal Repository</name>
        <url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
        <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
            <checksumPolicy>ignore</checksumPolicy>
        </releases>
    </pluginRepository>

    <pluginRepository>
        <id>wso2.snapshots</id>
        <name>WSO2 Snapshot Repository</name>
        <url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </pluginRepository>
    <pluginRepository>
        <id>wso2-nexus</id>
        <name>WSO2 internal Repository</name>
        <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
        <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
            <checksumPolicy>ignore</checksumPolicy>
        </releases>
    </pluginRepository>
</pluginRepositories>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>1.4.0</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>org.wso2.carbon.test</Bundle-SymbolicName>
                    <Bundle-Name>org.wso2.carbon.test</Bundle-Name>
                    <Import-Package>
                        com.okta.jwt,
                        *; resolution:=optional
                    </Import-Package>
                    <Export-Package>
                        org.wso2.carbon.test.*,
                    </Export-Package>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.apache.synapse</groupId>
        <artifactId>synapse-core</artifactId>
        <version>2.1.7-wso2v183</version>
        <scope>provided</scope>

    </dependency>
    <dependency>
        <groupId>org.apache.synapse</groupId>
        <artifactId>synapse-commons</artifactId>
        <version>2.1.7-wso2v183</version>
        <scope>provided</scope>

    </dependency>
    <dependency>
        <groupId>org.wso2.carbon.apimgt</groupId>
        <artifactId>org.wso2.carbon.apimgt.gateway</artifactId>
        <version>6.7.206</version>
        <scope>provided</scope>

    </dependency>
    <dependency>
        <groupId>com.okta.jwt</groupId>
        <artifactId>okta-jwt-verifier</artifactId>
        <version>0.5.0</version>
    </dependency>

    <dependency>
        <groupId>com.okta.jwt</groupId>
        <artifactId>okta-jwt-verifier-impl</artifactId>
        <version>0.5.0</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

References that I followed

For writing Custom handler

OKTA validation

2 Answers

Make sure to update the pom file for your project you implemented with okta-jwt-verifier and okta-jwt-verifier-impl dependency. You should add dependencies in pom.xml like below. Check only versions of using jars.

<dependencies>
<!-- add to have class path -->
    <dependency>
        <groupId>com.okta.jwt</groupId>
        <artifactId>okta-jwt-verifier</artifactId>
        <version>0.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.okta.jwt</groupId>
        <artifactId>okta-jwt-verifier-impl</artifactId>
        <version>0.4.0</version>
    </dependency>
</dependencies>

My problem got solved, when I created an OSGI bundle with eclipse plugin with all the external jars that were needed for token validation. Steps to follow.

  1. Create an osgi project in eclipse select option add external jar and add the jars that are needed.
  2. Export the bundle in say mybundle.jar
  3. Copy this bundle to /component/dropins/ directory
  4. Copy your project myCustomHandler.jar to /component/lib directory
  5. Restart/Start the server

If you get any conflicts error just remove those jars from bundle and if you get any class not found error add those jar to bundle and export it.

Related