Search code examples
androidamazon-web-servicesapi-gateway

AWS Autogenerated SDK on Android - Clock Skew Fix


How can I fix clock skew for an android app using an AWS Autogenerated SDK using CognitoCachingCredentialsProvider along with APIClientFactory?

Most AWS SDK calls seem to have it fixed automatically--but those done by the autogenerated SDK do not.

I'm looking for something similar to setTimeOffset--see https://aws.amazon.com/blogs/mobile/managing-device-time-with-the-aws-mobile-sdks/.

Update: Or https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#correctClockSkew-property.


Solution

  • Finally figured it out. If you parse the error message, you can determine the offset for your device, then set it using:

    SDKGlobalConfiguration.setGlobalTimeOffset(offset); //in seconds
    

    Once that's set the auto-generated SDKs will take into account the offset when it signs the request.

    A couple of things to keep in mind:

    • make sure you get the positive/negative offset correct, I had to multiple * -1 to correct mine.
    • I found I needed to synchronize my api calls (ensure only 1 was called at a time) because the math required to determine the offset required using any previous offset to account for clocks changing several times throughout the lifetime of the app.

    Documentation: https://aws-amplify.github.io/aws-sdk-android/docs/reference/index.html