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());
}
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());
}
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());
}
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());
}
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);
}
}
Aggregations