use of com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler 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.handlers.AuthenticationHandler 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