Example usage for com.amazonaws.services.cognitoidentity.model GetCredentialsForIdentityResult getCredentials

List of usage examples for com.amazonaws.services.cognitoidentity.model GetCredentialsForIdentityResult getCredentials

Introduction

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

Prototype


public Credentials getCredentials() 

Source Link

Document

Credentials for the provided identity ID.

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//w  w w  .  j  a v a2s  . c  om
 */
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;
}

From source file:io.fineo.client.auth.CognitoCredentialsProvider.java

License:Open Source License

/**
 * Gets the session credentials from Amazon Cognito.
 *///from www  . j  a v a  2s .  c  om
private void populateCredentialsWithCognito(String token) {

    // For Cognito-authenticated identities token will always be null, but
    // for developer-authenticated identities, refresh() may return a token
    // that the the developer backend has received from Cognito and we have
    // to send back in our request.
    Map<String, String> logins;
    if (token != null && !token.isEmpty()) {
        logins = new HashMap<String, String>();
        logins.put("cognito-identity.amazonaws.com", token);
    } else {
        logins = getLogins();
    }

    GetCredentialsForIdentityRequest request = new GetCredentialsForIdentityRequest()
            .withIdentityId(getIdentityId()).withLogins(logins).withCustomRoleArn(customRoleArn);

    GetCredentialsForIdentityResult result = null;

    try {
        result = cib.getCredentialsForIdentity(request);
    } catch (ResourceNotFoundException rnfe) {
        // If the identity id or identity pool is non-existant, this is
        // thrown
        result = retryGetCredentialsForIdentity();
    } catch (AmazonServiceException ase) {
        // If it's a corrupt id, then a validation exception is thrown
        if (ase.getErrorCode().equals("ValidationException")) {
            result = retryGetCredentialsForIdentity();
        } else {
            throw ase;
        }
    }

    com.amazonaws.services.cognitoidentity.model.Credentials credentials = result.getCredentials();
    sessionCredentials = new BasicSessionCredentials(credentials.getAccessKeyId(), credentials.getSecretKey(),
            credentials.getSessionToken());
    sessionCredentialsExpiration = credentials.getExpiration();

    if (!result.getIdentityId().equals(getIdentityId())) {
        setIdentityId(result.getIdentityId());
    }

}