Search in sources :

Example 1 with GetDetailsHandler

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

the class CognitoUser method getDetailsInBackground.

/**
 * Retrieves the current user attributes. Runs in background.
 * <p>
 * All attributes, which are set for this user, are fetched. This method
 * requires valid accessToken.
 * </p>
 *
 * @param callback REQUIRED: {@link GetDetailsHandler} callback
 */
public void getDetailsInBackground(final GetDetailsHandler 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();
                final CognitoUserDetails userDetails = getUserDetailsInternal(session);
                returnCallback = new Runnable() {

                    @Override
                    public void run() {
                        callback.onSuccess(userDetails);
                    }
                };
            } 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)

Example 2 with GetDetailsHandler

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

the class CognitoIdentityProviderGetUserTest method getUserAttributeInBackgroundWithCachedTokens.

// Get user attributes - in background
@Test
public void getUserAttributeInBackgroundWithCachedTokens() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    doReturn(TEST_VALID_GET_USER_RESULT).when(mockCSIClient).getUser(any(GetUserRequest.class));
    // Store tokens in shared preferences
    SharedPreferences sharedPreferences = appContext.getSharedPreferences("CognitoIdentityProviderCache", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN).commit();
    testUser.getDetailsInBackground(new GetDetailsHandler() {

        @Override
        public void onSuccess(CognitoUserDetails details) {
            // Extract the arguments passed to get user API call
            ArgumentCaptor<GetUserRequest> argumentCaptor = ArgumentCaptor.forClass(GetUserRequest.class);
            verify(mockCSIClient).getUser(argumentCaptor.capture());
            GetUserRequest requestSent = argumentCaptor.getValue();
            // Verify the arguments passed to the API call
            assertNotNull(requestSent);
            assertNotNull(requestSent.getAccessToken());
            // Verify result
            assertNotNull(details);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) GetUserRequest(com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest) ArgumentCaptor(org.mockito.ArgumentCaptor) SharedPreferences(android.content.SharedPreferences) CognitoUserDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserDetails) GetDetailsHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 3 with GetDetailsHandler

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

the class CognitoIdentityProviderGetUserTest method getUserAttributeInCurrentThreadWithNoCachedTokens.

@Test
public void getUserAttributeInCurrentThreadWithNoCachedTokens() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    doReturn(TEST_VALID_GET_USER_RESULT).when(mockCSIClient).getUser(any(GetUserRequest.class));
    testUser.getDetails(new GetDetailsHandler() {

        @Override
        public void onSuccess(CognitoUserDetails details) {
            // Extract the arguments passed to get user API call
            assertTrue(false);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) GetUserRequest(com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest) CognitoUserDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserDetails) GetDetailsHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 4 with GetDetailsHandler

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

the class CognitoIdentityProviderGetUserTest method getUserAttributeInBackgroundWithCachedTokensServiceException.

// Get user attributes - in background
@Test
public void getUserAttributeInBackgroundWithCachedTokensServiceException() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    InvalidParameterException exception = new InvalidParameterException("registration failed");
    doThrow(exception).when(mockCSIClient).getUser(any(GetUserRequest.class));
    // Store tokens in shared preferences
    SharedPreferences sharedPreferences = appContext.getSharedPreferences("CognitoIdentityProviderCache", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", getValidJWT(3600L)).commit();
    sharedPreferences.edit().putString("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN).commit();
    testUser.getDetailsInBackground(new GetDetailsHandler() {

        @Override
        public void onSuccess(CognitoUserDetails details) {
            // Extract the arguments passed to get user API call
            ArgumentCaptor<GetUserRequest> argumentCaptor = ArgumentCaptor.forClass(GetUserRequest.class);
            verify(mockCSIClient).getUser(argumentCaptor.capture());
            GetUserRequest requestSent = argumentCaptor.getValue();
            // Verify the arguments passed to the API call
            assertNotNull(requestSent);
            assertNotNull(requestSent.getAccessToken());
            // Verify result
            assertNotNull(details);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) GetUserRequest(com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest) ArgumentCaptor(org.mockito.ArgumentCaptor) SharedPreferences(android.content.SharedPreferences) CognitoUserDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserDetails) GetDetailsHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Example 5 with GetDetailsHandler

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

the class CognitoIdentityProviderGetUserTest method getUserAttributeInBackgroundWithNoCachedTokens.

@Test
public void getUserAttributeInBackgroundWithNoCachedTokens() {
    testPool = new CognitoUserPool(appContext, TEST_USER_POOL, TEST_CLIENT_ID, TEST_CLIENT_SECRET, mockCSIClient);
    testUser = testPool.getUser(TEST_USER_NAME);
    // Set mock result for the change password request API call
    doReturn(TEST_VALID_GET_USER_RESULT).when(mockCSIClient).getUser(any(GetUserRequest.class));
    testUser.getDetailsInBackground(new GetDetailsHandler() {

        @Override
        public void onSuccess(CognitoUserDetails details) {
            // Extract the arguments passed to get user API call
            assertTrue(false);
        }

        @Override
        public void onFailure(Exception exception) {
            assertNotNull(exception);
        }
    });
}
Also used : CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) GetUserRequest(com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest) CognitoUserDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserDetails) GetDetailsHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) Test(org.junit.Test)

Aggregations

GetDetailsHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler)8 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)8 CognitoUserDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserDetails)7 Test (org.junit.Test)7 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)6 GetUserRequest (com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest)6 SharedPreferences (android.content.SharedPreferences)4 ArgumentCaptor (org.mockito.ArgumentCaptor)4 AuthenticationHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler)2 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)2 ResourceNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException)2 Handler (android.os.Handler)1 CognitoDevice (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice)1 CognitoUser (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser)1 CognitoUserSession (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession)1 AuthenticationContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation)1 ChallengeContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation)1 MultiFactorAuthenticationContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation)1 CognitoInternalErrorException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException)1 CognitoNotAuthorizedException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)1