Search in sources :

Example 21 with GenericHandler

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

the class CognitoIdentityProviderDevicesTest method trustDeviceInCurrentThread.

@Test
public void trustDeviceInCurrentThread() {
    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_UPDATE_DEVICE_RESPONSE).when(mockCSIClient).updateDeviceStatus(any(UpdateDeviceStatusRequest.class));
    CognitoDevice cachedDevice = testUser.thisDevice();
    cachedDevice.rememberThisDevice(new GenericHandler() {

        @Override
        public void onSuccess() {
            ArgumentCaptor<UpdateDeviceStatusRequest> argumentCaptor = ArgumentCaptor.forClass(UpdateDeviceStatusRequest.class);
            verify(mockCSIClient).updateDeviceStatus(argumentCaptor.capture());
            UpdateDeviceStatusRequest 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) UpdateDeviceStatusRequest(com.amazonaws.services.cognitoidentityprovider.model.UpdateDeviceStatusRequest) Test(org.junit.Test)

Example 22 with GenericHandler

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

the class CognitoIdentityProviderDevicesTest method getDeviceDetailsInBackgroundThread.

@Test
public void getDeviceDetailsInBackgroundThread() {
    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.getDeviceInBackground(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 23 with GenericHandler

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

the class CognitoDevice method forgetDeviceInBackground.

/**
 * Stops tracking this device, runs in a background thread.
 *
 * @param callback              REQUIRED: {@link GenericHandler} callback.
 */
public void forgetDeviceInBackground(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 {
                forgetDeviceInternal(user.getCachedSession());
                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 24 with GenericHandler

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

the class CognitoDevice method getDeviceInBackground.

/**
 * Fetches device properties. Call this method to ensure the device properties are current.
 * Reading device properties from this object can return null values.
 *
 * @param callback              REQUIRED: {@link GenericHandler} callback.
 */
public void getDeviceInBackground(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 {
                final GetDeviceResult getDeviceResult = getDeviceInternal(user.getCachedSession());
                updateThis(getDeviceResult.getDevice());
                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) GetDeviceResult(com.amazonaws.services.cognitoidentityprovider.model.GetDeviceResult) 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 25 with GenericHandler

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

the class CognitoDevice method rememberThisDeviceInBackground.

/**
 * Marks this device as trusted, runs in a background.
 *
 * @param callback              REQUIRED: {@link GenericHandler} callback.
 */
public void rememberThisDeviceInBackground(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_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)

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