use of com.amazonaws.mobile.client.AWSMobileClient in project amplify-android by aws-amplify.
the class AuthComponentTest method getEscapeHatch.
/**
* Tests that the getEscapeHatch method of the Auth wrapper of AWSMobileClient (AMC) returns the instance of
* AWSMobileClient held by the plugin.
*/
@Test
public void getEscapeHatch() {
AWSCognitoAuthPlugin plugin = (AWSCognitoAuthPlugin) authCategory.getPlugin(PLUGIN_KEY);
AWSMobileClient client = plugin.getEscapeHatch();
assertEquals(mobileClient, client);
}
use of com.amazonaws.mobile.client.AWSMobileClient in project aws-sdk-android by aws-amplify.
the class KinesisVideoCredentialsProviderImpl method updateCredentials.
@Override
protected KinesisVideoCredentials updateCredentials() throws KinesisVideoException {
// Refresh the token first
log.debug("Refreshing credentials");
credentialsProvider.refresh();
// Get the AWS credentials and create Kinesis Video Credentials
final AWSCredentials awsCredentials = credentialsProvider.getCredentials();
String sessionToken = null;
if (awsCredentials instanceof AWSSessionCredentials) {
final AWSSessionCredentials sessionCredentials = (AWSSessionCredentials) awsCredentials;
sessionToken = sessionCredentials.getSessionToken();
}
Date expiration = KinesisVideoCredentials.CREDENTIALS_NEVER_EXPIRE;
if (credentialsProvider instanceof CognitoCredentialsProvider) {
final CognitoCredentialsProvider cognitoCredentialsProvider = (CognitoCredentialsProvider) credentialsProvider;
expiration = cognitoCredentialsProvider.getSessionCredentialsExpiration();
log.debug("Refreshed token expiration is %s", expiration);
} else if (credentialsProvider instanceof AWSMobileClient) {
AWSMobileClient awsMobileClient = (AWSMobileClient) credentialsProvider;
try {
expiration = awsMobileClient.getTokens().getAccessToken().getExpiration();
log.debug("Refreshed token expiration is %s", expiration);
} catch (Exception e) {
throw new KinesisVideoException("Failed to refresh! " + e.getMessage());
}
}
log.debug("Returning %scredentials with expiration %s", sessionToken == null ? "" : "session ", expiration);
return new KinesisVideoCredentials(awsCredentials.getAWSAccessKeyId(), awsCredentials.getAWSSecretKey(), sessionToken, expiration);
}
use of com.amazonaws.mobile.client.AWSMobileClient in project hypertrack-live-android by hypertrack.
the class CognitoClient method signIn.
public void signIn(final String email, final String password, @NonNull final Callback callback) {
final AWSMobileClient awsMobileClient = AWSMobileClient.getInstance();
awsMobileClient.signIn(email, password, null, new InnerCallback<SignInResult>(callback) {
@Override
public void onResult(SignInResult result) {
sharedHelper.setAccountEmail(email);
updatePublishableKey(callback);
}
@Override
public void onError(Exception e) {
awsMobileClient.signIn(email.toLowerCase(), password, null, new InnerCallback<SignInResult>(callback) {
@Override
public void onResult(SignInResult result) {
sharedHelper.setAccountEmail(email);
updatePublishableKey(callback);
}
@Override
public void onError(Exception e) {
super.onError(e);
signUpEmail = email;
signUpPassword = password;
}
});
}
});
}
use of com.amazonaws.mobile.client.AWSMobileClient in project amplify-android by aws-amplify.
the class SynchronousMobileClient method initialize.
/**
* Initialize the client for use, in a synchronous way, by delegating to
* {@link AWSMobileClient#initialize(Context, Callback)}.
* @param context An Android Context
* @param awsConfiguration custom AWS configuration to use for initializing Mobile Client
* @return The result received by {@link AWSMobileClient#initialize(Context, Callback)}, if successful
* @throws MobileClientException A wrapped form of the error received by the async callback.
*/
@NonNull
public UserStateDetails initialize(@NonNull Context context, @NonNull AWSConfiguration awsConfiguration) throws MobileClientException {
Objects.requireNonNull(context);
Objects.requireNonNull(awsConfiguration);
final UserStateDetails userStateDetails;
try {
userStateDetails = Await.<UserStateDetails, Exception>result((onResult, onError) -> {
Callback<UserStateDetails> callback = DelegatingCallback.with(onResult, onError);
awsMobileClient.initialize(context, awsConfiguration, callback);
});
} catch (Exception initializationError) {
throw new MobileClientException("Failed to initialize Mobile Client", initializationError);
}
return Objects.requireNonNull(userStateDetails);
}
use of com.amazonaws.mobile.client.AWSMobileClient in project amplify-android by aws-amplify.
the class AWSCognitoAuthPlugin method configure.
@Override
public void configure(@NonNull JSONObject pluginConfiguration, @NonNull Context context) throws AuthException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Exception> asyncException = new AtomicReference<>();
JSONObject mobileClientConfig;
LogFactory.setLevel(LogFactory.Level.OFF);
try {
mobileClientConfig = new JSONObject(pluginConfiguration.toString());
mobileClientConfig.put("UserAgentOverride", UserAgent.string());
} catch (JSONException exception) {
throw new AuthException("Failed to set user agent string", exception, AmplifyException.REPORT_BUG_TO_AWS_SUGGESTION);
}
awsMobileClient.initialize(context, new AWSConfiguration(mobileClientConfig), new Callback<UserStateDetails>() {
@Override
public void onResult(UserStateDetails result) {
switch(result.getUserState()) {
case GUEST:
case SIGNED_OUT:
lastEvent = AuthChannelEventName.SIGNED_OUT;
userId = null;
break;
case SIGNED_IN:
lastEvent = AuthChannelEventName.SIGNED_IN;
userId = getUserIdFromToken(result.getDetails().get(MOBILE_CLIENT_TOKEN_KEY));
break;
case SIGNED_OUT_USER_POOLS_TOKENS_INVALID:
case SIGNED_OUT_FEDERATED_TOKENS_INVALID:
lastEvent = AuthChannelEventName.SESSION_EXPIRED;
userId = getUserIdFromToken(result.getDetails().get(MOBILE_CLIENT_TOKEN_KEY));
break;
default:
userId = null;
lastEvent = null;
}
// Set up a listener to asynchronously update the user id if the user state changes in the future
awsMobileClient.addUserStateListener(userStateDetails -> {
switch(userStateDetails.getUserState()) {
case SIGNED_OUT:
case GUEST:
userId = null;
if (lastEvent != AuthChannelEventName.SIGNED_OUT) {
lastEvent = AuthChannelEventName.SIGNED_OUT;
Amplify.Hub.publish(HubChannel.AUTH, HubEvent.create(AuthChannelEventName.SIGNED_OUT));
}
break;
case SIGNED_IN:
fetchAndSetUserId(() -> {
if (lastEvent != AuthChannelEventName.SIGNED_IN) {
lastEvent = AuthChannelEventName.SIGNED_IN;
Amplify.Hub.publish(HubChannel.AUTH, HubEvent.create(AuthChannelEventName.SIGNED_IN));
}
});
break;
case SIGNED_OUT_FEDERATED_TOKENS_INVALID:
case SIGNED_OUT_USER_POOLS_TOKENS_INVALID:
fetchAndSetUserId(() -> {
/* No response needed */
});
if (lastEvent != AuthChannelEventName.SESSION_EXPIRED) {
lastEvent = AuthChannelEventName.SESSION_EXPIRED;
Amplify.Hub.publish(HubChannel.AUTH, HubEvent.create(AuthChannelEventName.SESSION_EXPIRED));
}
break;
default:
userId = null;
}
});
latch.countDown();
}
@Override
public void onError(Exception error) {
asyncException.set(error);
latch.countDown();
}
});
try {
if (latch.await(SECONDS_BEFORE_TIMEOUT, TimeUnit.SECONDS)) {
if (asyncException.get() != null) {
throw new AuthException("Failed to instantiate AWSMobileClient", asyncException.get(), "See attached exception for more details");
}
return;
} else {
throw new AuthException("Failed to instantiate AWSMobileClient within " + SECONDS_BEFORE_TIMEOUT + " seconds", "Check network connectivity");
}
} catch (InterruptedException error) {
throw new AuthException("Failed to instantiate AWSMobileClient", error, "See attached exception for more details");
}
}
Aggregations