Search in sources :

Example 1 with SignInResult

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

the class OAuth2Utils method _confirmSignIn.

private Runnable _confirmSignIn(final String signInChallengeResponse, final Map<String, String> clientMetadata, final Map<String, String> userAttributes, final Callback<SignInResult> callback) {
    return new Runnable() {

        @Override
        public void run() {
            if (signInState == null) {
                callback.onError(new IllegalStateException("Cannot call confirmSignIn(String, Callback) " + "without initiating sign-in. This call is used for SMS_MFA and NEW_PASSWORD_REQUIRED " + "sign-in state."));
                return;
            }
            final CognitoIdentityProviderContinuation detectedContinuation;
            switch(signInState) {
                case SMS_MFA:
                    signInMfaContinuation.setMfaCode(signInChallengeResponse);
                    signInMfaContinuation.setClientMetaData(clientMetadata);
                    detectedContinuation = signInMfaContinuation;
                    signInCallback = new InternalCallback<SignInResult>(callback);
                    break;
                case NEW_PASSWORD_REQUIRED:
                    ((NewPasswordContinuation) signInChallengeContinuation).setPassword(signInChallengeResponse);
                    signInChallengeContinuation.setClientMetaData(clientMetadata);
                    for (final String key : userAttributes.keySet()) {
                        signInChallengeContinuation.setChallengeResponse(CHALLENGE_RESPONSE_USER_ATTRIBUTES_PREFIX_KEY + key, userAttributes.get(key));
                    }
                    detectedContinuation = signInChallengeContinuation;
                    signInCallback = new InternalCallback<SignInResult>(callback);
                    break;
                case CUSTOM_CHALLENGE:
                    signInChallengeContinuation.setChallengeResponse("ANSWER", signInChallengeResponse);
                    detectedContinuation = signInChallengeContinuation;
                    signInCallback = new InternalCallback<SignInResult>(callback);
                    if (clientMetadata != null) {
                        signInChallengeContinuation.setClientMetaData(clientMetadata);
                    }
                    break;
                case DONE:
                    callback.onError(new IllegalStateException("confirmSignIn called after signIn has succeeded"));
                    return;
                default:
                    callback.onError(new IllegalStateException("confirmSignIn called on unsupported operation, " + "please file a feature request"));
                    return;
            }
            if (detectedContinuation != null) {
                detectedContinuation.continueTask();
            }
        }
    };
}
Also used : CognitoIdentityProviderContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.CognitoIdentityProviderContinuation) ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) NewPasswordContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.NewPasswordContinuation) SignInResult(com.amazonaws.mobile.client.results.SignInResult)

Example 2 with SignInResult

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

the class AWSMobileClientPersistenceTest method testIsSignedIn.

@Test
public void testIsSignedIn() {
    signInAndVerifySignIn();
    assertTrue(auth.isSignedIn());
    deleteAllEncryptionKeys();
    assertFalse(auth.isSignedIn());
    try {
        final SignInResult signInResult = auth.signIn(username, PASSWORD, null);
        assertNotNull(signInResult);
        assertEquals(SignInState.DONE, signInResult.getSignInState());
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
    assertTrue(auth.isSignedIn());
}
Also used : UserNotConfirmedException(com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException) SignInResult(com.amazonaws.mobile.client.results.SignInResult) Test(org.junit.Test)

Example 3 with SignInResult

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

the class AWSMobileClientPersistenceWithRestartabilityTest method testGetUserAttributes.

@Test
public void testGetUserAttributes() throws Exception {
    signInAndVerifySignIn();
    Map<String, String> userAttributes = auth.getUserAttributes();
    assertNotNull(userAttributes);
    assertEquals(getPackageConfigure().getString("email"), userAttributes.get("email"));
    deleteAllEncryptionKeys();
    initializeAWSMobileClient(appContext, UserState.SIGNED_OUT);
    try {
        auth.getUserAttributes();
    } catch (Exception ex) {
        assertEquals("Operation requires a signed-in state", ex.getMessage());
    }
    try {
        final SignInResult signInResult = auth.signIn(username, PASSWORD, null);
        assertNotNull(signInResult);
        assertEquals(SignInState.DONE, signInResult.getSignInState());
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
    userAttributes = auth.getUserAttributes();
    assertNotNull(userAttributes);
    assertEquals(getPackageConfigure().getString("email"), userAttributes.get("email"));
}
Also used : JSONException(org.json.JSONException) UserNotConfirmedException(com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException) SignInResult(com.amazonaws.mobile.client.results.SignInResult) Test(org.junit.Test)

Example 4 with SignInResult

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

the class AWSMobileClientPersistenceWithRestartabilityTest method testGetUserName.

@Test
public void testGetUserName() {
    signInAndVerifySignIn();
    assertNotNull(auth.getUsername());
    deleteAllEncryptionKeys();
    assertNull(auth.getUsername());
    try {
        final SignInResult signInResult = auth.signIn(username, PASSWORD, null);
        assertNotNull(signInResult);
        assertEquals(SignInState.DONE, signInResult.getSignInState());
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
    assertNotNull(auth.getUsername());
}
Also used : JSONException(org.json.JSONException) UserNotConfirmedException(com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException) SignInResult(com.amazonaws.mobile.client.results.SignInResult) Test(org.junit.Test)

Example 5 with SignInResult

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

the class AWSMobileClientPersistenceWithRestartabilityTest method signInAndVerifySignIn.

private void signInAndVerifySignIn() {
    try {
        final CountDownLatch stateNotificationLatch = new CountDownLatch(1);
        final AtomicReference<UserStateDetails> userState = new AtomicReference<UserStateDetails>();
        listener = new UserStateListener() {

            @Override
            public void onUserStateChanged(UserStateDetails details) {
                userState.set(details);
                auth.removeUserStateListener(listener);
                stateNotificationLatch.countDown();
            }
        };
        auth.addUserStateListener(listener);
        final SignInResult signInResult = auth.signIn(username, PASSWORD, null);
        assertEquals("Cannot support MFA in tests", SignInState.DONE, signInResult.getSignInState());
        assertTrue("isSignedIn is true", auth.isSignedIn());
        assertEquals(username, auth.getUsername());
        // Check credentials are available
        final AWSCredentials credentials = auth.getCredentials();
        assertNotNull("Credentials are null", credentials);
        assertNotNull("Access key is null", credentials.getAWSAccessKeyId());
        assertNotNull("Secret key is null", credentials.getAWSSecretKey());
        Tokens tokens = auth.getTokens();
        assertNotNull(tokens);
        Token accessToken = tokens.getAccessToken();
        assertNotNull(accessToken);
        assertTrue("Access token should not be expired", accessToken.getExpiration().after(new Date()));
        Token idToken = tokens.getIdToken();
        assertNotNull(idToken);
        assertTrue("Id token should not be expired", idToken.getExpiration().after(new Date()));
        Token refreshToken = tokens.getRefreshToken();
        assertNotNull(refreshToken);
        // Check one attribute
        final Map<String, String> userAttributes = auth.getUserAttributes();
        assertEquals(getPackageConfigure().getString("email"), userAttributes.get("email"));
        stateNotificationLatch.await(5, TimeUnit.SECONDS);
        UserStateDetails userStateDetails = userState.get();
        assertEquals(userStateDetails.getUserState(), UserState.SIGNED_IN);
        Map<String, String> details = userStateDetails.getDetails();
        assertNotEquals(getPackageConfigure().getString("identity_id"), details.toString());
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IdToken(com.amazonaws.mobileconnectors.cognitoauth.tokens.IdToken) RefreshToken(com.amazonaws.mobileconnectors.cognitoauth.tokens.RefreshToken) AccessToken(com.amazonaws.mobileconnectors.cognitoauth.tokens.AccessToken) Token(com.amazonaws.mobile.client.results.Token) CountDownLatch(java.util.concurrent.CountDownLatch) AWSCredentials(com.amazonaws.auth.AWSCredentials) Date(java.util.Date) JSONException(org.json.JSONException) UserNotConfirmedException(com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException) SignInResult(com.amazonaws.mobile.client.results.SignInResult) Tokens(com.amazonaws.mobile.client.results.Tokens)

Aggregations

SignInResult (com.amazonaws.mobile.client.results.SignInResult)29 Test (org.junit.Test)20 UserNotConfirmedException (com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException)16 JSONException (org.json.JSONException)11 Tokens (com.amazonaws.mobile.client.results.Tokens)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 CountDownLatch (java.util.concurrent.CountDownLatch)6 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)5 HashMap (java.util.HashMap)5 AWSCredentials (com.amazonaws.auth.AWSCredentials)4 NotAuthorizedException (com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException)4 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)4 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)3 Token (com.amazonaws.mobile.client.results.Token)3 InvalidParameterException (com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException)3 ResourceNotFoundException (com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException)3 UsernameExistsException (com.amazonaws.services.cognitoidentityprovider.model.UsernameExistsException)3 NonNull (androidx.annotation.NonNull)2 CognitoDevice (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice)2 CognitoUserSession (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession)2