use of com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation in project aws-sdk-android by aws-amplify.
the class AWSMobileClientTest method testRevokeTokenWithSignedInUser.
@Test
public void testRevokeTokenWithSignedInUser() throws Exception {
auth.signIn(username, PASSWORD, null);
assertTrue("isSignedIn is true", auth.isSignedIn());
final AtomicReference<Boolean> tokenRevoked = new AtomicReference<Boolean>(false);
final CountDownLatch revokeTokenLatch = new CountDownLatch(2);
final CognitoUser user = userPool.getCurrentUser();
user.getSession(new AuthenticationHandler() {
@Override
public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
revokeTokenLatch.countDown();
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
}
@Override
public void authenticationChallenge(ChallengeContinuation continuation) {
}
@Override
public void onFailure(Exception exception) {
exception.printStackTrace();
fail("Sign in failed.");
}
});
user.getDetails(new GetDetailsHandler() {
@Override
public void onSuccess(CognitoUserDetails cognitoUserDetails) {
revokeTokenLatch.countDown();
}
@Override
public void onFailure(Exception exception) {
exception.printStackTrace();
fail("Get user details failed.");
}
});
try {
user.revokeTokens();
tokenRevoked.set(true);
} catch (Exception e) {
e.printStackTrace();
}
revokeTokenLatch.await(5, TimeUnit.SECONDS);
assertTrue(tokenRevoked.get());
user.getDetails(new GetDetailsHandler() {
@Override
public void onSuccess(CognitoUserDetails cognitoUserDetails) {
fail("Request to get user details should fail with NotAuthorizedException after token is revoked.");
}
@Override
public void onFailure(Exception exception) {
assertTrue(exception instanceof NotAuthorizedException);
}
});
}
use of com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation 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 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 authFlowType = authFlowTypeInConfig ? awsConfiguration.optJsonObject(AUTH_KEY).getString("authenticationFlowType") : null;
if (authFlowTypeInConfig && AUTH_TYPE_INIT_CUSTOM_AUTH.equals(authFlowType)) {
// 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 (authFlowTypeInConfig && AUTH_TYPE_INIT_USER_PASSWORD.equals(authFlowType)) {
// 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);
}
}
};
}
use of com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation in project aws-mobile-appsync-sdk-android by awslabs.
the class BasicCognitoUserPoolsAuthProvider method fetchToken.
/**
* Fetches token from the Cognito User Pools client for the current user.
*/
private synchronized void fetchToken() {
final Semaphore semaphore = new Semaphore(0);
lastTokenRetrievalFailureMessage = null;
userPool.getCurrentUser().getSessionInBackground(new AuthenticationHandler() {
@Override
public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
token = userSession.getAccessToken().getJWTToken();
semaphore.release();
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
lastTokenRetrievalFailureMessage = "Cognito Userpools is not signed-in";
semaphore.release();
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
lastTokenRetrievalFailureMessage = "Cognito Userpools is not signed-in";
semaphore.release();
}
@Override
public void authenticationChallenge(ChallengeContinuation continuation) {
lastTokenRetrievalFailureMessage = "Cognito Userpools is not signed-in";
semaphore.release();
}
@Override
public void onFailure(Exception exception) {
lastTokenRetrievalFailureMessage = "Cognito Userpools failed to get session";
semaphore.release();
}
});
try {
semaphore.acquire();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted waiting for Cognito Userpools token.", e);
}
if (lastTokenRetrievalFailureMessage != null) {
throw new RuntimeException(lastTokenRetrievalFailureMessage);
}
}
use of com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation in project aws-mobile-appsync-sdk-android by awslabs.
the class CustomCognitoUserPool method setup.
@NonNull
public static String setup() {
// Sign in the user.
Await.result((Await.ResultErrorEmitter<SignInResult, RuntimeException>) (onResult, onError) -> {
DelegatingMobileClientCallback<SignInResult> callback = DelegatingMobileClientCallback.to(onResult, exception -> onError.accept(new RuntimeException(exception)));
TestAWSMobileClient.instance(getTargetContext()).signIn("appsync-multi-auth-test-user", "P@ssw0rd!", null, callback);
});
// Build a custom cognito user pool.
AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
awsConfiguration.setConfiguration("Custom");
CognitoUserPool cognitoUserPool = new CognitoUserPool(getTargetContext(), awsConfiguration);
// Get the ID token for this user.
return Await.result((onResult, onError) -> cognitoUserPool.getUser("appsync-multi-auth-test-user").getSession(new AuthenticationHandler() {
@Override
public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) {
onResult.accept(userSession.getIdToken().getJWTToken());
}
@Override
public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
Log.d(TAG, "Sending password.");
authenticationContinuation.setAuthenticationDetails(new AuthenticationDetails("appsync-multi-auth-test-user", "P@ssw0rd!", null));
authenticationContinuation.continueTask();
}
@Override
public void getMFACode(MultiFactorAuthenticationContinuation continuation) {
}
@Override
public void authenticationChallenge(ChallengeContinuation continuation) {
}
@Override
public void onFailure(Exception exception) {
onError.accept(new RuntimeException(exception));
}
}));
}
Aggregations