Search in sources :

Example 21 with CognitoUserPool

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

the class CognitoIdentityProviderForgotPasswordTest method forgotPasswordInCurrentThreadWrongCode.

@Test
public void forgotPasswordInCurrentThreadWrongCode() throws Exception {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    final FlowTracker tracker;
    tracker = new FlowTracker("getResetCode");
    tracker.activate();
    doReturn(TEST_FORGOT_PASSWORD_RESPONSE).when(mockCSIClient).forgotPassword(any(ForgotPasswordRequest.class));
    testUser.forgotPassword(new ForgotPasswordHandler() {

        public void onSuccess() {
            // Check the flow
            assertTrue(tracker.check("onSuccess"));
        }

        public void getResetCode(ForgotPasswordContinuation var1) {
            assertTrue(tracker.check("getResetCode"));
            // Except to fail
            tracker.setNext("onFailure");
            InvalidParameterException exception = new InvalidParameterException("password change request failed");
            doThrow(exception).when(mockCSIClient).confirmForgotPassword(any(ConfirmForgotPasswordRequest.class));
            var1.setPassword(TEST_USER_PASSWORD);
            var1.setVerificationCode(TEST_CODE);
            var1.continueTask();
        }

        public void onFailure(Exception var1) {
            assertTrue(tracker.check("onFailure"));
        }
    });
}
Also used : InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 22 with CognitoUserPool

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

the class CognitoIdentityProviderForgotPasswordTest method confirmPasswordInBackgroundNoCallback.

@Test
public void confirmPasswordInBackgroundNoCallback() throws Exception {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    doReturn(TEST_CONFIRM_PASSWORD_RESPONSE).when(mockCSIClient).confirmForgotPassword(any(ConfirmForgotPasswordRequest.class));
    try {
        testUser.confirmPasswordInBackground(TEST_CODE, TEST_USER_PASSWORD, null);
    } catch (final CognitoParameterInvalidException exception) {
        return;
    }
    assertTrue("CognitoParameterInvalidException is not thrown when the callback to confirmPassword in null.", true);
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) Test(org.junit.Test)

Example 23 with CognitoUserPool

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

the class CognitoIdentityProviderSignInUserTest method clearTokensWhenNoCachedTokens.

// Clear cached tokens when no cached tokens are available
@Test
public void clearTokensWhenNoCachedTokens() throws Exception {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    testUser.signOut();
    assertNull(awsKeyValueStorageUtility.get("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken"));
    assertNull(awsKeyValueStorageUtility.get("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken"));
    assertNull(awsKeyValueStorageUtility.get("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken"));
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) Test(org.junit.Test)

Example 24 with CognitoUserPool

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

the class CognitoIdentityProviderSignInUserTest method init.

@Before
public void init() throws Exception {
    // Initialization function
    MockitoAnnotations.initMocks(this);
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    String challengeNameNull = null;
    // Set challenge (response from service) for user SRP auth
    Map<String, String> initUserSRPAuthchallengeParameters = new HashMap<String, String>();
    initUserSRPAuthchallengeParameters.put("SRP_B", BigInteger.valueOf(3).toString(16));
    initUserSRPAuthchallengeParameters.put("SALT", BigInteger.valueOf(3).toString(16));
    initUserSRPAuthchallengeParameters.put("SECRET_BLOCK", "TEST_SECRET_BLOCK");
    initUserSRPAuthchallengeParameters.put("USER_ID_FOR_SRP", TEST_USER_NAME);
    initUserSRPAuthchallengeParameters.put("USERNAME", TEST_USER_NAME);
    TEST_VALID_INITIATE_USER_SRP_AUTH_RESPONSE = new InitiateAuthResult();
    TEST_VALID_INITIATE_USER_SRP_AUTH_RESPONSE.setChallengeName("PASSWORD_VERIFIER");
    TEST_VALID_INITIATE_USER_SRP_AUTH_RESPONSE.setSession("DummySession");
    TEST_VALID_INITIATE_USER_SRP_AUTH_RESPONSE.setChallengeParameters(initUserSRPAuthchallengeParameters);
    TEST_VALID_INITIATE_USER_SRP_AUTH_RESPONSE.setAuthenticationResult(null);
    // Set challenge (response from service) for user MFA challenge
    Map<String, String> mfaChallengeParameters = new HashMap<String, String>();
    mfaChallengeParameters.put("username", TEST_USER_NAME);
    mfaChallengeParameters.put("CODE_DELIVERY_DESTINATION", TEST_CODE_DESTINA);
    mfaChallengeParameters.put("CODE_DELIVERY_DELIVERY_MEDIUM", TEST_CODE_DEL_MED);
    TEST_VALID_MFA_CHALLENGE_RESPONSE = new RespondToAuthChallengeResult();
    TEST_VALID_MFA_CHALLENGE_RESPONSE.setChallengeName("SMS_MFA");
    TEST_VALID_MFA_CHALLENGE_RESPONSE.setSession("DummyMFASession");
    TEST_VALID_MFA_CHALLENGE_RESPONSE.setChallengeParameters(mfaChallengeParameters);
    TEST_VALID_MFA_CHALLENGE_RESPONSE.setAuthenticationResult(null);
    // Set challenge (response from service) for device authentication
    Map<String, String> deviceAuthChallengeParameters = new HashMap<String, String>();
    deviceAuthChallengeParameters.put("username", TEST_USER_NAME);
    TEST_VALID_DEVICE_AUTH_RESPONSE = new RespondToAuthChallengeResult();
    TEST_VALID_DEVICE_AUTH_RESPONSE.setChallengeName("DEVICE_SRP_AUTH");
    TEST_VALID_DEVICE_AUTH_RESPONSE.setChallengeParameters(deviceAuthChallengeParameters);
    TEST_VALID_DEVICE_AUTH_RESPONSE.setAuthenticationResult(null);
    // Set challenge (response from service) for device SRP verification
    Map<String, String> deviceSRPChallengeParameters = new HashMap<String, String>();
    deviceSRPChallengeParameters.put("SRP_B", BigInteger.valueOf(3).toString(16));
    deviceSRPChallengeParameters.put("SALT", BigInteger.valueOf(3).toString(16));
    deviceSRPChallengeParameters.put("SECRET_BLOCK", "TEST_SECRET_BLOCK");
    deviceSRPChallengeParameters.put("USERNAME", TEST_USER_NAME);
    TEST_VALID_DEVICE_SRP_RESPONSE = new RespondToAuthChallengeResult();
    TEST_VALID_DEVICE_SRP_RESPONSE.setChallengeName("DEVICE_SRP_AUTH");
    TEST_VALID_DEVICE_SRP_RESPONSE.setChallengeParameters(deviceSRPChallengeParameters);
    TEST_VALID_DEVICE_SRP_RESPONSE.setAuthenticationResult(null);
    // Set challenge (response from service) with tokens and no device - for when authentication is successful
    AuthenticationResultType tokensWithoutDevice = new AuthenticationResultType();
    tokensWithoutDevice.setAccessToken(getValidJWT(3600L));
    tokensWithoutDevice.setIdToken(getValidJWT(3600L));
    tokensWithoutDevice.setRefreshToken(TEST_NEW_RTOKEN);
    tokensWithoutDevice.setTokenType("DUMMY");
    tokensWithoutDevice.setExpiresIn(10);
    tokensWithoutDevice.setNewDeviceMetadata(null);
    TEST_VALID_SUCCESSFUL_AUTH_RESPONSE = new RespondToAuthChallengeResult();
    TEST_VALID_SUCCESSFUL_AUTH_RESPONSE.setChallengeName(challengeNameNull);
    TEST_VALID_SUCCESSFUL_AUTH_RESPONSE.setChallengeParameters(null);
    TEST_VALID_SUCCESSFUL_AUTH_RESPONSE.setAuthenticationResult(tokensWithoutDevice);
    // Set challenge (response from service) with tokens and new device - for when authentication is successful
    NewDeviceMetadataType newUserDevice = new NewDeviceMetadataType();
    newUserDevice.setDeviceKey(TEST_DEVICE_KEY);
    newUserDevice.setDeviceGroupKey(TEST_DEV_GRP_KEY);
    AuthenticationResultType tokensWithNewDevice = new AuthenticationResultType();
    tokensWithNewDevice.setAccessToken(getValidJWT(3600L));
    tokensWithNewDevice.setIdToken(getValidJWT(3600L));
    tokensWithNewDevice.setRefreshToken(TEST_NEW_RTOKEN);
    tokensWithNewDevice.setTokenType("DUMMY");
    tokensWithNewDevice.setExpiresIn(10);
    tokensWithNewDevice.setNewDeviceMetadata(newUserDevice);
    TEST_VALID_SUCCESSFUL_AUTH_WITH_NEW_DEVICE_RESPONSE = new RespondToAuthChallengeResult();
    TEST_VALID_SUCCESSFUL_AUTH_WITH_NEW_DEVICE_RESPONSE.setChallengeName(challengeNameNull);
    TEST_VALID_SUCCESSFUL_AUTH_WITH_NEW_DEVICE_RESPONSE.setChallengeParameters(null);
    TEST_VALID_SUCCESSFUL_AUTH_WITH_NEW_DEVICE_RESPONSE.setAuthenticationResult(tokensWithNewDevice);
    // Set challenge (response from service) for user SRP auth
    Map<String, String> initUserPasswordAuthchallengeParameters = new HashMap<String, String>();
    initUserSRPAuthchallengeParameters.put("USER_ID_FOR_SRP", TEST_USER_NAME);
    initUserSRPAuthchallengeParameters.put("USERNAME", TEST_USER_NAME);
    TEST_VALID_INITIATE_USER_PASSWORD_AUTH_RESPONSE = new InitiateAuthResult();
    TEST_VALID_INITIATE_USER_PASSWORD_AUTH_RESPONSE.setChallengeName("PASSWORD_VERIFIER");
    TEST_VALID_INITIATE_USER_PASSWORD_AUTH_RESPONSE.setSession("DummySession");
    TEST_VALID_INITIATE_USER_PASSWORD_AUTH_RESPONSE.setChallengeParameters(initUserPasswordAuthchallengeParameters);
    TEST_VALID_INITIATE_USER_PASSWORD_AUTH_RESPONSE.setAuthenticationResult(tokensWithoutDevice);
    awsKeyValueStorageUtility = getAWSKeyValueStorageUtility(testPool);
}
Also used : NewDeviceMetadataType(com.amazonaws.services.cognitoidentityprovider.model.NewDeviceMetadataType) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) InitiateAuthResult(com.amazonaws.services.cognitoidentityprovider.model.InitiateAuthResult) HashMap(java.util.HashMap) RespondToAuthChallengeResult(com.amazonaws.services.cognitoidentityprovider.model.RespondToAuthChallengeResult) AuthenticationResultType(com.amazonaws.services.cognitoidentityprovider.model.AuthenticationResultType) Before(org.junit.Before)

Example 25 with CognitoUserPool

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

the class CognitoIdentityProviderSignInUserTest method getSessionWithUserPasswordInBackgroundThreadNoCachedTokensNoMFA.

// Authenticate user with user password auth in current thread
// Failing due to Mockito error
@Ignore
@Test
public void getSessionWithUserPasswordInBackgroundThreadNoCachedTokensNoMFA() throws Exception {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    assertNotNull(testUser.getUserId());
    // Test with a user cached in shared preferences, with no cached tokens
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + ".LastAuthUser", TEST_USER_NAME);
    // Set mock returns for Authentication API Calls
    // API call to start forgot-password flow
    doReturn(TEST_VALID_INITIATE_USER_SRP_AUTH_RESPONSE).when(mockCSIClient).initiateAuth(any(InitiateAuthRequest.class));
    // Tracker to validate the callback sequence -> callback.getAuthenticationDetails() -> onSuccess()
    final FlowTracker tracker = new FlowTracker("getAuthenticationDetails");
    System.out.println("Get session");
    testUser.getSessionInBackground(new AuthenticationHandler() {

        @Override
        public void onSuccess(CognitoUserSession session, CognitoDevice device) {
            // Check if this method was called out of sequence
            assertTrue(tracker.check("onSuccess"));
            // Extract the arguments passed to the initiateAuth API call
            ArgumentCaptor<InitiateAuthRequest> argumentCaptor = ArgumentCaptor.forClass(InitiateAuthRequest.class);
            verify(mockCSIClient).initiateAuth(argumentCaptor.capture());
            InitiateAuthRequest requestSent = argumentCaptor.getValue();
            // Verify the arguments passed in the API call
            assertNotNull(requestSent);
            assertEquals(TEST_CLIENT_ID, requestSent.getClientId());
            assertEquals("USER_PASSWORD_AUTH", requestSent.getAuthFlow());
            // Check authentication parameters - username and password.
            assertNotNull(requestSent.getAuthParameters().get("USERNAME"));
            assertNotNull(requestSent.getAuthParameters().get("PASSWORD"));
            // Verify if we have a valid session
            assertNotNull(session);
            assertTrue(session.isValid());
            // Verify that the device is null
            assertNull(device);
            tracker.setNext("end");
        }

        @Override
        public void getAuthenticationDetails(AuthenticationContinuation continuation, String username) {
            // Check if this method was called out of sequence
            assertTrue(tracker.check("getAuthenticationDetails"));
            // Check parameters
            assertNotNull(continuation);
            assertNotNull(continuation.getParameters());
            // Continue with authentication details
            Map<String, String> TEST_IN_VALIDATION_DATA = new HashMap<String, String>();
            TEST_IN_VALIDATION_DATA.put("DummyAttribute_1", "Value4DummyAttribute_1");
            TEST_IN_VALIDATION_DATA.put("DummyAttribute_2", "Value4DummyAttribute_2");
            AuthenticationDetails authDetails = new AuthenticationDetails(TEST_USER_NAME, TEST_USER_PASSWORD, TEST_IN_VALIDATION_DATA);
            authDetails.setAuthenticationType(TEST_USER_PASSWORD_AUTH_TYPE);
            tracker.setNext("onSuccess");
            continuation.setAuthenticationDetails(authDetails);
            // Set challenge response for valid
            doReturn(TEST_VALID_INITIATE_USER_PASSWORD_AUTH_RESPONSE).when(mockCSIClient).initiateAuth(any(InitiateAuthRequest.class));
            doReturn(TEST_VALID_SUCCESSFUL_AUTH_RESPONSE).when(mockCSIClient).respondToAuthChallenge(any(RespondToAuthChallengeRequest.class));
            continuation.continueTask();
        }

        @Override
        public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
            // Check if this method was called out of sequence
            assertTrue(tracker.check("getMFACode"));
        }

        @Override
        public void onFailure(Exception exception) {
            // Check if this method was called out of sequence
            assertTrue(tracker.check("onFailure"));
        }

        @Override
        public void authenticationChallenge(ChallengeContinuation continuation) {
            // Check if this method was called out of sequence
            assertTrue(tracker.check("authenticationChallenge"));
        }
    });
}
Also used : InitiateAuthRequest(com.amazonaws.services.cognitoidentityprovider.model.InitiateAuthRequest) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) ArgumentCaptor(org.mockito.ArgumentCaptor) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) AuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation) ChallengeContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation) AuthenticationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler) FlowTracker(com.amazonaws.mobileconnectors.cognitoidentityprovider.utils.FlowTracker) CognitoUserSession(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession) HashMap(java.util.HashMap) Map(java.util.Map) AuthenticationDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails) Ignore(org.junit.Ignore) 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