Search in sources :

Example 21 with CognitoDevice

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice 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);
    }
}
Also used : MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) AuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) ChallengeContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation) AuthenticationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler) Semaphore(java.util.concurrent.Semaphore) CognitoUserSession(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession)

Example 22 with CognitoDevice

use of com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice 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));
        }
    }));
}
Also used : InstrumentationRegistry.getTargetContext(androidx.test.InstrumentationRegistry.getTargetContext) NonNull(androidx.annotation.NonNull) CognitoUserSession(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession) SignInResult(com.amazonaws.mobile.client.results.SignInResult) Await(com.amazonaws.mobileconnectors.appsync.util.Await) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) AuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) AuthenticationDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails) ChallengeContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation) AuthenticationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) Log(android.util.Log) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) CognitoDevice(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) SignInResult(com.amazonaws.mobile.client.results.SignInResult) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) AuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation) MultiFactorAuthenticationContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation) ChallengeContinuation(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation) AuthenticationHandler(com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler) CognitoUserSession(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession) Await(com.amazonaws.mobileconnectors.appsync.util.Await) AuthenticationDetails(com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails) NonNull(androidx.annotation.NonNull)

Aggregations

CognitoDevice (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoDevice)22 Test (org.junit.Test)15 ArgumentCaptor (org.mockito.ArgumentCaptor)12 CognitoUserSession (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession)10 AuthenticationContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation)10 ChallengeContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.ChallengeContinuation)10 MultiFactorAuthenticationContinuation (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.MultiFactorAuthenticationContinuation)10 AuthenticationHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler)10 AuthenticationDetails (com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails)7 GenericHandler (com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler)6 HashMap (java.util.HashMap)6 ReturningRunnable (com.amazonaws.mobile.client.internal.ReturningRunnable)5 FlowTracker (com.amazonaws.mobileconnectors.cognitoidentityprovider.utils.FlowTracker)4 InitiateAuthRequest (com.amazonaws.services.cognitoidentityprovider.model.InitiateAuthRequest)4 Map (java.util.Map)4 JSONException (org.json.JSONException)4 Ignore (org.junit.Ignore)4 SignInResult (com.amazonaws.mobile.client.results.SignInResult)3 ForgetDeviceRequest (com.amazonaws.services.cognitoidentityprovider.model.ForgetDeviceRequest)3 GetDeviceRequest (com.amazonaws.services.cognitoidentityprovider.model.GetDeviceRequest)3