Search in sources :

Example 21 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration in project aws-sdk-android by aws-amplify.

the class SignUpActivity method onCreate.

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    signUpView = (SignUpView) findViewById(R.id.signup_view);
    Context appContext = getApplicationContext();
    mUserPool = new CognitoUserPool(appContext, new AWSConfiguration(appContext));
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
Also used : Context(android.content.Context) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) InputMethodManager(android.view.inputmethod.InputMethodManager) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration)

Example 22 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration in project amplify-android by aws-amplify.

the class AuthComponentConfigureTest method testConfigure.

/**
 * Tests that the configure method of the Auth wrapper of AWSMobileClient (AMC) calls AMC.initialize() with the
 * passed in context, a new AWSConfiguration object containing the passed in JSONObject, and a callback object
 * which causes configure to complete successfully in the onResult case.
 * @throws AmplifyException an exception wrapping the exception returned in onError of AMC.initialize()
 * @throws JSONException has to be declared as part of creating a test JSON object
 */
@Test
public void testConfigure() throws AmplifyException, JSONException {
    UserStateDetails userStateDetails = new UserStateDetails(UserState.SIGNED_OUT, null);
    Context context = getApplicationContext();
    JSONObject pluginConfig = new JSONObject().put("TestKey", "TestVal");
    pluginConfig.put("UserAgentOverride", UserAgent.string());
    JSONObject json = new JSONObject().put("plugins", new JSONObject().put(PLUGIN_KEY, pluginConfig));
    AuthCategoryConfiguration authConfig = new AuthCategoryConfiguration();
    authConfig.populateFromJSON(json);
    doAnswer(invocation -> {
        Callback<UserStateDetails> callback = invocation.getArgument(2);
        callback.onResult(userStateDetails);
        return null;
    }).when(mobileClient).initialize(any(), any(), any());
    authCategory.configure(authConfig, context);
    ArgumentCaptor<AWSConfiguration> awsConfigCaptor = ArgumentCaptor.forClass(AWSConfiguration.class);
    verify(mobileClient).initialize(eq(context), awsConfigCaptor.capture(), any());
    String returnedConfig = awsConfigCaptor.getValue().toString();
    String inputConfig = pluginConfig.toString();
    // Strip the opening and closing braces from the test input and ensure that the key/value pair is included
    // in the returned aws config.
    assertTrue(returnedConfig.contains(inputConfig.substring(1, inputConfig.length() - 1)));
}
Also used : Context(android.content.Context) ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) JSONObject(org.json.JSONObject) AuthCategoryConfiguration(com.amplifyframework.auth.AuthCategoryConfiguration) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) Test(org.junit.Test)

Example 23 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration 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);
}
Also used : Objects(java.util.Objects) Context(android.content.Context) Consumer(com.amplifyframework.core.Consumer) Nullable(androidx.annotation.Nullable) Callback(com.amazonaws.mobile.client.Callback) NonNull(androidx.annotation.NonNull) Await(com.amplifyframework.testutils.Await) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) SignInResult(com.amazonaws.mobile.client.results.SignInResult) AWSMobileClient(com.amazonaws.mobile.client.AWSMobileClient) ApplicationProvider(androidx.test.core.app.ApplicationProvider) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) Callback(com.amazonaws.mobile.client.Callback) UserStateDetails(com.amazonaws.mobile.client.UserStateDetails) NonNull(androidx.annotation.NonNull)

Example 24 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration 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)}. It constructs a default
 * instance of {@link AWSConfiguration} using the provided context to locate
 * awsconfiguration.json file.
 * @param context An Android Context
 * @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) throws MobileClientException {
    Objects.requireNonNull(context);
    AWSConfiguration configuration = new AWSConfiguration(context);
    return initialize(context, configuration);
}
Also used : AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) NonNull(androidx.annotation.NonNull)

Example 25 with AWSConfiguration

use of com.amazonaws.mobile.config.AWSConfiguration in project aws-sdk-android by aws-amplify.

the class AWSMobileClientTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    setUpCredentials();
    Context appContext = ApplicationProvider.getApplicationContext();
    final CountDownLatch latch = new CountDownLatch(1);
    AWSMobileClient.getInstance().initialize(appContext, new Callback<UserStateDetails>() {

        @Override
        public void onResult(UserStateDetails result) {
            latch.countDown();
        }

        @Override
        public void onError(Exception e) {
            latch.countDown();
        }
    });
    latch.await();
    final AWSConfiguration awsConfiguration = AWSMobileClient.getInstance().getConfiguration();
    JSONObject userPoolConfig = awsConfiguration.optJsonObject("CognitoUserPool");
    assertNotNull(userPoolConfig);
    clientRegion = Regions.fromName(userPoolConfig.getString("Region"));
    userPoolId = userPoolConfig.getString("PoolId");
    clientId = userPoolConfig.getString("AppClientId");
    clientSecret = userPoolConfig.optString("AppClientSecret");
    JSONObject identityPoolConfig = awsConfiguration.optJsonObject("CredentialsProvider").getJSONObject("CognitoIdentity").getJSONObject("Default");
    assertNotNull(identityPoolConfig);
    identityPoolId = identityPoolConfig.getString("PoolId");
    userPool = new CognitoUserPool(appContext, userPoolId, clientId, clientSecret, clientRegion);
    deleteAllUsers(userPoolId);
    createUserViaAdminAPI(userPoolId, USERNAME_ADMIN_API_USER, EMAIL_ADMIN_API_USER);
}
Also used : Context(android.content.Context) CognitoUserPool(com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool) JSONObject(org.json.JSONObject) CountDownLatch(java.util.concurrent.CountDownLatch) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) UsernameExistsException(com.amazonaws.services.cognitoidentityprovider.model.UsernameExistsException) JSONException(org.json.JSONException) UserNotConfirmedException(com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException) NotAuthorizedException(com.amazonaws.services.cognitoidentityprovider.model.NotAuthorizedException) ResourceNotFoundException(com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException) InvalidParameterException(com.amazonaws.services.cognitoidentityprovider.model.InvalidParameterException) BeforeClass(org.junit.BeforeClass)

Aggregations

AWSConfiguration (com.amazonaws.mobile.config.AWSConfiguration)48 JSONObject (org.json.JSONObject)28 Test (org.junit.Test)26 JSONException (org.json.JSONException)16 Context (android.content.Context)13 NonNull (androidx.annotation.NonNull)11 BasicAPIKeyAuthProvider (com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 BeforeClass (org.junit.BeforeClass)7 AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)6 UserStateDetails (com.amazonaws.mobile.client.UserStateDetails)4 AWSCredentials (com.amazonaws.auth.AWSCredentials)3 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)3 SignInResult (com.amazonaws.mobile.client.results.SignInResult)3 CognitoUserPool (com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool)3 UserNotConfirmedException (com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException)3 Nullable (androidx.annotation.Nullable)2 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)2 AWSMobileClient (com.amazonaws.mobile.client.AWSMobileClient)2 Callback (com.amazonaws.mobile.client.Callback)2