I need to use AWS cognito to take a token and then call a secret API. I have this python script that works. How can I convert it to Java SDK v2?
I found a sample that uses the Java SDK V1, but it was not working with v2.
from warrant.aws_srp import AWSSRP
username = "user"
password = "pass"
client_id = "xxxxxxxxxxxxxxxxx"
user_pool_id = "us-east-1_xxx123445"
region = "us-east-1"
aws = AWSSRP(username=username, password=password, pool_id=user_pool_id, client_id=client_id)
tokens = aws.authenticate_user()
print(tokens)
Converting the AuthenticationHelper sample class from the v1 sdk to v2 isn't too difficult. The v1 calls just need to be updated to the v2 versions. All of the SRP generation remains the same. Below is how I converted the main methods from that class to use the v2 sdk.
PerformSRPAuthentication
initiateUserSrpAuthRequest
userSrpAuthRequest
You'll also need to replace the two v1 utility classes,
com.amazonaws.util.Base64
andcom.amazonaws.util.StringUtils
. All of the instances ofStringUtils.UTF8
can be replaced withjava.nio.charset.Charset.forName("UTF-8")
. ReplacingBase64
is a bit trickier. I ended up just copying the 4 related classes into my project locally. Those beingIts not pretty, but it works for me. I'm not sure why AWS can't implement a wrapper for the SRP auth like all the other SDK's.