Search in sources :

Example 1 with InvalidParameterException

use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.

the class AWSMobileClientTest method testRevokeTokenWithSignedOutUser.

@Test
public void testRevokeTokenWithSignedOutUser() throws Exception {
    auth.signIn(username, PASSWORD, null);
    assertTrue("isSignedIn is true", auth.isSignedIn());
    final CountDownLatch revokeTokenLatch = new CountDownLatch(1);
    final CognitoUser user = userPool.getCurrentUser();
    user.getSession(new AuthenticationHandler() {

        @Override
        public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
            revokeTokenLatch.countDown();
        }

        @Override
        public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
        }

        @Override
        public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
        }

        @Override
        public void authenticationChallenge(ChallengeContinuation continuation) {
        }

        @Override
        public void onFailure(Exception exception) {
            exception.printStackTrace();
            fail("Sign in failed.");
        }
    });
    revokeTokenLatch.await(5, TimeUnit.SECONDS);
    auth.signOut();
    assertFalse("isSignedIn is false", auth.isSignedIn());
    try {
        user.revokeTokens();
    } catch (Exception e) {
        assertTrue(e instanceof InvalidParameterException);
    }
}
Also used : MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) AuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) ChallengeContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation) AuthenticationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler) CountDownLatch(java.util.concurrent.CountDownLatch) CognitoUserSession(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession) CognitoUser(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser) 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) Test(org.junit.Test)

Example 2 with InvalidParameterException

use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.

the class CognitoUser method getSession.

/**
 * getSession orchestrates the SignIn flow with Amazon Cognito UserPools.
 *
 * <p>
 *     This method is synchronous and performs network operations
 *     on the same thread in which the method is called.  Calling this
 *     method in the MainThread will result in NetworkOnMainThreadException
 * </p>
 *
 * <p>
 *     1) Read the tokens (Id, Access and Refresh) that are cached on the device.
 *      1.1) If the Id and Access tokens are present and they are valid, the
 *          {@link AuthenticationHandler#onSuccess(CognitoUserSession, CognitoDevice)}.
 *          will be called with a {@link CognitoUserSession} that has references to the valid tokens.
 *          This means that the user is signed-in.
 *      1.2) If the Id and Access tokens are expired, and if there is a valid refresh token,
 *          a network call is made to get new Id and Access tokens.
 *          If valid Id and Access tokens are retrieved, they are cached on the device
 *          and {@link AuthenticationHandler#onSuccess(CognitoUserSession, CognitoDevice)}
 *          will be called with a {@link CognitoUserSession} that has references to the valid
 *          tokens. This means that the user is signed-in.
 *
 *     2) If there are no valid tokens cached on the device, the callback method
 *          {@link AuthenticationHandler#getAuthenticationDetails(AuthenticationContinuation, String)}
 *          will be called where the {@link AuthenticationDetails} will need to be supplied
 *          to continue the SignIn operation. See
 *          {@link com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.CognitoIdentityProviderContinuation}
 *          for details on continuation objects.
 *
 *     3) In all other error scenarios, {@link AuthenticationHandler#onFailure(Exception)} will
 *          be called with the type and message of the exception and it is the responsibility of
 *          the caller to handle the exceptions appropriately.
 * </p>
 *
 * @param clientMetadata A map of custom key-value pairs that is passed to the lambda function for
 *                       custom workflow.
 * @param callback REQUIRED: {@link AuthenticationHandler} callback
 */
public void getSession(final Map<String, String> clientMetadata, final AuthenticationHandler callback) {
    if (callback == null) {
        throw new InvalidParameterException("callback is null");
    }
    try {
        getCachedSession();
        callback.onSuccess(cipSession, null);
    } catch (final InvalidParameterException e) {
        callback.onFailure(e);
    } catch (final CognitoNotAuthorizedException e) {
        final AuthenticationContinuation authenticationContinuation = new AuthenticationContinuation(this, context, AuthenticationContinuation.RUN_IN_CURRENT, callback);
        authenticationContinuation.setClientMetaData(clientMetadata);
        callback.getAuthenticationDetails(authenticationContinuation, getUserId());
    } catch (final Exception e) {
        callback.onFailure(e);
    }
}
Also used : InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) AuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation) CognitoNotAuthorizedException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CognitoInternalErrorException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException) UserNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException) CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoNotAuthorizedException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)

Example 3 with InvalidParameterException

use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderChangePasswordInstrumentedTest method changePasswordInCurrentWithNoCachedTokens.

@Test
public void changePasswordInCurrentWithNoCachedTokens() throws Exception {
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    InvalidParameterException exception = new InvalidParameterException("password change request failed");
    doThrow(exception).when(mockCSIClient).changePassword(any(ChangePasswordRequest.class));
    final FlowTracker tracker = new FlowTracker("onFailure");
    tracker.activate();
    testUser.changePasswordInBackground(TEST_USER_PASSWORD, TEST_USER_NEW_PASS, new GenericHandler() {

        @Override
        public void onSuccess() {
            assertTrue(tracker.check("onSuccess"));
        }

        @Override
        public void onFailure(Exception exception) {
            assertTrue(tracker.check("onFailure"));
            assertNotNull(exception);
        }
    });
}
Also used : InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) FlowTracker(com.amazonaws.mobileconnectors.cognitoidentityprovider.utils.FlowTracker) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) ChangePasswordRequest(com.amazonaws.services.cognitoidentityprovider.model.ChangePasswordRequest) Test(org.junit.Test)

Example 4 with InvalidParameterException

use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderChangePasswordInstrumentedTest method changePasswordInBackgroundThreadWithNoCachedTokens.

@Test
public void changePasswordInBackgroundThreadWithNoCachedTokens() throws Exception {
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    InvalidParameterException exception = new InvalidParameterException("password change request failed");
    doThrow(exception).when(mockCSIClient).changePassword(any(ChangePasswordRequest.class));
    final FlowTracker tracker = new FlowTracker("onFailure");
    tracker.activate();
    testUser.changePasswordInBackground(TEST_USER_PASSWORD, TEST_USER_NEW_PASS, new GenericHandler() {

        @Override
        public void onSuccess() {
            assertTrue(tracker.check("onSuccess"));
        }

        @Override
        public void onFailure(Exception exception) {
            assertTrue(tracker.check("onFailure"));
            assertNotNull(exception);
        }
    });
}
Also used : InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) FlowTracker(com.amazonaws.mobileconnectors.cognitoidentityprovider.utils.FlowTracker) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) ChangePasswordRequest(com.amazonaws.services.cognitoidentityprovider.model.ChangePasswordRequest) Test(org.junit.Test)

Example 5 with InvalidParameterException

use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.

the class CognitoIdentityProviderChangePasswordInstrumentedTest method changePasswordInCurrentThreadWithCachedTokensException.

// Test change password, exception when run in current thread
@Test
public void changePasswordInCurrentThreadWithCachedTokensException() throws Exception {
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    InvalidParameterException exception = new InvalidParameterException("password change request failed");
    doThrow(exception).when(mockCSIClient).changePassword(any(ChangePasswordRequest.class));
    // Store tokens in shared preferences
    SharedPreferences sharedPreferences = appContext.getSharedPreferences("CognitoIdentityProviderCache", Context.MODE_PRIVATE);
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(360000L));
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", getValidJWT(360000L));
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN);
    final FlowTracker tracker = new FlowTracker("onFailure");
    tracker.activate();
    testUser.changePassword(TEST_USER_PASSWORD, TEST_USER_NEW_PASS, new GenericHandler() {

        @Override
        public void onSuccess() {
            assertTrue(tracker.check("onSuccess"));
        }

        @Override
        public void onFailure(Exception exception) {
            assertTrue(tracker.check("onFailure"));
            ArgumentCaptor<ChangePasswordRequest> argumentCaptor = ArgumentCaptor.forClass(ChangePasswordRequest.class);
            verify(mockCSIClient).changePassword(argumentCaptor.capture());
            ChangePasswordRequest requestSent = argumentCaptor.getValue();
            assertNotNull(requestSent);
            assertEquals(TEST_USER_PASSWORD, requestSent.getPreviousPassword());
            assertEquals(TEST_USER_NEW_PASS, requestSent.getProposedPassword());
            assertNotNull(requestSent.getAccessToken());
            assertNotNull(exception);
        }
    });
}
Also used : InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) ArgumentCaptor(org.mockito.ArgumentCaptor) SharedPreferences(android.content.SharedPreferences) FlowTracker(com.amazonaws.mobileconnectors.cognitoidentityprovider.utils.FlowTracker) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) ChangePasswordRequest(com.amazonaws.services.cognitoidentityprovider.model.ChangePasswordRequest) Test(org.junit.Test)

Aggregations

InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)17 Test (org.junit.Test)15 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)9 SharedPreferences (android.content.SharedPreferences)7 GenericHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler)5 FlowTracker (com.amazonaws.mobileconnectors.cognitoidentityprovider.utils.FlowTracker)5 ChangePasswordRequest (com.amazonaws.services.cognitoidentityprovider.model.ChangePasswordRequest)5 ResourceNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException)3 ArgumentCaptor (org.mockito.ArgumentCaptor)3 CognitoUser (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser)2 CognitoUserAttributes (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes)2 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)2 CognitoUserDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserDetails)2 AuthenticationContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation)2 MultiFactorAuthenticationContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation)2 GetDetailsHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler)2 UpdateAttributesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler)2 GetUserRequest (com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest)2 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)2 UpdateUserAttributesRequest (com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest)2