Search in sources :

Example 1 with AWSCognitoAuthConfirmSignInOptions

use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions in project amplify-android by aws-amplify.

the class AuthComponentTest method confirmSignInWithOptions.

/**
 * Tests that the confirmSignIn method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.confirmSignIn with
 * the confirmation code and options it received and the success callback receives a valid AuthSignInResult.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
// Casts final parameter to Callback to differentiate methods
@SuppressWarnings("unchecked")
public void confirmSignInWithOptions() throws AuthException {
    SignInResult amcResult = new SignInResult(SignInState.DONE, new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    Tokens tokensResult = new Tokens(ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN);
    AWSCognitoAuthConfirmSignInOptions.CognitoBuilder options = AWSCognitoAuthConfirmSignInOptions.builder();
    Map<String, String> metadata = new HashMap<String, String>();
    AuthUserAttribute attribute = new AuthUserAttribute(AuthUserAttributeKey.email(), "test@test.test");
    List<AuthUserAttribute> attributes = Collections.singletonList(attribute);
    Map<String, String> attributeMap = Collections.singletonMap("email", "test@test.test");
    metadata.put("key", "value");
    options.metadata(metadata);
    options.userAttributes(attributes);
    AWSCognitoAuthConfirmSignInOptions builtOptions = options.build();
    doAnswer(invocation -> {
        Callback<Tokens> callback = invocation.getArgument(0);
        callback.onResult(tokensResult);
        return null;
    }).when(mobileClient).getTokens(any());
    doAnswer(invocation -> {
        Callback<SignInResult> callback = invocation.getArgument(3);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).confirmSignIn(any(String.class), any(), any(), (Callback<SignInResult>) any());
    AuthSignInResult result = synchronousAuth.confirmSignIn(CONFIRMATION_CODE, builtOptions);
    validateSignInResult(result, true, AuthSignInStep.DONE);
    verify(mobileClient).confirmSignIn(eq(CONFIRMATION_CODE), eq(metadata), eq(attributeMap), (Callback<SignInResult>) any());
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) HashMap(java.util.HashMap) AWSCognitoAuthConfirmSignInOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions) UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SignInResult(com.amazonaws.mobile.client.results.SignInResult) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) Tokens(com.amazonaws.mobile.client.results.Tokens) Test(org.junit.Test)

Example 2 with AWSCognitoAuthConfirmSignInOptions

use of com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions in project amplify-android by aws-amplify.

the class AWSCognitoAuthPlugin method confirmSignIn.

@Override
public void confirmSignIn(@NonNull String confirmationCode, @NonNull AuthConfirmSignInOptions options, @NonNull Consumer<AuthSignInResult> onSuccess, @NonNull Consumer<AuthException> onException) {
    final Map<String, String> metadata = new HashMap<>();
    final Map<String, String> userAttributes = new HashMap<>();
    if (options instanceof AWSCognitoAuthConfirmSignInOptions) {
        metadata.putAll(((AWSCognitoAuthConfirmSignInOptions) options).getMetadata());
        for (AuthUserAttribute attribute : ((AWSCognitoAuthConfirmSignInOptions) options).getUserAttributes()) {
            userAttributes.put(attribute.getKey().getKeyString(), attribute.getValue());
        }
    }
    awsMobileClient.confirmSignIn(confirmationCode, metadata, userAttributes, new Callback<SignInResult>() {

        @Override
        public void onResult(SignInResult result) {
            try {
                AuthSignInResult newResult = convertSignInResult(result);
                fetchAndSetUserId(() -> onSuccess.accept(newResult));
            } catch (AuthException exception) {
                onException.accept(exception);
            }
        }

        @Override
        public void onError(Exception error) {
            onException.accept(CognitoAuthExceptionConverter.lookup(error, "Confirm sign in failed"));
        }
    });
}
Also used : AuthUserAttribute(com.amplifyframework.auth.AuthUserAttribute) HashMap(java.util.HashMap) AWSCognitoAuthConfirmSignInOptions(com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions) AuthException(com.amplifyframework.auth.AuthException) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) 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) SignInResult(com.amazonaws.mobile.client.results.SignInResult) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult)

Aggregations

SignInResult (com.amazonaws.mobile.client.results.SignInResult)2 AuthUserAttribute (com.amplifyframework.auth.AuthUserAttribute)2 AWSCognitoAuthConfirmSignInOptions (com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions)2 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)2 HashMap (java.util.HashMap)2 Tokens (com.amazonaws.mobile.client.results.Tokens)1 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)1 AuthNavigationException (com.amazonaws.mobileconnectors.cognitoauth.exceptions.AuthNavigationException)1 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)1 AmplifyException (com.amplifyframework.AmplifyException)1 AuthException (com.amplifyframework.auth.AuthException)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1