use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.
the class OAuth2Utils method _updateUserAttributes.
private Runnable _updateUserAttributes(final Map<String, String> userAttributes, final Map<String, String> clientMetadata, final Callback<List<UserCodeDeliveryDetails>> callback) {
return new Runnable() {
@Override
public void run() {
if (!waitForSignIn()) {
callback.onError(new Exception("Operation requires a signed-in state"));
return;
}
final CognitoUserAttributes cognitoUserAttributes = new CognitoUserAttributes();
if (userAttributes != null) {
for (final String key : userAttributes.keySet()) {
cognitoUserAttributes.addAttribute(key, userAttributes.get(key));
}
}
userpool.getCurrentUser().updateAttributes(cognitoUserAttributes, clientMetadata, new UpdateAttributesHandler() {
@Override
public void onSuccess(List<CognitoUserCodeDeliveryDetails> attributesVerificationList) {
final List<UserCodeDeliveryDetails> list = new LinkedList<UserCodeDeliveryDetails>();
for (CognitoUserCodeDeliveryDetails details : attributesVerificationList) {
list.add(new UserCodeDeliveryDetails(details.getDestination(), details.getDeliveryMedium(), details.getAttributeName()));
}
callback.onResult(list);
}
@Override
public void onFailure(Exception exception) {
callback.onError(exception);
}
});
}
};
}
use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.
the class CognitoUser method updateAttributesInBackground.
/**
* Updates attributes for a user. Runs in background.
* <p>
* Requires valid accessToken.
* </p>
*
* @param clientMetadata A map of custom key-value pairs that is passed to the lambda function for
* custom workflow.
* @param attributes REQUIRED: All attributes and values that need to be
* updated for this user.
* @param callback REQUIRED: {@link UpdateAttributesHandler} callback.
*/
public void updateAttributesInBackground(final Map<String, String> clientMetadata, final CognitoUserAttributes attributes, final UpdateAttributesHandler 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 UpdateUserAttributesResult updateUserAttributesResult = updateAttributesInternal(clientMetadata, attributes, session);
final List<CognitoUserCodeDeliveryDetails> attributesVerificationList = new ArrayList<CognitoUserCodeDeliveryDetails>();
if (updateUserAttributesResult.getCodeDeliveryDetailsList() != null) {
for (final CodeDeliveryDetailsType details : updateUserAttributesResult.getCodeDeliveryDetailsList()) {
attributesVerificationList.add(new CognitoUserCodeDeliveryDetails(details));
}
}
returnCallback = new Runnable() {
@Override
public void run() {
callback.onSuccess(attributesVerificationList);
}
};
} catch (final Exception e) {
returnCallback = new Runnable() {
@Override
public void run() {
callback.onFailure(e);
}
};
}
handler.post(returnCallback);
}
}).start();
}
use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.
the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInCurrentThreadWithNoCachedTokens.
@Test
public void updateUserAttributeInCurrentThreadWithNoCachedTokens() {
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_UPDATE_USER_RESULT).when(mockCSIClient).updateUserAttributes(any(UpdateUserAttributesRequest.class));
CognitoUserAttributes attributes = new CognitoUserAttributes();
attributes.addAttribute("TestAttribute1", "TestData1");
attributes.addAttribute("TestAttribute2", "TestData2");
testUser.updateAttributes(attributes, new UpdateAttributesHandler() {
@Override
public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
assertNotNull(var1);
}
@Override
public void onFailure(Exception exception) {
assertNotNull(exception);
}
});
}
use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.
the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInCurrentThreadWithCachedTokensServiceException.
// Get user attributes -
@Test
public void updateUserAttributeInCurrentThreadWithCachedTokensServiceException() {
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).updateUserAttributes(any(UpdateUserAttributesRequest.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();
CognitoUserAttributes attributes = new CognitoUserAttributes();
attributes.addAttribute("TestAttribute1", "TestData1");
attributes.addAttribute("TestAttribute2", "TestData2");
testUser.updateAttributes(attributes, new UpdateAttributesHandler() {
@Override
public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
assertNotNull(var1);
}
@Override
public void onFailure(Exception exception) {
assertNotNull(exception);
// System.out.println("Exception: "+exception.toString());
}
});
}
use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler in project aws-sdk-android by aws-amplify.
the class CognitoIdentityProviderUpdateDetailsTest method updateUserAttributeInBackgroundWithCachedTokensServiceException.
// Get user attributes - in background
@Test
public void updateUserAttributeInBackgroundWithCachedTokensServiceException() {
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("failed");
doThrow(exception).when(mockCSIClient).updateUserAttributes(any(UpdateUserAttributesRequest.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();
CognitoUserAttributes attributes = new CognitoUserAttributes();
attributes.addAttribute("TestAttribute1", "TestData1");
attributes.addAttribute("TestAttribute2", "TestData2");
testUser.updateAttributesInBackground(attributes, new UpdateAttributesHandler() {
@Override
public void onSuccess(List<CognitoUserCodeDeliveryDetails> var1) {
assertNotNull(var1);
}
@Override
public void onFailure(Exception exception) {
assertNotNull(exception);
}
});
}
Aggregations