use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.
the class AWSCognitoAuthPlugin method updateUserAttributes.
@Override
public void updateUserAttributes(@NonNull List<AuthUserAttribute> attributes, @NonNull AuthUpdateUserAttributesOptions options, @NonNull Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>> onSuccess, @NonNull Consumer<AuthException> onError) {
final Map<String, String> clientMetadata = new HashMap<>();
if (options instanceof AWSCognitoAuthUpdateUserAttributesOptions) {
AWSCognitoAuthUpdateUserAttributesOptions cognitoOptions = (AWSCognitoAuthUpdateUserAttributesOptions) options;
clientMetadata.putAll(cognitoOptions.getMetadata());
}
Map<String, String> attributesMap = new HashMap<>();
for (AuthUserAttribute attribute : attributes) {
attributesMap.put(attribute.getKey().getKeyString(), attribute.getValue());
}
awsMobileClient.updateUserAttributes(attributesMap, clientMetadata, new Callback<List<UserCodeDeliveryDetails>>() {
@Override
public void onResult(List<UserCodeDeliveryDetails> result) {
Map<String, UserCodeDeliveryDetails> codeDetailsMap = new HashMap<>();
Map<AuthUserAttributeKey, AuthUpdateAttributeResult> resultMap = new HashMap<>();
for (UserCodeDeliveryDetails details : result) {
codeDetailsMap.put(details.getAttributeName(), details);
}
for (String attributeKey : attributesMap.keySet()) {
if (codeDetailsMap.containsKey(attributeKey)) {
resultMap.put(AuthUserAttributeKey.custom(attributeKey), new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, Collections.emptyMap(), convertCodeDeliveryDetails(codeDetailsMap.get(attributeKey)))));
} else {
resultMap.put(AuthUserAttributeKey.custom(attributeKey), new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.DONE, Collections.emptyMap(), null)));
}
}
onSuccess.accept(resultMap);
}
@Override
public void onError(Exception error) {
onError.accept(new AuthException("Failed to update user attributes", error, "See attached exception for more details"));
}
});
}
use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.
the class AWSCognitoAuthPlugin method updateUserAttribute.
@Override
public void updateUserAttribute(@NonNull AuthUserAttribute attribute, @NonNull AuthUpdateUserAttributeOptions options, @NonNull Consumer<AuthUpdateAttributeResult> onSuccess, @NonNull Consumer<AuthException> onError) {
final Map<String, String> clientMetadata = new HashMap<>();
if (options instanceof AWSCognitoAuthUpdateUserAttributeOptions) {
AWSCognitoAuthUpdateUserAttributeOptions cognitoOptions = (AWSCognitoAuthUpdateUserAttributeOptions) options;
clientMetadata.putAll(cognitoOptions.getMetadata());
}
awsMobileClient.updateUserAttributes(Collections.singletonMap(attribute.getKey().getKeyString(), attribute.getValue()), clientMetadata, new Callback<List<UserCodeDeliveryDetails>>() {
@Override
public void onResult(List<UserCodeDeliveryDetails> result) {
if (result.size() == 0) {
onSuccess.accept(new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.DONE, Collections.emptyMap(), null)));
} else {
onSuccess.accept(new AuthUpdateAttributeResult(true, new AuthNextUpdateAttributeStep(AuthUpdateAttributeStep.CONFIRM_ATTRIBUTE_WITH_CODE, Collections.emptyMap(), convertCodeDeliveryDetails(result.get(0)))));
}
}
@Override
public void onError(Exception error) {
onError.accept(new AuthException("Failed to update user attributes", error, "See attached exception for more details"));
}
});
}
use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project aws-sdk-android by aws-amplify.
the class OAuth2Utils method _signIn.
private Runnable _signIn(final String username, final String password, final Map<String, String> validationData, final Map<String, String> clientMetadata, final Callback<SignInResult> callback) {
this.signInCallback = callback;
signInState = null;
mStore.set(SIGN_IN_MODE, SignInMode.SIGN_IN.toString());
return new Runnable() {
@Override
public void run() {
try {
userpool.getUser(username).getSession(clientMetadata, new AuthenticationHandler() {
@Override
public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
try {
mCognitoUserSession = userSession;
signInState = SignInState.DONE;
} catch (Exception e) {
signInCallback.onError(e);
signInCallback = null;
}
try {
if (isFederationEnabled()) {
federatedSignInWithoutAssigningState(userpoolsLoginKey, mCognitoUserSession.getIdToken().getJWTToken());
}
releaseSignInWait();
} catch (Exception e) {
Log.w(TAG, "Failed to federate tokens during sign-in", e);
} finally {
setUserState(new UserStateDetails(UserState.SIGNED_IN, getSignInDetailsMap()));
}
signInCallback.onResult(SignInResult.DONE);
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
Log.d(TAG, "Sending password.");
final HashMap<String, String> authParameters = new HashMap<>();
// Check if the auth flow type setting is in the configuration.
boolean authFlowTypeInConfig = awsConfiguration.optJsonObject(AUTH_KEY) != null && awsConfiguration.optJsonObject(AUTH_KEY).has("authenticationFlowType");
try {
String authFlowType = authFlowTypeInConfig ? awsConfiguration.optJsonObject(AUTH_KEY).getString("authenticationFlowType") : null;
if (authFlowTypeInConfig && AUTH_TYPE_INIT_CUSTOM_AUTH.equals(authFlowType)) {
// use one of the below constructors depending on what's passed in.
if (password != null) {
authenticationContinuation.setAuthenticationDetails(new AuthenticationDetails(username, password, authParameters, validationData));
} else {
authenticationContinuation.setAuthenticationDetails(new AuthenticationDetails(username, authParameters, validationData));
}
} else if (authFlowTypeInConfig && AUTH_TYPE_INIT_USER_PASSWORD.equals(authFlowType)) {
// If there's a value in the config and it's USER_PASSWORD_AUTH, set the auth type (challenge name)
// to be USER_PASSWORD.
AuthenticationDetails authenticationDetails = new AuthenticationDetails(username, password, validationData);
authenticationDetails.setAuthenticationType(CHLG_TYPE_USER_PASSWORD);
authenticationContinuation.setAuthenticationDetails(authenticationDetails);
} else {
// Otherwise, auth flow is USER_SRP_AUTH and the auth type (challenge name)
// will default to PASSWORD_VERIFIER.
Log.d(TAG, "Using USER_SRP_AUTH for flow type.");
authenticationContinuation.setAuthenticationDetails(new AuthenticationDetails(username, password, validationData));
}
} catch (JSONException exception) {
Log.w(TAG, "Exception while attempting to read authenticationFlowType from config.", exception);
}
authenticationContinuation.continueTask();
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
signInMfaContinuation = continuation;
CognitoUserCodeDeliveryDetails parameters = continuation.getParameters();
signInState = SignInState.SMS_MFA;
signInCallback.onResult(new SignInResult(SignInState.SMS_MFA, new UserCodeDeliveryDetails(parameters.getDestination(), parameters.getDeliveryMedium(), parameters.getAttributeName())));
}
@Override
public void authenticationChallenge(ChallengeContinuation continuation) {
try {
signInState = SignInState.valueOf(continuation.getChallengeName());
signInChallengeContinuation = continuation;
signInCallback.onResult(new SignInResult(signInState, continuation.getParameters()));
} catch (IllegalArgumentException e) {
signInCallback.onError(e);
}
}
@Override
public void onFailure(Exception exception) {
signInCallback.onError(exception);
}
});
} catch (Exception e) {
callback.onError(e);
}
}
};
}
Aggregations