Search in sources :

Example 1 with ForgetDeviceRequest

use of com.amazonaws.services.cognitoidentityprovider.model.ForgetDeviceRequest 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 2 with ForgetDeviceRequest

use of com.amazonaws.services.cognitoidentityprovider.model.ForgetDeviceRequest 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 3 with ForgetDeviceRequest

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

the class CognitoDevice method forgetDeviceInternal.

/**
 * Internal method to forget this device.
 *
 * @param session              REQUIRED: A valid {@link CognitoUserSession}.
 */
private void forgetDeviceInternal(CognitoUserSession session) {
    if (session != null && session.isValid()) {
        if (this.deviceKey != null) {
            final ForgetDeviceRequest forgetDeviceRequest = new ForgetDeviceRequest();
            forgetDeviceRequest.setAccessToken(session.getAccessToken().getJWTToken());
            forgetDeviceRequest.setDeviceKey(this.deviceKey);
            user.getCognitoIdentityProviderClient().forgetDevice(forgetDeviceRequest);
        } else {
            throw new CognitoParameterInvalidException("Device key is null");
        }
    } else {
        throw new CognitoNotAuthorizedException("User is not authorized");
    }
}
Also used : CognitoParameterInvalidException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException) CognitoNotAuthorizedException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException) ForgetDeviceRequest(com.amazonaws.services.cognitoidentityprovider.model.ForgetDeviceRequest)

Example 4 with ForgetDeviceRequest

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

the class DeviceOperations method _forgetDevice.

private ReturningRunnable<Void> _forgetDevice(final String deviceKey) {
    return new ReturningRunnable<Void>() {

        @Override
        public Void run() throws Exception {
            CognitoDevice cognitoDevice = getCognitoDevice(deviceKey);
            final ForgetDeviceRequest forgetDeviceRequest = new ForgetDeviceRequest().withAccessToken(mobileClient.getTokens().getAccessToken().getTokenString()).withDeviceKey(cognitoDevice.getDeviceKey());
            userpoolLL.forgetDevice(forgetDeviceRequest);
            return null;
        }
    };
}
Also used : ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) ForgetDeviceRequest(com.amazonaws.services.cognitoidentityprovider.model.ForgetDeviceRequest)

Aggregations

ForgetDeviceRequest (com.amazonaws.services.cognitoidentityprovider.model.ForgetDeviceRequest)4 CognitoDevice (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice)3 GenericHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler)2 Test (org.junit.Test)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)1 CognitoNotAuthorizedException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)1 CognitoParameterInvalidException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoParameterInvalidException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1