googleapis / google-auth-library-java

Open source Auth client library for Java
https://developers.google.com/identity
BSD 3-Clause "New" or "Revised" License
410 stars 227 forks source link

While creating token seeing java.io.IOException #1496

Open BhuvanaBilagi opened 2 months ago

BhuvanaBilagi commented 2 months ago

Issue is : while creating token we are facing intermittent connectivity issue that to only in PROD. I am using the correct service account JSON file, which I have verified.

By using Java code, I am creating a token. The code is as below. Also I have shared the error msg.

Basically, from the Mulesoft API, we are trying to call FCM, and to create a token, we have implemented the JAVA code. Due to security purposes, we are storing all the values which are present in the JSON file to Azure Vault. In our Mulesoft API, I have created a variable to store the values after reading them from Azure Vault and passing it to the Java code.

Google Cloud OAuth Access Tokens are short-lived. The default and maximum expiration time is 3,600 seconds, which is 1 hour. So, we are storing the token in ObjectStore (TempDb) for 3,000 seconds (slightly less than an hour). After 3,000 seconds, the token will be removed from ObjectStore, and we invoke the Java code to get a new token. Again, we store the fresh token in ObjectStore.

Can anybody help to resolve this issue?

import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger;

import com.google.auth.oauth2.GoogleCredentials;

public class CreateBearerToken {

private static final String MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
private static final String[] SCOPES = { MESSAGING_SCOPE };

public static String getToken(String fcmServerDetails) {
    String token = null;
    final String METHOD_NAME= "getToken(String fcmServerDetails)";

    try {
        logger.entering(CLASS_NAME, "getToken()");
        String jsonString = fcmServerDetails;              
        InputStream inputStream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));

        GoogleCredentials googleCredentials = GoogleCredentials.fromStream(inputStream).createScoped(Arrays.asList(SCOPES));
        googleCredentials.refresh();
        token = googleCredentials.getAccessToken().getTokenValue();
    } catch (IOException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
        e.printStackTrace();
    }catch (Exception e) {
        logger.log(Level.SEVERE,e.getMessage(),e);
        e.printStackTrace();
        throw e;
    }
    return token;
}

} In pom I have added these two dependency :

com.google.auth google-auth-library-oauth2-http 0.4.0
    <dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>9.2.0</version>
    </dependency>

The error msg as below -

Error getting access token for service account: java.io.IOException: Error getting access token for service account: at com.google.auth.oauth2.ServiceAccountCrdentials.refreshAccessToken(ServiceAccountCredentials.java:227) ~[google-auth-library-oauth2-http-0.4.0.jar:?] at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:97) ~[google-auth-library-oauth2-http-0.4.0.jar:?] at com.att.clm.mule.fcm.internal.util.CreateBearerToken.getToken(CreateBearerToken.java:32) ~[att-clm-pushnotification-google-s/:?] at sun.reflect.GeneratedMethodAccessor172.invoke(Unknown Source) ~[?:?] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_402] at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_402] at org.mule.weave.v2.module.java.JavaInvokeFunction.doExecute(JavaInvokeFunction.scala:59) ~[?:?] at org.mule.weave.v2.core.functions.BaseQuternaryFunctionValue.doCall(QuaternaryFunctionValue.scala:46) ~[?:?] at org.mule.weave.v2.core.functions.BaseQuternaryFunctionValue.doCall$(QuaternaryFunctionValue.scala:45) ~[?:?] at org.mule.weave.v2.module.java.JavaInvokeFunction.doCall(JavaInvokeFunction.scala:31) ~[?:?] at org.mule.weave.v2.model.values.BaseFunctionValue.call(FunctionValue.scala:153) ~[?:?] at org.mule.weave.v2.model.values.BaseFunctionValue.call$(FunctionValue.scala:146) ~[?:?] at org.mule.weave.v2.module.java.JavaInvokeFunction.call(JavaInvokeFunction.scala:31) ~[?:?] at org.mule.weave.v2.core.functions.InterceptedFunctionValue.$anonfun$call$6(InterceptedFunctionValue.scala:116) ~[?:?] at org.mule.weave.v2.core.functions.InterceptedFunctionCallBack.doExecute(InterceptedFunctionValue.scala:274) ~[?:?] at org.mule.weave.v2.core.functions.BaseUnaryFunctionValue.call(UnaryFunctionValue.scala:40) ~[?:?] at org.mule.weave.v2.core.functions.BaseUnaryFunctionValue.call$(UnaryFunctionValue.scala:39) ~[?:?] at org.mule.weave.v2.core.functions.InterceptedFunctionCallBack.call(InterceptedFunctionValue.scala:270) ~[?:?] at org.mule.weave.v2.model.service.SecurityManagerCheckFunctionValue$.$anonfun$doExecute$2(SecurityManagerService.scala:106) ~[?:?] at org.mule.weave.v2.model.service.NoSecurityManagerService$.executeWith(SecurityManagerService.scala:69) ~[?:?] at org.mule.weave.v2.model.service.SecurityManagerCheckFunctionValue$.doExecute(SecurityManagerService.scala:106) ~[?:?] at org.mule.weave.v2.model.service.SecurityManagerCheckFunctionValue$.doExecute(SecurityManagerService.scala:100) ~[?:?] at org.mule.weave.v2.core.functions.BaseQuternaryFunctionValue.call(QuaternaryFunctionValue.scala:42) ~[?:?] at org.mule.weave.v2.core.functions.BaseQuternaryFunctionValue.call$(QuaternaryFunctionValue.scala:41) ~[?:?] at org.mule.weave.v2.model.service.SecurityManagerCheckFunctionValue$.call(SecurityManagerService.scala:100) ~[?:?] at org.mule.weave.v2.core.functions.InterceptedFunctionValue.call(InterceptedFunctionValue.scala:115) ~[?:?] at org.mule.weave.v2.interpreted.node.executors.DefaultFunctionCallExecutor.doCall(DefaultFunctionCallExecutor.scala:87) ~[?:?] at org.mule.weave.v2.interpreted.node.executors.DefaultFunctionCallExecutor.execute(DefaultFunctionCallExecutor.scala:69) ~[?:?] at org.mule.weave.v2.interpreted.node.DefaultFunctionCallNode.doExecute(FunctionCallNode.scala:27) ~[?:?] at org.mule.weave.v2.interpreted.node.ValueNode.execute(ValueNode.scala:26) ~[?:?] at org.mule.weave.v2.interpreted.node.ValueNode.execute$(ValueNode.scala:21) ~[?:?] at org.mule.weave.v2.interpreted.node.DefaultFunctionCallNode.execute(FunctionCallNode.scala:23) ~[?:?] at org.mule.weave.v2.interpreted.node.structure.function.UnaryFunctionExecutionContextAwareFunction.$anonfun$doExecute$2(ExecutionContextAwareFunctionValue.scala:239) ~[?:?] at org.mule.weave.v2.interpreted.ExecutionContext.runInFrame(ExecutionContext.scala:168) ~[?:?] at org.mule.weave.v2.interpreted.ExecutionContext.runInFrame$(ExecutionContext.scala:162) ~[?:?] at org.mule.weave.v2.interpreted.DefaultExecutionContext.runInFrame(ExecutionContext.scala:365) ~[?:?] at org.mule.weave.v2.interpreted.node.structure.function.UnaryFunctionExecutionContextAwareFunction.doExecute(ExecutionContextAwareFunctionValue.scala:239) ~[?:?] at org.mule.weave.v2.core.functions.BaseUnaryFunctionValue.call(UnaryFunctionValue.scala:40) ~[?:?] at org.mule.weave.v2.core.functions.BaseUnaryFunctionValue.call$(UnaryFunctionValue.scala:39) ~[?:?] at org.mule.weave.v2.interpreted.node.structure.function.UnaryFunctionExecutionContextAwareFunction.call(ExecutionContextAwareFunctionValue.scala:190) ~[?:?] at org.mule.weave.v2.interpreted.node.executors.UnaryFunctionExecutor.doCall(UnaryFunctionExecutor.scala:93) ~[?:?] at org.mule.weave.v2.interpreted.node.executors.UnaryFunctionExecutor.executeUnary(UnaryFunctionExecutor.scala:46) ~[?:?] at org.mule.weave.v2.interpreted.node.UnaryFunctionCallNode.doExecute(FunctionCallNode.scala:66) ~[?:?] at org.mule.weave.v2.interpreted.node.ValueNode.execute(ValueNode.scala:26) ~[?:?] at org.mule.weave.v2.interpreted.node.ValueNode.execute$(ValueNode.scala:21) ~[?:?] at org.mule.weave.v2.interpreted.node.UnaryFunctionCallNode.execute(FunctionCallNode.scala:63) ~[?:?] at org.mule.weave.v2.interpreted.node.structure.KeyValuePairNode.createKeyValuePair(KeyValuePairNode.scala:34) ~[?:?] at org.mule.weave.v2.interpreted.node.structure.KeyValuePairNode.toKeyValuePair(KeyValuePairNode.scala:29) ~[?:?] at org.mule.weave.v2.interpreted.node.structure.ObjectNode.doExecute(DynamicObjectNode.scala:74) ~[?:?] at org.mule.weave.v2.interpreted.node.ValueNode.execute(ValueNode.scala:26) ~[?:?] at org.mule.weave.v2.interpreted.node.ValueNode.execute$(ValueNode.scala:21) ~[?:?] at org.mule.weave.v2.interpreted.node.structure.ObjectNode.execute(DynamicObjectNode.scala:68) ~[?:?] at org.mule.weave.v2.interpreted.node.structure.DocumentNode.doExecute(DocumentNode.scala:26) ~[?:?] at org.mule.weave.v2.interpreted.node.ValueNode.execute(ValueNode.scala:26) ~[?:?] at org.mule.weave.v2.interpreted.node.ValueNode.execute$(ValueNode.scala:21) ~[?:?] at org.mule.weave.v2.interpreted.node.structure.DocumentNode.execute(DocumentNode.scala:11) ~[?:?] at org.mule.weave.v2.interpreted.InterpretedMappingExecutableWeave.$anonfun$writeWith$3(InterpreterMappingCompilerPhase.scala:264) ~[?:?] at org.mule.weave.v2.module.writer.WriterHelper$.writeValue(Writer.scala:161) ~[?:?] at org.mule.weave.v2.module.writer.WriterHelper$.writeAndGetResult(Writer.scala:139) ~[?:?] at org.mule.weave.v2.interpreted.InterpretedMappingExecutableWeave.writeWith(InterpreterMappingCompilerPhase.scala:264) ~[?:?] at org.mule.weave.v2.el.WeaveExpressionLanguageSession.evaluateWithTimeout(WeaveExpressionLanguageSession.scala:311) ~[?:?] at org.mule.weave.v2.el.WeaveExpressionLanguageSession.$anonfun$evaluate$3(WeaveExpressionLanguageSession.scala:120) ~[?:?] at org.mule.weave.v2.el.WeaveExpressionLanguageSession.doEvaluate(WeaveExpressionLanguageSession.scala:268) ~[?:?] at org.mule.weave.v2.el.WeaveExpressionLanguageSession.evaluate(WeaveExpressionLanguageSession.scala:119) ~[?:?] at org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor$1.evaluate(DataWeaveExpressionLanguageAdaptor.java:311) ~[mule-core-4.4.0-20240408.jar:4.4.0-20240408] at org.mule.runtime.core.internal.el.DefaultExpressionManagerSession.evaluate(DefaultExpressionManagerSession.java:105) ~[mule-core-4.4.0-20240408.jar:4.4.0-20240408] at com.mulesoft.mule.runtime.core.internal.processor.SetPayloadTransformationTarget.process(SetPayloadTransformationTarget.java:32) ~[mule-core-ee-4.4.0-20240408.jar:4.4.0-20240408] at com.mulesoft.mule.runtime.core.internal.processor.TransformMessageProcessor.lambda$0(TransformMessageProcessor.java:92) ~[mule-core-ee-4.4.0-20240408.jar:4.4.0-20240408] at java.util.Optional.ifPresent(Optional.java:159) ~[?:1.8.0_402] at com.mulesoft.mule.runtime.core.internal.processor.TransformM

jskeet commented 2 months ago

This is the .NET repository - I'll transfer your issue to the Java auth repo.

BhuvanaBilagi commented 2 months ago

Thank you!