Amazon Cognito developer authenticated identity with Java SDK

耗尽温柔 提交于 2019-12-03 08:48:13

The last comment from Jeff led me to the answer. Thanks Jeff!

String cognitoIdentityId = "your user's identity id";
String openIdToken = "open id token for the user created on backend";

Map<String,String> logins = new HashMap<>();
logins.put("cognito-identity.amazonaws.com", openIdToken);
GetCredentialsForIdentityRequest getCredentialsRequest =
    new GetCredentialsForIdentityRequest()
    .withIdentityId(cognitoIdentityId)
    .withLogins(logins);
AmazonCognitoIdentityClient cognitoIdentityClient = new AmazonCognitoIdentityClient();
GetCredentialsForIdentityResult getCredentialsResult = cognitoIdentityClient.getCredentialsForIdentity(getCredentialsRequest);
Credentials credentials = getCredentialsResult.getCredentials();
AWSSessionCredentials sessionCredentials = new BasicSessionCredentials(
    credentials.getAccessKeyId(),
    credentials.getSecretKey(),
    credentials.getSessionToken()
);

AmazonS3Client s3Client = new AmazonS3Client(sessionCredentials);
...

If that's the route you want to go, you can find this role in the IAM console, named Cognito_(Auth|Unauth)_DefaultRole. These are what Cognito generated and linked to your pool, and you can get the ARN from there.

This blog post may be of some assistance. All of the APIs the SDK uses to communicate with Cognito to get credentials are exposed in the Java SDK, you just need to use your own back end to get the token itself. Once you have it, you can set the logins the same way you normally would with another provider and it'll all work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!