use of com.amazonaws.mobile.auth.core.IdentityManager in project aws-sdk-android by aws-amplify.
the class OAuth2Utils method fetchCognitoIdentity.
/**
* Fetch the Cognito Identity for the user.
* Register the SignProvider with permissions.
* Resume any previously signed in auth session and fetch the cognito
* federated identity for the user in order to connect to
* AWS services.
*
* @param context The activity context
* @param startupAuthResultHandler The callback function for resuming session
* @deprecated This method is no longer in use.
*/
private void fetchCognitoIdentity(final Context context, final StartupAuthResultHandler startupAuthResultHandler) {
try {
Log.d(TAG, "Fetching the Cognito Identity.");
// Create IdentityManager, register the providers and set the permissions.
final IdentityManager identityManager = new IdentityManager(context, this.awsConfiguration);
IdentityManager.setDefaultIdentityManager(identityManager);
if (this.signInProviderConfig == null) {
this.registerConfigSignInProviders(this.awsConfiguration);
} else {
this.registerUserSignInProvidersWithPermissions();
}
this.resumeSession((Activity) context, startupAuthResultHandler);
} catch (final Exception exception) {
Log.e(TAG, "Error occurred in fetching the Cognito Identity " + "and resuming the auth session", exception);
}
}
use of com.amazonaws.mobile.auth.core.IdentityManager in project aws-sdk-android by aws-amplify.
the class OAuth2Utils method registerConfigSignInProviders.
/**
* Register the SignInProvider and permissions based on the
* AWSConfiguration.
*/
private void registerConfigSignInProviders(final AWSConfiguration awsConfiguration) {
Log.d(TAG, "Using the SignInProviderConfig from `awsconfiguration.json`.");
final IdentityManager identityManager = IdentityManager.getDefaultIdentityManager();
try {
if (isConfigurationKeyPresent(USER_POOLS, awsConfiguration)) {
identityManager.addSignInProvider(CognitoUserPoolsSignInProvider.class);
}
if (isConfigurationKeyPresent(FACEBOOK, awsConfiguration)) {
identityManager.addSignInProvider(FacebookSignInProvider.class);
}
if (isConfigurationKeyPresent(GOOGLE, awsConfiguration)) {
identityManager.addSignInProvider(GoogleSignInProvider.class);
}
} catch (NoClassDefFoundError exception) {
// The above sign in providers are optional dependencies that are required for
// drop-in UI to work correctly. Not registering them will still allow users to
// sign in via other methods.
}
}
use of com.amazonaws.mobile.auth.core.IdentityManager in project aws-sdk-android by aws-amplify.
the class SignInUI method presentAuthUI.
/**
* Initiate the sign-in flow.
* Resume any previously signed-in Auth session.
* Check if the user is not signed in and present the AuthUI screen.
*/
private void presentAuthUI() {
Log.d(LOG_TAG, "Presenting the SignIn UI.");
final IdentityManager identityManager = IdentityManager.getDefaultIdentityManager();
final boolean canCancel = this.authUIConfiguration.getCanCancel();
identityManager.login(this.loginCallingActivity, new DefaultSignInResultHandler() {
@Override
public void onSuccess(Activity activity, IdentityProvider identityProvider) {
if (identityProvider != null) {
Log.d(LOG_TAG, "Sign-in succeeded. The identity provider name is available here using: " + identityProvider.getDisplayName());
startNextActivity(activity, loginNextActivity);
}
}
@Override
public boolean onCancel(Activity activity) {
// Return true to allow this.
return canCancel;
}
});
SignInActivity.startSignInActivity(this.loginCallingActivity, this.authUIConfiguration);
}
use of com.amazonaws.mobile.auth.core.IdentityManager in project aws-sdk-android by aws-amplify.
the class SignInUI method loginWithBuilder.
/**
* Present SignIn-UI screen if the user is not signed-in
* On successful sign-in, move to the next activity.
*/
private void loginWithBuilder(final LoginBuilder loginBuilder) {
try {
Log.d(LOG_TAG, "Initiating the SignIn flow.");
if (loginBuilder.getAuthUIConfiguration() != null) {
this.authUIConfiguration = loginBuilder.getAuthUIConfiguration();
}
final IdentityManager identityManager = IdentityManager.getDefaultIdentityManager();
if (loginBuilder.shouldFederate && identityManager.isUserSignedIn()) {
Log.d(LOG_TAG, "User is already signed-in. Moving to the next activity.");
startNextActivity(this.loginCallingActivity, this.loginNextActivity);
} else {
Log.d(LOG_TAG, "User is not signed-in. Presenting the SignInUI.");
presentAuthUI();
}
} catch (final Exception exception) {
Log.e(LOG_TAG, "Error occurred in sign-in ", exception);
}
}
use of com.amazonaws.mobile.auth.core.IdentityManager in project aws-sdk-android by aws-amplify.
the class OAuth2Utils method _initialize.
protected Runnable _initialize(final Context context, final AWSConfiguration awsConfiguration, final Callback<UserStateDetails> callback) {
return new Runnable() {
public void run() {
synchronized (initLockObject) {
if (AWSMobileClient.this.awsConfiguration != null) {
callback.onResult(getUserStateDetails(true));
return;
}
// Default value
mIsPersistenceEnabled = true;
// appropriately.
try {
if (awsConfiguration.optJsonObject(AUTH_KEY) != null && awsConfiguration.optJsonObject(AUTH_KEY).has("Persistence")) {
mIsPersistenceEnabled = awsConfiguration.optJsonObject(AUTH_KEY).getBoolean("Persistence");
}
} catch (final Exception ex) {
// If reading from awsconfiguration.json fails, invoke callback.
callback.onError(new RuntimeException("Failed to initialize AWSMobileClient; please check your awsconfiguration.json", ex));
return;
}
userAgentOverride = awsConfiguration.getUserAgentOverride();
mContext = context.getApplicationContext();
mStore = new AWSMobileClientStore(AWSMobileClient.this);
final IdentityManager identityManager = new IdentityManager(mContext);
identityManager.enableFederation(false);
identityManager.setConfiguration(awsConfiguration);
identityManager.setPersistenceEnabled(mIsPersistenceEnabled);
IdentityManager.setDefaultIdentityManager(identityManager);
registerConfigSignInProviders(awsConfiguration);
identityManager.addSignInStateChangeListener(new SignInStateChangeListener() {
@Override
public void onUserSignedIn() {
Log.d(TAG, "onUserSignedIn: Updating user state from drop-in UI");
signInState = SignInState.DONE;
com.amazonaws.mobile.auth.core.IdentityProvider currentIdentityProvider = identityManager.getCurrentIdentityProvider();
String token = currentIdentityProvider.getToken();
String providerKey = currentIdentityProvider.getCognitoLoginKey();
federatedSignInWithoutAssigningState(providerKey, token, new Callback<UserStateDetails>() {
@Override
public void onResult(UserStateDetails result) {
Log.d(TAG, "onResult: showSignIn federated");
setUserState(getUserStateDetails(false));
getSignInUILatch().countDown();
}
@Override
public void onError(Exception e) {
Log.w(TAG, "onError: User sign-in had errors from drop-in UI", e);
setUserState(getUserStateDetails(false));
getSignInUILatch().countDown();
}
});
}
@Override
public void onUserSignedOut() {
Log.d(TAG, "onUserSignedOut: Updating user state from drop-in UI");
setUserState(getUserStateDetails(false));
showSignInWaitLatch.countDown();
}
});
if (awsConfiguration.optJsonObject("CredentialsProvider") != null && awsConfiguration.optJsonObject("CredentialsProvider").optJSONObject("CognitoIdentity") != null) {
try {
JSONObject identityPoolJSON = awsConfiguration.optJsonObject("CredentialsProvider").getJSONObject("CognitoIdentity").getJSONObject(awsConfiguration.getConfiguration());
final String poolId = identityPoolJSON.getString("PoolId");
final String regionStr = identityPoolJSON.getString("Region");
final ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setUserAgent(DEFAULT_USER_AGENT + " " + awsConfiguration.getUserAgent());
if (userAgentOverride != null) {
clientConfig.setUserAgentOverride(userAgentOverride);
}
AmazonCognitoIdentityClient cibClient = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials(), clientConfig);
cibClient.setRegion(Region.getRegion(regionStr));
provider = new AWSMobileClientCognitoIdentityProvider(null, poolId, cibClient);
cognitoIdentity = new CognitoCachingCredentialsProvider(mContext, provider, Regions.fromName(regionStr));
cognitoIdentity.setPersistenceEnabled(mIsPersistenceEnabled);
if (userAgentOverride != null) {
cognitoIdentity.setUserAgentOverride(userAgentOverride);
}
} catch (Exception e) {
callback.onError(new RuntimeException("Failed to initialize Cognito Identity; please check your awsconfiguration.json", e));
return;
}
}
final JSONObject userPoolJSON = awsConfiguration.optJsonObject("CognitoUserPool");
if (userPoolJSON != null) {
try {
mUserPoolPoolId = userPoolJSON.getString("PoolId");
final String clientId = userPoolJSON.getString("AppClientId");
final String clientSecret = userPoolJSON.optString("AppClientSecret");
final String pinpointEndpointId = CognitoPinpointSharedContext.getPinpointEndpoint(context, userPoolJSON.optString("PinpointAppId"));
final String cognitoUserPoolCustomEndpoint = userPoolJSON.optString(COGNITO_USERPOOL_CUSTOM_ENDPOINT);
final ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setUserAgent(DEFAULT_USER_AGENT + " " + awsConfiguration.getUserAgent());
if (userAgentOverride != null) {
clientConfig.setUserAgentOverride(userAgentOverride);
}
userpoolLL = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), clientConfig);
userpoolLL.setRegion(com.amazonaws.regions.Region.getRegion(Regions.fromName(userPoolJSON.getString("Region"))));
userpoolsLoginKey = String.format("cognito-idp.%s.amazonaws.com/%s", userPoolJSON.getString("Region"), userPoolJSON.getString("PoolId"));
userpool = new CognitoUserPool(mContext, mUserPoolPoolId, clientId, clientSecret, userpoolLL, pinpointEndpointId, cognitoUserPoolCustomEndpoint);
userpool.setPersistenceEnabled(mIsPersistenceEnabled);
mDeviceOperations = new DeviceOperations(AWSMobileClient.this, userpoolLL);
} catch (Exception e) {
callback.onError(new RuntimeException("Failed to initialize Cognito Userpool; please check your awsconfiguration.json", e));
return;
}
}
JSONObject hostedUIJSON = getHostedUIJSON(awsConfiguration);
if (hostedUIJSON != null) {
try {
// Pre-warm the Custom Tabs based on
if (hostedUIJSON.has("TokenURI")) {
Log.d(TAG, "initialize: OAuth2 client detected");
mOAuth2Client = new OAuth2Client(mContext, AWSMobileClient.this);
mOAuth2Client.setPersistenceEnabled(mIsPersistenceEnabled);
mOAuth2Client.setUserAgentOverride(userAgentOverride);
} else {
_initializeHostedUI(hostedUIJSON);
}
} catch (Exception e) {
callback.onError(new RuntimeException("Failed to initialize OAuth, please check your awsconfiguration.json", e));
}
}
if (cognitoIdentity == null && userpool == null) {
callback.onError(new RuntimeException("Neither Cognito Identity or Cognito UserPool was used." + " At least one must be present to use AWSMobileClient."));
return;
}
AWSMobileClient.this.awsConfiguration = awsConfiguration;
final UserStateDetails userStateDetails = getUserStateDetails(true);
callback.onResult(userStateDetails);
setUserState(userStateDetails);
}
}
};
}
Aggregations