Search in sources :

Example 51 with CognitoUserPool

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUserPoolTest method newUserPool.

@Test
public void newUserPool() {
    // Check user pool constructors
    CognitoUserPool pool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, new ClientConfiguration());
    assertNotNull(pool);
    assertNotNull(pool.getUserPoolId());
    assertEquals(TEST_USER_POOL, pool.getUserPoolId());
    assertNotNull(pool.getClientId());
    assertEquals(TEST_CLIENT_ID, pool.getClientId());
    CognitoUserPool pool2 = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET);
    assertNotNull(pool2);
    assertNotNull(pool2.getUserPoolId());
    assertEquals(TEST_USER_POOL, pool2.getUserPoolId());
    assertNotNull(pool2.getClientId());
    assertEquals(TEST_CLIENT_ID, pool2.getClientId());
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) ClientConfiguration(com.amazonaws.ClientConfiguration) Test(org.junit.Test)

Example 52 with CognitoUserPool

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderUserPoolTest method newUserPoolWithPinpointWithCachedApp.

@Test
public void newUserPoolWithPinpointWithCachedApp() {
    // Test new user pool creation with PP app id.
    // Test shared Pinpoint context
    awsKeyValueStorageUtility = new AWSKeyValueStore(ApplicationProvider.getApplicationContext(), TEST_PP_APP_ID_1 + PP_PREFERENCES_AND_FILE_MANAGER_SUFFIX, true);
    awsKeyValueStorageUtility.put("UNIQUE_ID_KEY", TEST_PP_ENDPOINT_1);
    CognitoUserPool pool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, new ClientConfiguration(), TEST_AWS_REGION_1, TEST_PP_APP_ID_1);
    assertNotNull(pool);
    assertNotNull(pool.getUserPoolId());
    assertEquals(TEST_USER_POOL, pool.getUserPoolId());
    assertNotNull(pool.getClientId());
    assertEquals(TEST_CLIENT_ID, pool.getClientId());
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) AWSKeyValueStore(com.amazonaws.internal.keyvaluestore.AWSKeyValueStore) ClientConfiguration(com.amazonaws.ClientConfiguration) Test(org.junit.Test)

Example 53 with CognitoUserPool

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool in project aws-sdk-android by aws-amplify.

the class AWSMobileClientTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    setUpCredentials();
    Context appContext = ApplicationProvider.getApplicationContext();
    final CountDownLatch latch = new CountDownLatch(1);
    AWSMobileClient.getInstance().initialize(appContext, new Callback<UserStateDetails>() {

        @Override
        public void onResult(UserStateDetails result) {
            latch.countDown();
        }

        @Override
        public void onError(Exception e) {
            latch.countDown();
        }
    });
    latch.await();
    final AWSConfiguration awsConfiguration = AWSMobileClient.getInstance().getConfiguration();
    JSONObject userPoolConfig = awsConfiguration.optJsonObject("CognitoUserPool");
    assertNotNull(userPoolConfig);
    clientRegion = Regions.fromName(userPoolConfig.getString("Region"));
    userPoolId = userPoolConfig.getString("PoolId");
    clientId = userPoolConfig.getString("AppClientId");
    clientSecret = userPoolConfig.optString("AppClientSecret");
    JSONObject identityPoolConfig = awsConfiguration.optJsonObject("CredentialsProvider").getJSONObject("CognitoIdentity").getJSONObject("Default");
    assertNotNull(identityPoolConfig);
    identityPoolId = identityPoolConfig.getString("PoolId");
    userPool = new CognitoUserPool(appContext, userPoolId, clientId, clientSecret, clientRegion);
    deleteAllUsers(userPoolId);
    createUserViaAdminAPI(userPoolId, USERNAME_ADMIN_API_USER, EMAIL_ADMIN_API_USER);
}
Also used : Context(android.content.Context) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) JSONObject(org.json.JSONObject) CountDownLatch(java.util.concurrent.CountDownLatch) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) UsernameExistsException(com.amazonaws.services.cognitoidentityprovider.model.UsernameExistsException) JSONException(org.json.JSONException) UserNotConfirmedException(com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) BeforeClass(org.junit.BeforeClass)

Example 54 with CognitoUserPool

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool in project aws-sdk-android by aws-amplify.

the class OAuth2Utils method _initialize.

protected Runnable _initialize(final Context context, final AWSConfiguration awsConfiguration, final Callback<UserStateDetails> callback) {
    return new Runnable() {

        public void run() {
            synchronized (initLockObject) {
                if (AWSMobileClient.this.awsConfiguration != null) {
                    callback.onResult(getUserStateDetails(true));
                    return;
                }
                // Default value
                mIsPersistenceEnabled = true;
                // appropriately.
                try {
                    if (awsConfiguration.optJsonObject(AUTH_KEY) != null && awsConfiguration.optJsonObject(AUTH_KEY).has("Persistence")) {
                        mIsPersistenceEnabled = awsConfiguration.optJsonObject(AUTH_KEY).getBoolean("Persistence");
                    }
                } catch (final Exception ex) {
                    // If reading from awsconfiguration.json fails, invoke callback.
                    callback.onError(new RuntimeException("Failed to initialize AWSMobileClient; please check your awsconfiguration.json", ex));
                    return;
                }
                userAgentOverride = awsConfiguration.getUserAgentOverride();
                mContext = context.getApplicationContext();
                mStore = new AWSMobileClientStore(AWSMobileClient.this);
                final IdentityManager identityManager = new IdentityManager(mContext);
                identityManager.enableFederation(false);
                identityManager.setConfiguration(awsConfiguration);
                identityManager.setPersistenceEnabled(mIsPersistenceEnabled);
                IdentityManager.setDefaultIdentityManager(identityManager);
                registerConfigSignInProviders(awsConfiguration);
                identityManager.addSignInStateChangeListener(new SignInStateChangeListener() {

                    @Override
                    public void onUserSignedIn() {
                        Log.d(TAG, "onUserSignedIn: Updating user state from drop-in UI");
                        signInState = SignInState.DONE;
                        com.amazonaws.mobile.auth.core.IdentityProvider currentIdentityProvider = identityManager.getCurrentIdentityProvider();
                        String token = currentIdentityProvider.getToken();
                        String providerKey = currentIdentityProvider.getCognitoLoginKey();
                        federatedSignInWithoutAssigningState(providerKey, token, new Callback<UserStateDetails>() {

                            @Override
                            public void onResult(UserStateDetails result) {
                                Log.d(TAG, "onResult: showSignIn federated");
                                setUserState(getUserStateDetails(false));
                                getSignInUILatch().countDown();
                            }

                            @Override
                            public void onError(Exception e) {
                                Log.w(TAG, "onError: User sign-in had errors from drop-in UI", e);
                                setUserState(getUserStateDetails(false));
                                getSignInUILatch().countDown();
                            }
                        });
                    }

                    @Override
                    public void onUserSignedOut() {
                        Log.d(TAG, "onUserSignedOut: Updating user state from drop-in UI");
                        setUserState(getUserStateDetails(false));
                        showSignInWaitLatch.countDown();
                    }
                });
                if (awsConfiguration.optJsonObject("CredentialsProvider") != null && awsConfiguration.optJsonObject("CredentialsProvider").optJSONObject("CognitoIdentity") != null) {
                    try {
                        JSONObject identityPoolJSON = awsConfiguration.optJsonObject("CredentialsProvider").getJSONObject("CognitoIdentity").getJSONObject(awsConfiguration.getConfiguration());
                        final String poolId = identityPoolJSON.getString("PoolId");
                        final String regionStr = identityPoolJSON.getString("Region");
                        final ClientConfiguration clientConfig = new ClientConfiguration();
                        clientConfig.setUserAgent(DEFAULT_USER_AGENT + " " + awsConfiguration.getUserAgent());
                        if (userAgentOverride != null) {
                            clientConfig.setUserAgentOverride(userAgentOverride);
                        }
                        AmazonCognitoIdentityClient cibClient = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials(), clientConfig);
                        cibClient.setRegion(Region.getRegion(regionStr));
                        provider = new AWSMobileClientCognitoIdentityProvider(null, poolId, cibClient);
                        cognitoIdentity = new CognitoCachingCredentialsProvider(mContext, provider, Regions.fromName(regionStr));
                        cognitoIdentity.setPersistenceEnabled(mIsPersistenceEnabled);
                        if (userAgentOverride != null) {
                            cognitoIdentity.setUserAgentOverride(userAgentOverride);
                        }
                    } catch (Exception e) {
                        callback.onError(new RuntimeException("Failed to initialize Cognito Identity; please check your awsconfiguration.json", e));
                        return;
                    }
                }
                final JSONObject userPoolJSON = awsConfiguration.optJsonObject("CognitoUserPool");
                if (userPoolJSON != null) {
                    try {
                        mUserPoolPoolId = userPoolJSON.getString("PoolId");
                        final String clientId = userPoolJSON.getString("AppClientId");
                        final String clientSecret = userPoolJSON.optString("AppClientSecret");
                        final String pinpointEndpointId = CognitoPinpointSharedContext.getPinpointEndpoint(context, userPoolJSON.optString("PinpointAppId"));
                        final String cognitoUserPoolCustomEndpoint = userPoolJSON.optString(COGNITO_USERPOOL_CUSTOM_ENDPOINT);
                        final ClientConfiguration clientConfig = new ClientConfiguration();
                        clientConfig.setUserAgent(DEFAULT_USER_AGENT + " " + awsConfiguration.getUserAgent());
                        if (userAgentOverride != null) {
                            clientConfig.setUserAgentOverride(userAgentOverride);
                        }
                        userpoolLL = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), clientConfig);
                        userpoolLL.setRegion(com.amazonaws.regions.Region.getRegion(Regions.fromName(userPoolJSON.getString("Region"))));
                        userpoolsLoginKey = String.format("cognito-idp.%s.amazonaws.com/%s", userPoolJSON.getString("Region"), userPoolJSON.getString("PoolId"));
                        userpool = new CognitoUserPool(mContext, mUserPoolPoolId, clientId, clientSecret, userpoolLL, pinpointEndpointId, cognitoUserPoolCustomEndpoint);
                        userpool.setPersistenceEnabled(mIsPersistenceEnabled);
                        mDeviceOperations = new DeviceOperations(AWSMobileClient.this, userpoolLL);
                    } catch (Exception e) {
                        callback.onError(new RuntimeException("Failed to initialize Cognito Userpool; please check your awsconfiguration.json", e));
                        return;
                    }
                }
                JSONObject hostedUIJSON = getHostedUIJSON(awsConfiguration);
                if (hostedUIJSON != null) {
                    try {
                        // Pre-warm the Custom Tabs based on
                        if (hostedUIJSON.has("TokenURI")) {
                            Log.d(TAG, "initialize: OAuth2 client detected");
                            mOAuth2Client = new OAuth2Client(mContext, AWSMobileClient.this);
                            mOAuth2Client.setPersistenceEnabled(mIsPersistenceEnabled);
                            mOAuth2Client.setUserAgentOverride(userAgentOverride);
                        } else {
                            _initializeHostedUI(hostedUIJSON);
                        }
                    } catch (Exception e) {
                        callback.onError(new RuntimeException("Failed to initialize OAuth, please check your awsconfiguration.json", e));
                    }
                }
                if (cognitoIdentity == null && userpool == null) {
                    callback.onError(new RuntimeException("Neither Cognito Identity or Cognito UserPool was used." + " At least one must be present to use AWSMobileClient."));
                    return;
                }
                AWSMobileClient.this.awsConfiguration = awsConfiguration;
                final UserStateDetails userStateDetails = getUserStateDetails(true);
                callback.onResult(userStateDetails);
                setUserState(userStateDetails);
            }
        }
    };
}
Also used : IdentityManager(com.amazonaws.mobile.auth.core.IdentityManager) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) OAuth2Client(com.amazonaws.mobile.client.internal.oauth2.OAuth2Client) AnonymousAWSCredentials(com.amazonaws.auth.AnonymousAWSCredentials) AWSAbstractCognitoIdentityProvider(com.amazonaws.auth.AWSAbstractCognitoIdentityProvider) AmazonCognitoIdentityProvider(com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProvider) AmazonCognitoIdentityProviderClient(com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient) JSONException(org.json.JSONException) InvalidUserPoolConfigurationException(com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException) AmazonClientException(com.amazonaws.AmazonClientException) NotAuthorizedException(com.amazonaws.services.cognitoidentity.model.NotAuthorizedException) SignInStateChangeListener(com.amazonaws.mobile.auth.core.SignInStateChangeListener) CustomTabsCallback(androidx.browser.customtabs.CustomTabsCallback) InternalCallback(com.amazonaws.mobile.client.internal.InternalCallback) JSONObject(org.json.JSONObject) ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) CognitoCachingCredentialsProvider(com.amazonaws.auth.CognitoCachingCredentialsProvider) AmazonCognitoIdentityClient(com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 55 with CognitoUserPool

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool in project aws-mobile-appsync-sdk-android by awslabs.

the class AppSyncClientUnitTest method testMultipleAuth.

@Test(expected = RuntimeException.class)
public void testMultipleAuth() {
    awsConfiguration.setConfiguration("ApiKey");
    APIKeyAuthProvider apiKeyAuthProvider = new BasicAPIKeyAuthProvider(awsConfiguration);
    awsConfiguration.setConfiguration("AmazonCognitoUserPools");
    CognitoUserPool cognitoUserPool = new CognitoUserPool(shadowContext, awsConfiguration);
    BasicCognitoUserPoolsAuthProvider basicCognitoUserPoolsAuthProvider = new BasicCognitoUserPoolsAuthProvider(cognitoUserPool);
    final AWSAppSyncClient awsAppSyncClient = AWSAppSyncClient.builder().context(shadowContext).awsConfiguration(awsConfiguration).apiKey(apiKeyAuthProvider).cognitoUserPoolsAuthProvider(basicCognitoUserPoolsAuthProvider).build();
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) BasicCognitoUserPoolsAuthProvider(com.amazonaws.mobileconnectors.appsync.sigv4.BasicCognitoUserPoolsAuthProvider) APIKeyAuthProvider(com.amazonaws.mobileconnectors.appsync.sigv4.APIKeyAuthProvider) BasicAPIKeyAuthProvider(com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider) BasicAPIKeyAuthProvider(com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider) Test(org.junit.Test)

Aggregations

CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)56 Test (org.junit.Test)41 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)28 ArgumentCaptor (org.mockito.ArgumentCaptor)11 SharedPreferences (android.content.SharedPreferences)9 CognitoUser (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser)9 GetUserRequest (com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest)8 ResourceNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException)8 CognitoUserAttributes (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes)7 SignUpResult (com.amazonaws.services.cognitoidentityprovider.model.SignUpResult)7 Before (org.junit.Before)7 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)6 CognitoUserDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserDetails)6 GetDetailsHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler)6 SignUpHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.SignUpHandler)6 UpdateAttributesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler)6 SignUpRequest (com.amazonaws.services.cognitoidentityprovider.model.SignUpRequest)6 UnexpectedLambdaException (com.amazonaws.services.cognitoidentityprovider.model.UnexpectedLambdaException)6 UpdateUserAttributesRequest (com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest)6 ClientConfiguration (com.amazonaws.ClientConfiguration)4