Search code examples
jwtquarkusquarkus-rest-client

Unable to create instance of JwtClaimsBuilder in Quarkus application


I am currently working on the Quarkus application. I have created an authentication service which will validate user and will create JWT token and send it to the client after successful authentications.

But currently, I am facing issue while creating a JWT token. For token creation, I am using

 <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-smallrye-jwt</artifactId>
 </dependency>

Please refer below code for which is used for token creation.

  public static String generateTokenString(PrivateKey privateKey, String kid,
                                         String jsonResName, Map<String, Long> timeClaims) throws Exception {
    JwtClaimsBuilder claims = Jwt.claims(jsonResName);
    long currentTimeInSecs = currentTimeInSecs();
    long exp = timeClaims != null && timeClaims.containsKey(Claims.exp.name())
            ? timeClaims.get(Claims.exp.name()) : currentTimeInSecs + 300;

    claims.issuedAt(currentTimeInSecs);
    claims.claim(Claims.auth_time.name(), currentTimeInSecs);
    claims.expiresAt(exp);

    return claims.jws().signatureKeyId(kid).sign(privateKey);
}

Once the above method is executed then JwtClaimsBuilder claims = Jwt.claims(jsonResName); throws NullPointerException as it is unable to parse JWT Claims.

Please refer to below logs

Caused by: io.smallrye.jwt.build.JwtException: Failure to parse the JWT claims:java.lang.NullPointerException
    at io.smallrye.jwt.build.impl.JwtSigningUtils.parseJwtClaims(JwtSigningUtils.java:358)
    at io.smallrye.jwt.build.impl.JwtClaimsBuilderImpl.parseJsonToClaims(JwtClaimsBuilderImpl.java:279)
    at io.smallrye.jwt.build.impl.JwtClaimsBuilderImpl.<init>(JwtClaimsBuilderImpl.java:37)
    at io.smallrye.jwt.build.impl.JwtProviderImpl.claims(JwtProviderImpl.java:35)
    at io.smallrye.jwt.build.Jwt.claims(Jwt.java:74)
    at com.keepnote.util.JwtUtil.generateTokenString(JwtUtil.java:33)
    at com.keepnote.util.JwtUtil.generateTokenString(JwtUtil.java:28)
    at com.keepnote.services.AuthenticationServiceImpl.generateToken(AuthenticationServiceImpl.java:59)
    at com.keepnote.services.AuthenticationServiceImpl.login(AuthenticationServiceImpl.java:39)
    at com.keepnote.services.AuthenticationServiceImpl_Subclass.login$$superaccessor16(AuthenticationServiceImpl_Subclass.zig:130)
    at com.keepnote.services.AuthenticationServiceImpl_Subclass$$function$$16.apply(AuthenticationServiceImpl_Subclass$$function$$16.zig:33)
    at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
    at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:119)
    at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:92)
    at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:32)
    at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:53)
    at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:26)
    at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired_Bean.intercept(TransactionalInterceptorRequired_Bean.zig:168)
    at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
    at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
    at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
    at com.keepnote.services.AuthenticationServiceImpl_Subclass.login(AuthenticationServiceImpl_Subclass.zig:78)
    at com.keepnote.resources.AuthenticationResource.login(AuthenticationResource.java:36)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:130)
    at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:621)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:487)
    at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$2(ResourceMethodInvoker.java:437)
    at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:362)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:439)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:400)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:374)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:67)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:488)
    ... 20 more
Caused by: java.lang.NullPointerException
    at java.base/java.io.Reader.<init>(Reader.java:167)
    at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at io.smallrye.jwt.build.impl.JwtSigningUtils.readJsonContent(JwtSigningUtils.java:280)
    at io.smallrye.jwt.build.impl.JwtSigningUtils.parseJwtClaims(JwtSigningUtils.java:356)
    ... 57 more


File which is being passed.

{
 "iss": "https://quarkus.io/using-jwt-rbac",
 "jti": "a-123",
 "sub": "jdoe-using-jwt-rbac",
 "upn": "[email protected]",
 "preferred_username": "Chandan",
 "aud": "using-jwt-rbac",
 "birthdate": "2001-07-13",
 "roleMappings": {
   "group1": "Group1MappedRole",
   "group2": "Group2MappedRole"
 },
 "groups": [
   "Echoer",
   "Tester",
   "Subscriber",
   "group2"
 ]
}



Solution

  • This is a bug in quarkus dev mode. Will be fixed in 1.5.0. See also here: https://github.com/quarkusio/quarkus/issues/9135