Search in sources :

Example 11 with GenericHandler

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

the class CognitoIdentityProviderDevicesTest method forgetDeviceInCurrentThread.

@Test
public void forgetDeviceInCurrentThread() {
    CognitoDeviceHelper.cacheDeviceKey(TEST_USER_NAME, TEST_USER_POOL, TEST_DEVICE_KEY, appContext);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Store tokens in shared preferences
    final String testAccessToken = getValidJWT(3600L);
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(3600L));
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", testAccessToken);
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN);
    // Set mock result for the change password request API call
    doNothing().when(mockCSIClient).forgetDevice(any(ForgetDeviceRequest.class));
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    CognitoDevice cachedDevice = testUser.thisDevice();
    cachedDevice.forgetDevice(new GenericHandler() {

        @Override
        public void onSuccess() {
            ArgumentCaptor<ForgetDeviceRequest> argumentCaptor = ArgumentCaptor.forClass(ForgetDeviceRequest.class);
            verify(mockCSIClient).forgetDevice(argumentCaptor.capture());
            ForgetDeviceRequest requestSent = argumentCaptor.getValue();
            Log.d(TAG, "testAccessToken = " + testAccessToken);
            Log.d(TAG, "requestSent.getAccessToken() = " + requestSent.getAccessToken());
            assertEquals(testAccessToken, requestSent.getAccessToken());
            countDownLatch.countDown();
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) ArgumentCaptor(org.mockito.ArgumentCaptor) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) ForgetDeviceRequest(com.amazonaws.services.cognitoidentityprovider.model.ForgetDeviceRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 12 with GenericHandler

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

the class CognitoIdentityProviderDevicesTest method getDeviceDetailsInCurrentThread.

@Test
public void getDeviceDetailsInCurrentThread() {
    CognitoDeviceHelper.cacheDeviceKey(TEST_USER_NAME, TEST_USER_POOL, TEST_DEVICE_KEY, appContext);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Store tokens in shared preferences
    final String testAccessToken = getValidJWT(3600L);
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + ".idToken", getValidJWT(3600L));
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", testAccessToken);
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN);
    // Set mock result for the change password request API call
    doReturn(TEST_VALID_GET_DEVICE_RESPONSE).when(mockCSIClient).getDevice(any(GetDeviceRequest.class));
    CognitoDevice cachedDevice = testUser.thisDevice();
    cachedDevice.getDevice(new GenericHandler() {

        @Override
        public void onSuccess() {
            ArgumentCaptor<GetDeviceRequest> argumentCaptor = ArgumentCaptor.forClass(GetDeviceRequest.class);
            verify(mockCSIClient).getDevice(argumentCaptor.capture());
            GetDeviceRequest requestSent = argumentCaptor.getValue();
            assertEquals(testAccessToken, requestSent.getAccessToken());
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) ArgumentCaptor(org.mockito.ArgumentCaptor) GetDeviceRequest(com.amazonaws.services.cognitoidentityprovider.model.GetDeviceRequest) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) Test(org.junit.Test)

Example 13 with GenericHandler

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

the class CognitoIdentityProviderDevicesTest method forgetDeviceInBackgroundThread.

@Test
public void forgetDeviceInBackgroundThread() {
    CognitoDeviceHelper.cacheDeviceKey(TEST_USER_NAME, TEST_USER_POOL, TEST_DEVICE_KEY, appContext);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Store tokens in shared preferences
    final String testAccessToken = getValidJWT(3600L);
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(3600L));
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", testAccessToken);
    awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN);
    // Set mock result for the change password request API call
    doNothing().when(mockCSIClient).forgetDevice(any(ForgetDeviceRequest.class));
    CognitoDevice cachedDevice = testUser.thisDevice();
    cachedDevice.forgetDeviceInBackground(new GenericHandler() {

        @Override
        public void onSuccess() {
            ArgumentCaptor<ForgetDeviceRequest> argumentCaptor = ArgumentCaptor.forClass(ForgetDeviceRequest.class);
            verify(mockCSIClient).forgetDevice(argumentCaptor.capture());
            ForgetDeviceRequest requestSent = argumentCaptor.getValue();
            assertEquals(testAccessToken, requestSent.getAccessToken());
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) ArgumentCaptor(org.mockito.ArgumentCaptor) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) ForgetDeviceRequest(com.amazonaws.services.cognitoidentityprovider.model.ForgetDeviceRequest) Test(org.junit.Test)

Example 14 with GenericHandler

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

the class CognitoDevice method doNotRememberThisDeviceInBackground.

/**
 * Marks this device as <b>not</b> trusted, runs in a background thread.
 *
 * @param callback              REQUIRED: {@link GenericHandler} callback.
 */
public void doNotRememberThisDeviceInBackground(final GenericHandler callback) {
    if (callback == null) {
        throw new CognitoParameterInvalidException("callback is null");
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            final Handler handler = new Handler(context.getMainLooper());
            Runnable returnCallback;
            try {
                updateDeviceStatusInternal(user.getCachedSession(), DEVICE_TYPE_NOT_REMEMBERED);
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onSuccess();
                    }
                };
            } catch (final Exception e) {
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onFailure(e);
                    }
                };
            }
            handler.post(returnCallback);
        }
    }).start();
}
Also used : CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) Handler(android.os.Handler) GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) CognitoInternalErrorException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException) CognitoNotAuthorizedException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException) CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException)

Example 15 with GenericHandler

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

the class CognitoUser method verifyAttributeInBackground.

/**
 * Verify an attribute with the verification code, in background.
 * <p>
 * Call this method to verify an attribute with the "verification code". To
 * request for a "verification code" call the method
 * {@link CognitoUser#getAttributeVerificationCodeInBackground(String, VerificationHandler)}
 * .
 * </p>
 *
 * @param attributeName REQUIRED: The attribute that is being verified.
 * @param verificationCode REQUIRED: The code for verification.
 * @param callback REQUIRED: Callback
 */
public void verifyAttributeInBackground(final String attributeName, final String verificationCode, final GenericHandler callback) {
    if (callback == null) {
        throw new CognitoParameterInvalidException("callback is null");
    }
    final CognitoUser user = this;
    new Thread(new Runnable() {

        @Override
        public void run() {
            final Handler handler = new Handler(context.getMainLooper());
            Runnable returnCallback;
            try {
                final CognitoUserSession session = user.getCachedSession();
                verifyAttributeInternal(attributeName, verificationCode, session);
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onSuccess();
                    }
                };
            } catch (final Exception e) {
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onFailure(e);
                    }
                };
            }
            handler.post(returnCallback);
        }
    }).start();
}
Also used : CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) Handler(android.os.Handler) GetDetailsHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler) AuthenticationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler) GenericHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler) VerificationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.VerificationHandler) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) ForgotPasswordHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.ForgotPasswordHandler) RegisterMfaHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.RegisterMfaHandler) DevicesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.DevicesHandler) 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)

Aggregations

GenericHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler)25 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)15 Test (org.junit.Test)13 Handler (android.os.Handler)12 CognitoInternalErrorException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException)12 CognitoNotAuthorizedException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)12 CognitoParameterInvalidException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException)12 ArgumentCaptor (org.mockito.ArgumentCaptor)9 AuthenticationHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler)8 DevicesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.DevicesHandler)8 ForgotPasswordHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.ForgotPasswordHandler)8 GetDetailsHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler)8 RegisterMfaHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.RegisterMfaHandler)8 UpdateAttributesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler)8 VerificationHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.VerificationHandler)8 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)8 ResourceNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException)8 UserNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 FlowTracker (com.amazonaws.mobileconnectors.cognitoidentityprovider.utils.FlowTracker)7