Search in sources :

Example 1 with GetUserRequest

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

the class CognitoUser method getUserDetailsInternal.

/**
 * Internal method to fetch user attributes.
 *
 * @param session REQUIRED: {@link CognitoUserSession}
 * @return User attributes
 */
private CognitoUserDetails getUserDetailsInternal(CognitoUserSession session) {
    if (session != null && session.isValid()) {
        final GetUserRequest getUserRequest = new GetUserRequest();
        getUserRequest.setAccessToken(session.getAccessToken().getJWTToken());
        final GetUserResult userResult = cognitoIdentityProviderClient.getUser(getUserRequest);
        return new CognitoUserDetails(new CognitoUserAttributes(userResult.getUserAttributes()), new CognitoUserSettings(userResult.getMFAOptions()));
    } else {
        throw new CognitoNotAuthorizedException("user is not authenticated");
    }
}
Also used : GetUserResult(com.amazonaws.services.cognitoidentityprovider.model.GetUserResult) GetUserRequest(com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest) CognitoNotAuthorizedException(com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)

Example 2 with GetUserRequest

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

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

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

the class CognitoIdentityProviderGetUserTest method getUserAttributeInCurrentThreadWithCachedTokens.

// Get user attributes -
@Test
public void getUserAttributeInCurrentThreadWithCachedTokens() {
    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.getDetails(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 5 with GetUserRequest

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

the class CognitoIdentityProviderGetUserTest method getUserAttributeInCurrentThreadWithCachedTokensServiceException.

// Get user attributes -
@Test
public void getUserAttributeInCurrentThreadWithCachedTokensServiceException() {
    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.getDetails(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)

Aggregations

GetUserRequest (com.amazonaws.services.cognitoidentityprovider.model.GetUserRequest)7 SharedPreferences (android.content.SharedPreferences)6 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)6 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)6 Test (org.junit.Test)6 ArgumentCaptor (org.mockito.ArgumentCaptor)6 CognitoUserDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserDetails)4 GetDetailsHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GetDetailsHandler)4 CognitoUserAttributes (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes)2 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)2 UpdateAttributesHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler)2 UpdateUserAttributesRequest (com.amazonaws.services.cognitoidentityprovider.model.UpdateUserAttributesRequest)2 CognitoNotAuthorizedException (com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException)1 GetUserResult (com.amazonaws.services.cognitoidentityprovider.model.GetUserResult)1