Search in sources :

Example 1 with CognitoCredentialsProvider

use of com.amazonaws.auth.CognitoCredentialsProvider in project aws-sdk-android by aws-amplify.

the class KinesisVideoCredentialsProviderImpl method updateCredentials.

@Override
protected KinesisVideoCredentials updateCredentials() throws KinesisVideoException {
    // Refresh the token first
    log.debug("Refreshing credentials");
    credentialsProvider.refresh();
    // Get the AWS credentials and create Kinesis Video Credentials
    final AWSCredentials awsCredentials = credentialsProvider.getCredentials();
    String sessionToken = null;
    if (awsCredentials instanceof AWSSessionCredentials) {
        final AWSSessionCredentials sessionCredentials = (AWSSessionCredentials) awsCredentials;
        sessionToken = sessionCredentials.getSessionToken();
    }
    Date expiration = KinesisVideoCredentials.CREDENTIALS_NEVER_EXPIRE;
    if (credentialsProvider instanceof CognitoCredentialsProvider) {
        final CognitoCredentialsProvider cognitoCredentialsProvider = (CognitoCredentialsProvider) credentialsProvider;
        expiration = cognitoCredentialsProvider.getSessionCredentialsExpiration();
        log.debug("Refreshed token expiration is %s", expiration);
    } else if (credentialsProvider instanceof AWSMobileClient) {
        AWSMobileClient awsMobileClient = (AWSMobileClient) credentialsProvider;
        try {
            expiration = awsMobileClient.getTokens().getAccessToken().getExpiration();
            log.debug("Refreshed token expiration is %s", expiration);
        } catch (Exception e) {
            throw new KinesisVideoException("Failed to refresh! " + e.getMessage());
        }
    }
    log.debug("Returning %scredentials with expiration %s", sessionToken == null ? "" : "session ", expiration);
    return new KinesisVideoCredentials(awsCredentials.getAWSAccessKeyId(), awsCredentials.getAWSSecretKey(), sessionToken, expiration);
}
Also used : CognitoCredentialsProvider(com.amazonaws.auth.CognitoCredentialsProvider) KinesisVideoCredentials(com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials) AWSSessionCredentials(com.amazonaws.auth.AWSSessionCredentials) AWSMobileClient(com.amazonaws.mobile.client.AWSMobileClient) AWSCredentials(com.amazonaws.auth.AWSCredentials) KinesisVideoException(com.amazonaws.kinesisvideo.common.exception.KinesisVideoException) Date(java.util.Date) KinesisVideoException(com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)

Example 2 with CognitoCredentialsProvider

use of com.amazonaws.auth.CognitoCredentialsProvider in project aws-sdk-android by aws-amplify.

the class AWSConfigurationTest method testCognitoCachingCredentialsProviderWithAWSConfiguration.

@Test
public void testCognitoCachingCredentialsProviderWithAWSConfiguration() {
    try {
        JSONObject jsonObject = new JSONObject(jsonString);
        AWSConfiguration awsConfiguration = new AWSConfiguration(jsonObject);
        assertNotNull(awsConfiguration);
        CognitoCredentialsProvider cognitoCredentialsProvider = new CognitoCredentialsProvider(awsConfiguration);
        assertNotNull(cognitoCredentialsProvider);
    } catch (JSONException e) {
        fail("Error in constructing AWSConfiguration." + e.getLocalizedMessage());
    }
}
Also used : CognitoCredentialsProvider(com.amazonaws.auth.CognitoCredentialsProvider) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Test(org.junit.Test)

Example 3 with CognitoCredentialsProvider

use of com.amazonaws.auth.CognitoCredentialsProvider in project aws-sdk-android by aws-amplify.

the class CreateLexServiceRequest method generateRequestInternal.

/**
 * Utility method for generating a request. Populates the request with a user ID from either interactionConfig or
 * the credentialsProvider, which must be an instance of CognitoCredentialsProvider.
 *
 * @param sessionAttributes Session attributes for this current transaction.
 * @param requestAttributes Attributes to add to the current request
 * @param interactionConfig The configuration for the interaction, which must contain the user ID if
 *                          credentialsProvider is not an instance of CognitoCredentialsProvider.
 * @param credentialsProvider The credentialsProvider to obtain the Cognito IdentityId. If the user ID is not set in
 *                            interactionConfig, credentialsProvider must be an instance of CognitoCredentialsProvider
 * @param mode The desired response type
 * @return a PostContentRequest with attributes and user ID, but no content
 */
private static PostContentRequest generateRequestInternal(Map<String, String> sessionAttributes, Map<String, String> requestAttributes, InteractionConfig interactionConfig, AWSCredentialsProvider credentialsProvider, ResponseType mode) {
    final PostContentRequest request = new PostContentRequest();
    request.setBotName(interactionConfig.getBotName());
    request.setBotAlias(interactionConfig.getBotAlias());
    request.setAccept(mode.toString());
    final Map<String, String> newSessionAttributes = new HashMap<String, String>();
    newSessionAttributes.putAll(interactionConfig.getGlobalSessionAttributes());
    if (sessionAttributes != null) {
        newSessionAttributes.putAll(sessionAttributes);
    }
    request.setSessionAttributes(mapToBase64(newSessionAttributes));
    // Set the request attributes
    if (requestAttributes != null) {
        request.setRequestAttributes(mapToBase64(requestAttributes));
    }
    if (interactionConfig.getUserId() == null || interactionConfig.getUserId().isEmpty()) {
        final CognitoCredentialsProvider cognitoCredentialsProvider = (CognitoCredentialsProvider) credentialsProvider;
        request.setUserId(cognitoCredentialsProvider.getIdentityId());
    } else {
        request.setUserId(interactionConfig.getUserId());
    }
    return request;
}
Also used : CognitoCredentialsProvider(com.amazonaws.auth.CognitoCredentialsProvider) PostContentRequest(com.amazonaws.services.lexrts.model.PostContentRequest) HashMap(java.util.HashMap)

Example 4 with CognitoCredentialsProvider

use of com.amazonaws.auth.CognitoCredentialsProvider in project aws-mobile-appsync-sdk-android by awslabs.

the class AppSyncClientUnitTest method testAwsIamAuthProvider.

@Test
public void testAwsIamAuthProvider() {
    awsConfiguration.setConfiguration("AwsIam");
    final CognitoCredentialsProvider credentialsProvider = new CognitoCredentialsProvider(awsConfiguration);
    final AWSAppSyncClient awsAppSyncClient = AWSAppSyncClient.builder().context(shadowContext).awsConfiguration(awsConfiguration).credentialsProvider(credentialsProvider).build();
    assertNotNull(awsAppSyncClient);
}
Also used : CognitoCredentialsProvider(com.amazonaws.auth.CognitoCredentialsProvider) Test(org.junit.Test)

Example 5 with CognitoCredentialsProvider

use of com.amazonaws.auth.CognitoCredentialsProvider in project aws-mobile-appsync-sdk-android by awslabs.

the class AppSyncClientUnitTest method testConfigMismatch_ApiKey.

@Test(expected = RuntimeException.class)
public void testConfigMismatch_ApiKey() {
    awsConfiguration.setConfiguration("AwsIam");
    final CognitoCredentialsProvider credentialsProvider = new CognitoCredentialsProvider(awsConfiguration);
    awsConfiguration.setConfiguration("ApiKey");
    final AWSAppSyncClient awsAppSyncClient = AWSAppSyncClient.builder().context(shadowContext).awsConfiguration(awsConfiguration).credentialsProvider(credentialsProvider).build();
}
Also used : CognitoCredentialsProvider(com.amazonaws.auth.CognitoCredentialsProvider) Test(org.junit.Test)

Aggregations

CognitoCredentialsProvider (com.amazonaws.auth.CognitoCredentialsProvider)5 Test (org.junit.Test)3 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 AWSSessionCredentials (com.amazonaws.auth.AWSSessionCredentials)1 KinesisVideoCredentials (com.amazonaws.kinesisvideo.auth.KinesisVideoCredentials)1 KinesisVideoException (com.amazonaws.kinesisvideo.common.exception.KinesisVideoException)1 AWSMobileClient (com.amazonaws.mobile.client.AWSMobileClient)1 PostContentRequest (com.amazonaws.services.lexrts.model.PostContentRequest)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1