Example usage for com.amazonaws.services.cognitoidentity.model GetCredentialsForIdentityRequest addLoginsEntry

List of usage examples for com.amazonaws.services.cognitoidentity.model GetCredentialsForIdentityRequest addLoginsEntry

Introduction

In this page you can find the example usage for com.amazonaws.services.cognitoidentity.model GetCredentialsForIdentityRequest addLoginsEntry.

Prototype

public GetCredentialsForIdentityRequest addLoginsEntry(String key, String value) 

Source Link

Usage

From source file:cf.funge.aworldofplants.provider.CognitoCredentialsProvider.java

License:Open Source License

/**
 * Retreives a set of AWS temporary credentials from Amazon Cognito using Developer Authenticated Identities.
 *
 * @param user The end user object. The identity property in the User object needs to be populated with a valid
 *             identityId and openID Token
 * @return A valid set of temporary AWS credentials
 * @throws AuthorizationException//from  w w w .  j ava2 s .co m
 */
public UserCredentials getUserCredentials(User user) throws AuthorizationException {
    if (user == null || user.getCognitoIdentityId() == null || user.getCognitoIdentityId().trim().equals("")) {
        throw new AuthorizationException("Invalid user");
    }

    GetCredentialsForIdentityRequest credsRequest = new GetCredentialsForIdentityRequest();
    credsRequest.setIdentityId(user.getCognitoIdentityId());
    credsRequest.addLoginsEntry(CognitoConfiguration.COGNITO_PROVIDER_NAME,
            user.getIdentity().getOpenIdToken());

    GetCredentialsForIdentityResult resp = identityClient.getCredentialsForIdentity(credsRequest);
    if (resp == null) {
        throw new AuthorizationException("Empty GetCredentialsForIdentity response");
    }

    UserCredentials creds = new UserCredentials();
    creds.setAccessKey(resp.getCredentials().getAccessKeyId());
    creds.setSecretKey(resp.getCredentials().getSecretKey());
    creds.setSessionToken(resp.getCredentials().getSessionToken());
    creds.setExpiration(resp.getCredentials().getExpiration().getTime());

    return creds;
}