Search in sources :

Example 1 with Tokens

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

the class AuthComponentTest method signInWithWebUI.

/**
 * Tests that the signInWithWebUI method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.showSignIn
 * with the proper parameters and converts the returned result to the proper AuthSignInResult.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void signInWithWebUI() throws AuthException {
    Map<String, String> additionalInfoMap = Collections.singletonMap("testKey", "testVal");
    UserStateDetails userStateResult = new UserStateDetails(UserState.SIGNED_IN, additionalInfoMap);
    doAnswer(invocation -> {
        Callback<UserStateDetails> callback = invocation.getArgument(2);
        callback.onResult(userStateResult);
        return null;
    }).when(mobileClient).showSignIn(any(), any(), any());
    Tokens tokensResult = new Tokens(ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN);
    doAnswer(invocation -> {
        Callback<Tokens> callback = invocation.getArgument(0);
        callback.onResult(tokensResult);
        return null;
    }).when(mobileClient).getTokens(any());
    Activity activity = new Activity();
    String federationProviderName = "testFedProvider";
    String idpIdentifier = "testIdpID";
    String browserPackage = "org.mozilla.firefox";
    List<String> scopes = Collections.singletonList("scope");
    Map<String, String> signInMap = Collections.singletonMap("signInKey", "signInVal");
    Map<String, String> signOutMap = Collections.singletonMap("signOutKey", "signOutVal");
    Map<String, String> tokensMap = Collections.singletonMap("tokensKey", "tokensVal");
    AuthSignInResult result = synchronousAuth.signInWithWebUI(activity, AWSCognitoAuthWebUISignInOptions.builder().federationProviderName(federationProviderName).idpIdentifier(idpIdentifier).scopes(scopes).signInQueryParameters(signInMap).signOutQueryParameters(signOutMap).tokenQueryParameters(tokensMap).browserPackage(browserPackage).build());
    assertTrue(result.isSignInComplete());
    assertEquals(AuthSignInStep.DONE, result.getNextStep().getSignInStep());
    assertEquals(additionalInfoMap, result.getNextStep().getAdditionalInfo());
    ArgumentCaptor<SignInUIOptions> optionsCaptor = ArgumentCaptor.forClass(SignInUIOptions.class);
    verify(mobileClient).showSignIn(eq(activity), optionsCaptor.capture(), any());
    SignInUIOptions signInUIOptions = optionsCaptor.getValue();
    HostedUIOptions hostedUIOptions = signInUIOptions.getHostedUIOptions();
    assertNotNull(hostedUIOptions);
    assertNull(hostedUIOptions.getIdentityProvider());
    assertEquals(federationProviderName, hostedUIOptions.getFederationProviderName());
    assertEquals(idpIdentifier, hostedUIOptions.getIdpIdentifier());
    assertEquals(browserPackage, signInUIOptions.getBrowserPackage());
    assertArrayEquals(scopes.toArray(), hostedUIOptions.getScopes());
    assertEquals(signInMap, hostedUIOptions.getSignInQueryParameters());
    assertEquals(signOutMap, hostedUIOptions.getSignOutQueryParameters());
    assertEquals(tokensMap, hostedUIOptions.getTokenQueryParameters());
}
Also used : HostedUIOptions(com.amazonaws.mobile.client.HostedUIOptions) SignInUIOptions(com.amazonaws.mobile.client.SignInUIOptions) Activity(android.app.Activity) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) Tokens(com.amazonaws.mobile.client.results.Tokens) Test(org.junit.Test)

Example 2 with Tokens

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

the class AuthComponentTest method signInWithAuthFlow.

/**
 * Tests that the signIn method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.signIn with
 * the username, password it received, authFLowType it received, and, if included,
 * the metadata sent in the options object.
 * Also ensures that in the onResult case, 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 signInWithAuthFlow() throws AuthException {
    SignInResult amcResult = new SignInResult(SignInState.SMS_MFA, new UserCodeDeliveryDetails(DESTINATION, DELIVERY_MEDIUM, ATTRIBUTE_NAME));
    Tokens tokensResult = new Tokens(ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN);
    doAnswer(invocation -> {
        Callback<Tokens> callback = invocation.getArgument(0);
        callback.onResult(tokensResult);
        return null;
    }).when(mobileClient).getTokens(any());
    doAnswer(invocation -> {
        Callback<SignInResult> callback = invocation.getArgument(5);
        callback.onResult(amcResult);
        return null;
    }).when(mobileClient).signIn(any(), any(), any(), any(), any(), (Callback<SignInResult>) any());
    AuthSignInResult result = synchronousAuth.signIn(USERNAME, PASSWORD, AWSCognitoAuthSignInOptions.builder().metadata(CLIENTMETADATA).authFlowType(AuthFlowType.CUSTOM_AUTH).build());
    validateSignInResult(result, false, AuthSignInStep.CONFIRM_SIGN_IN_WITH_SMS_MFA_CODE);
    verify(mobileClient).signIn(eq(USERNAME), eq(PASSWORD), eq(CLIENTMETADATA), eq(Collections.emptyMap()), eq(com.amazonaws.services.cognitoidentityprovider.model.AuthFlowType.CUSTOM_AUTH), (Callback<SignInResult>) any());
}
Also used : UserCodeDeliveryDetails(com.amazonaws.mobile.client.results.UserCodeDeliveryDetails) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) SignInResult(com.amazonaws.mobile.client.results.SignInResult) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) Tokens(com.amazonaws.mobile.client.results.Tokens) Test(org.junit.Test)

Example 3 with Tokens

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

the class AuthComponentTest method signInWithSocialWebUI.

/**
 * Tests that the signInWithSocialWebUI method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.showSignIn
 * with the proper parameters and converts the returned result to the proper AuthSignInResult.
 * @throws AuthException test fails if this gets thrown since method should succeed
 */
@Test
public void signInWithSocialWebUI() throws AuthException {
    Map<String, String> additionalInfoMap = Collections.singletonMap("testKey", "testVal");
    UserStateDetails userStateResult = new UserStateDetails(UserState.SIGNED_IN, additionalInfoMap);
    doAnswer(invocation -> {
        Callback<UserStateDetails> callback = invocation.getArgument(2);
        callback.onResult(userStateResult);
        return null;
    }).when(mobileClient).showSignIn(any(), any(), any());
    Tokens tokensResult = new Tokens(ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN);
    doAnswer(invocation -> {
        Callback<Tokens> callback = invocation.getArgument(0);
        callback.onResult(tokensResult);
        return null;
    }).when(mobileClient).getTokens(any());
    Activity activity = new Activity();
    AuthSignInResult result = synchronousAuth.signInWithSocialWebUI(AuthProvider.facebook(), activity);
    assertTrue(result.isSignInComplete());
    assertEquals(AuthSignInStep.DONE, result.getNextStep().getSignInStep());
    assertEquals(additionalInfoMap, result.getNextStep().getAdditionalInfo());
    ArgumentCaptor<SignInUIOptions> optionsCaptor = ArgumentCaptor.forClass(SignInUIOptions.class);
    verify(mobileClient).showSignIn(eq(activity), optionsCaptor.capture(), any());
    HostedUIOptions hostedUIOptions = optionsCaptor.getValue().getHostedUIOptions();
    assertNotNull(hostedUIOptions);
    assertEquals("Facebook", hostedUIOptions.getIdentityProvider());
}
Also used : HostedUIOptions(com.amazonaws.mobile.client.HostedUIOptions) SignInUIOptions(com.amazonaws.mobile.client.SignInUIOptions) Activity(android.app.Activity) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AuthSignInResult(com.amplifyframework.auth.result.AuthSignInResult) Tokens(com.amazonaws.mobile.client.results.Tokens) Test(org.junit.Test)

Example 4 with Tokens

use of com.amazonaws.mobile.client.results.Tokens 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 5 with Tokens

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

the class DefaultCognitoUserPoolsAuthProvider method fetchToken.

// Fetches token from the mobile client.
private synchronized void fetchToken() throws ApiException {
    final Semaphore semaphore = new Semaphore(0);
    lastTokenRetrievalFailureMessage = null;
    awsMobileClient.getTokens(new Callback<Tokens>() {

        @Override
        public void onResult(Tokens result) {
            token = result.getAccessToken().getTokenString();
            semaphore.release();
        }

        @Override
        public void onError(Exception error) {
            lastTokenRetrievalFailureMessage = error.getLocalizedMessage();
            semaphore.release();
        }
    });
    try {
        semaphore.acquire();
    } catch (InterruptedException exception) {
        throw new ApiException("Interrupted waiting for Cognito Userpools token.", exception, AmplifyException.TODO_RECOVERY_SUGGESTION);
    }
    if (lastTokenRetrievalFailureMessage != null) {
        throw new ApiAuthException(lastTokenRetrievalFailureMessage, AmplifyException.TODO_RECOVERY_SUGGESTION);
    }
}
Also used : ApiAuthException(com.amplifyframework.api.ApiException.ApiAuthException) Semaphore(java.util.concurrent.Semaphore) AmplifyException(com.amplifyframework.AmplifyException) ApiException(com.amplifyframework.api.ApiException) ApiAuthException(com.amplifyframework.api.ApiException.ApiAuthException) Tokens(com.amazonaws.mobile.client.results.Tokens) ApiException(com.amplifyframework.api.ApiException)

Aggregations

Tokens (com.amazonaws.mobile.client.results.Tokens)20 Test (org.junit.Test)15 SignInResult (com.amazonaws.mobile.client.results.SignInResult)9 AWSCredentials (com.amazonaws.auth.AWSCredentials)7 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)6 JSONException (org.json.JSONException)6 UserStateDetails (com.amazonaws.mobile.client.UserStateDetails)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 AmazonClientException (com.amazonaws.AmazonClientException)4 Token (com.amazonaws.mobile.client.results.Token)4 UserCodeDeliveryDetails (com.amazonaws.mobile.client.results.UserCodeDeliveryDetails)4 UserNotConfirmedException (com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException)4 RandomString (com.amplifyframework.testutils.random.RandomString)4 Date (java.util.Date)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)3 AmplifyException (com.amplifyframework.AmplifyException)3 Activity (android.app.Activity)2 HostedUIOptions (com.amazonaws.mobile.client.HostedUIOptions)2