use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.
the class AWSMobileClientTest method testRevokeTokenWithSignedOutUser.
@Test
public void testRevokeTokenWithSignedOutUser() throws Exception {
auth.signIn(username, PASSWORD, null);
assertTrue("isSignedIn is true", auth.isSignedIn());
final CountDownLatch revokeTokenLatch = new CountDownLatch(1);
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.");
}
});
revokeTokenLatch.await(5, TimeUnit.SECONDS);
auth.signOut();
assertFalse("isSignedIn is false", auth.isSignedIn());
try {
user.revokeTokens();
} catch (Exception e) {
assertTrue(e instanceof InvalidParameterException);
}
}
use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.
the class CognitoUser method getSession.
/**
* getSession orchestrates the SignIn flow with Amazon Cognito UserPools.
*
* <p>
* This method is synchronous and performs network operations
* on the same thread in which the method is called. Calling this
* method in the MainThread will result in NetworkOnMainThreadException
* </p>
*
* <p>
* 1) Read the tokens (Id, Access and Refresh) that are cached on the device.
* 1.1) If the Id and Access tokens are present and they are valid, the
* {@link AuthenticationHandler#onSuccess(CognitoUserSession, CognitoDevice)}.
* will be called with a {@link CognitoUserSession} that has references to the valid tokens.
* This means that the user is signed-in.
* 1.2) If the Id and Access tokens are expired, and if there is a valid refresh token,
* a network call is made to get new Id and Access tokens.
* If valid Id and Access tokens are retrieved, they are cached on the device
* and {@link AuthenticationHandler#onSuccess(CognitoUserSession, CognitoDevice)}
* will be called with a {@link CognitoUserSession} that has references to the valid
* tokens. This means that the user is signed-in.
*
* 2) If there are no valid tokens cached on the device, the callback method
* {@link AuthenticationHandler#getAuthenticationDetails(AuthenticationContinuation, String)}
* will be called where the {@link AuthenticationDetails} will need to be supplied
* to continue the SignIn operation. See
* {@link com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.CognitoIdentityProviderContinuation}
* for details on continuation objects.
*
* 3) In all other error scenarios, {@link AuthenticationHandler#onFailure(Exception)} will
* be called with the type and message of the exception and it is the responsibility of
* the caller to handle the exceptions appropriately.
* </p>
*
* @param clientMetadata A map of custom key-value pairs that is passed to the lambda function for
* custom workflow.
* @param callback REQUIRED: {@link AuthenticationHandler} callback
*/
public void getSession(final Map<String, String> clientMetadata, final AuthenticationHandler callback) {
if (callback == null) {
throw new InvalidParameterException("callback is null");
}
try {
getCachedSession();
callback.onSuccess(cipSession, null);
} catch (final InvalidParameterException e) {
callback.onFailure(e);
} catch (final CognitoNotAuthorizedException e) {
final AuthenticationContinuation authenticationContinuation = new AuthenticationContinuation(this, context, AuthenticationContinuation.RUN_IN_CURRENT, callback);
authenticationContinuation.setClientMetaData(clientMetadata);
callback.getAuthenticationDetails(authenticationContinuation, getUserId());
} catch (final Exception e) {
callback.onFailure(e);
}
}
use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.
the class CognitoIdentityProviderChangePasswordInstrumentedTest method changePasswordInCurrentWithNoCachedTokens.
@Test
public void changePasswordInCurrentWithNoCachedTokens() throws Exception {
testUser = testPool.getUser(TEST_USER_NAME);
// Set mock result for the change password request API call
InvalidParameterException exception = new InvalidParameterException("password change request failed");
doThrow(exception).when(mockCSIClient).changePassword(any(ChangePasswordRequest.class));
final FlowTracker tracker = new FlowTracker("onFailure");
tracker.activate();
testUser.changePasswordInBackground(TEST_USER_PASSWORD, TEST_USER_NEW_PASS, new GenericHandler() {
@Override
public void onSuccess() {
assertTrue(tracker.check("onSuccess"));
}
@Override
public void onFailure(Exception exception) {
assertTrue(tracker.check("onFailure"));
assertNotNull(exception);
}
});
}
use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.
the class CognitoIdentityProviderChangePasswordInstrumentedTest method changePasswordInBackgroundThreadWithNoCachedTokens.
@Test
public void changePasswordInBackgroundThreadWithNoCachedTokens() throws Exception {
testUser = testPool.getUser(TEST_USER_NAME);
// Set mock result for the change password request API call
InvalidParameterException exception = new InvalidParameterException("password change request failed");
doThrow(exception).when(mockCSIClient).changePassword(any(ChangePasswordRequest.class));
final FlowTracker tracker = new FlowTracker("onFailure");
tracker.activate();
testUser.changePasswordInBackground(TEST_USER_PASSWORD, TEST_USER_NEW_PASS, new GenericHandler() {
@Override
public void onSuccess() {
assertTrue(tracker.check("onSuccess"));
}
@Override
public void onFailure(Exception exception) {
assertTrue(tracker.check("onFailure"));
assertNotNull(exception);
}
});
}
use of com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException in project aws-sdk-android by aws-amplify.
the class CognitoIdentityProviderChangePasswordInstrumentedTest method changePasswordInCurrentThreadWithCachedTokensException.
// Test change password, exception when run in current thread
@Test
public void changePasswordInCurrentThreadWithCachedTokensException() throws Exception {
testUser = testPool.getUser(TEST_USER_NAME);
// Set mock result for the change password request API call
InvalidParameterException exception = new InvalidParameterException("password change request failed");
doThrow(exception).when(mockCSIClient).changePassword(any(ChangePasswordRequest.class));
// Store tokens in shared preferences
SharedPreferences sharedPreferences = appContext.getSharedPreferences("CognitoIdentityProviderCache", Context.MODE_PRIVATE);
awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "idToken", getValidJWT(360000L));
awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "accessToken", getValidJWT(360000L));
awsKeyValueStorageUtility.put("CognitoIdentityProvider." + TEST_CLIENT_ID + "." + TEST_USER_NAME + "." + "refreshToken", TEST_CACHED_RTOKEN);
final FlowTracker tracker = new FlowTracker("onFailure");
tracker.activate();
testUser.changePassword(TEST_USER_PASSWORD, TEST_USER_NEW_PASS, new GenericHandler() {
@Override
public void onSuccess() {
assertTrue(tracker.check("onSuccess"));
}
@Override
public void onFailure(Exception exception) {
assertTrue(tracker.check("onFailure"));
ArgumentCaptor<ChangePasswordRequest> argumentCaptor = ArgumentCaptor.forClass(ChangePasswordRequest.class);
verify(mockCSIClient).changePassword(argumentCaptor.capture());
ChangePasswordRequest requestSent = argumentCaptor.getValue();
assertNotNull(requestSent);
assertEquals(TEST_USER_PASSWORD, requestSent.getPreviousPassword());
assertEquals(TEST_USER_NEW_PASS, requestSent.getProposedPassword());
assertNotNull(requestSent.getAccessToken());
assertNotNull(exception);
}
});
}
Aggregations