Search in sources :

Example 11 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.

the class AWSCognitoAuthPlugin method convertSignUpResult.

private AuthSignUpResult convertSignUpResult(@NonNull SignUpResult result, @NonNull String username) {
    UserCodeDeliveryDetails details = Objects.requireNonNull(result).getUserCodeDeliveryDetails();
    AuthCodeDeliveryDetails newDetails = details != null ? new AuthCodeDeliveryDetails(details.getDestination(), AuthCodeDeliveryDetails.DeliveryMedium.fromString(details.getDeliveryMedium()), details.getAttributeName()) : null;
    return new AuthSignUpResult(true, new AuthNextSignUpStep(result.getConfirmationState() ? AuthSignUpStep.DONE : AuthSignUpStep.CONFIRM_SIGN_UP_STEP, Collections.emptyMap(), newDetails), result.getUserSub() != null ? new AuthUser(result.getUserSub(), username) : null);
}
Also used : AuthCodeDeliveryDetails(com.amplifyframework.auth.AuthCodeDeliveryDetails) AuthNextSignUpStep(com.amplifyframework.auth.result.step.AuthNextSignUpStep) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthSignUpResult(com.amplifyframework.auth.result.AuthSignUpResult) AuthUser(com.amplifyframework.auth.AuthUser)

Example 12 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project amplify-android by aws-amplify.

the class AWSCognitoAuthPlugin method resendUserAttributeConfirmationCode.

@Override
public void resendUserAttributeConfirmationCode(@NonNull AuthUserAttributeKey attributeKey, @NonNull AuthResendUserAttributeConfirmationCodeOptions options, @NonNull Consumer<AuthCodeDeliveryDetails> onSuccess, @NonNull Consumer<AuthException> onError) {
    final Map<String, String> clientMetadata = new HashMap<>();
    if (options instanceof AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) {
        AWSCognitoAuthResendUserAttributeConfirmationCodeOptions cognitoOptions = (AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) options;
        clientMetadata.putAll(cognitoOptions.getMetadata());
    }
    String attributeName = attributeKey.getKeyString();
    awsMobileClient.verifyUserAttribute(attributeName, clientMetadata, new Callback<UserCodeDeliveryDetails>() {

        @Override
        public void onResult(UserCodeDeliveryDetails result) {
            onSuccess.accept(convertCodeDeliveryDetails(result));
        }

        @Override
        public void onError(Exception error) {
            onError.accept(new AuthException("Failed to resend user attribute confirmation code", error, "See attached exception for more details"));
        }
    });
}
Also used : HashMap(java.util.HashMap) AWSCognitoAuthResendUserAttributeConfirmationCodeOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthException(com.amplifyframework.auth.AuthException) AuthException(com.amplifyframework.auth.AuthException) JSONException(org.json.JSONException) AmplifyException(com.amplifyframework.AmplifyException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) AuthNavigationException(com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)

Example 13 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails 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);
                }
            });
        }
    };
}
Also used : CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) UpdateAttributesHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.UpdateAttributesHandler) CognitoUserAttributes(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserAttributes) ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) JSONException(org.json.JSONException) InvalidUserPoolConfigurationException(com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException) AmazonClientException(com.amazonaws.AmazonClientException) NotAuthorizedException(com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)

Example 14 with UserCodeDeliveryDetails

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 AuthFlowType authFlowType, 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 resolvedAuthFlowType = authFlowType != null ? authFlowType.name() : null;
                            if (resolvedAuthFlowType == null && authFlowTypeInConfig) {
                                resolvedAuthFlowType = awsConfiguration.optJsonObject(AUTH_KEY).getString("authenticationFlowType");
                            }
                            if (resolvedAuthFlowType != null && AUTH_TYPE_INIT_CUSTOM_AUTH.equals(resolvedAuthFlowType)) {
                                // 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 (resolvedAuthFlowType != null && AUTH_TYPE_INIT_USER_PASSWORD.equals(resolvedAuthFlowType)) {
                                // 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);
            }
        }
    };
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) JSONException(org.json.JSONException) JSONException(org.json.JSONException) InvalidUserPoolConfigurationException(com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException) AmazonClientException(com.amazonaws.AmazonClientException) NotAuthorizedException(com.amazonaws.services.cognitoidentity.model.NotAuthorizedException) SignInResult(com.amazonaws.mobile.client.results.SignInResult) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) AuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation) ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) ChallengeContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation) AuthenticationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler) CognitoUserSession(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession) AuthenticationDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails)

Example 15 with UserCodeDeliveryDetails

use of com.amazonaws.mobile.client.results.UserCodeDeliveryDetails in project aws-sdk-android by aws-amplify.

the class OAuth2Utils method updateUserAttributes.

/**
 * Sends a map of user attributes to update. If an attribute needs to
 * be verified, then the verification delivery information is returned.
 * @param userAttributes the attributes i.e. email
 * @param clientMetadata A map of custom key-value pairs that is passed to the lambda function for
 *                       custom workflow.
 * @param callback verification delivery information
 */
@AnyThread
public void updateUserAttributes(final Map<String, String> userAttributes, final Map<String, String> clientMetadata, final Callback<List<UserCodeDeliveryDetails>> callback) {
    InternalCallback internalCallback = new InternalCallback<List<UserCodeDeliveryDetails>>(callback);
    internalCallback.async(_updateUserAttributes(userAttributes, clientMetadata, internalCallback));
}
Also used : InternalCallback(com.amazonaws.mobile.client.internal.InternalCallback) CognitoUserCodeDeliveryDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AnyThread(androidx.annotation.AnyThread)

Aggregations

UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)28 Test (org.junit.Test)18 HashMap (java.util.HashMap)14 RandomString (com.amplifyframework.testutils.random.RandomString)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11 JSONException (org.json.JSONException)8 SignInResult (com.amazonaws.mobile.client.results.SignInResult)7 SignUpResult (com.amazonaws.mobile.client.results.SignUpResult)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 CognitoUserCodeDeliveryDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails)6 AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)6 AuthSignUpResult (com.amplifyframework.auth.result.AuthSignUpResult)6 AuthUpdateAttributeResult (com.amplifyframework.auth.result.AuthUpdateAttributeResult)6 AmazonClientException (com.amazonaws.AmazonClientException)5 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)5 NotAuthorizedException (com.amazonaws.services.cognitoidentity.model.NotAuthorizedException)5 InvalidUserPoolConfigurationException (com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException)5 Tokens (com.amazonaws.mobile.client.results.Tokens)4 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)4