use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.
the class ApiRequestDecoratorFactoryTest method testApiKeyOverrideNotProvided.
/**
* Test cases for when no custom provider is given
* for {@link com.amplifyframework.api.aws.sigv4.ApiKeyAuthProvider}.
* This is the recommended path, and a customer should
* never have to provide a custom provider.
* @throws ApiException From API configuration
*/
@Test
public void testApiKeyOverrideNotProvided() throws ApiException {
final String apiKey = "API_KEY_1";
ApiAuthProviders providers = ApiAuthProviders.noProviderOverrides();
ApiRequestDecoratorFactory factory = new ApiRequestDecoratorFactory(providers, AuthorizationType.API_KEY, "", EndpointType.GRAPHQL, apiKey);
Request request = new Request.Builder().url("https://localhost/").build();
Request decoratedRequest = factory.forAuthType(AuthorizationType.API_KEY).decorate(request);
assertEquals(apiKey, decoratedRequest.header(X_API_KEY));
}
use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.
the class ApiRequestDecoratorFactoryTest method testApiKeyOverrideProvided.
/**
* Test cases for when a custom implementation of
* {@link com.amplifyframework.api.aws.sigv4.ApiKeyAuthProvider}
* is provided to the plugin to override the default method
* of obtaining API key directly from configuration.
* @throws ApiException From API configuration
*/
@Test
public void testApiKeyOverrideProvided() throws ApiException {
ApiAuthProviders providers = ApiAuthProviders.builder().apiKeyAuthProvider(() -> "CUSTOM_API_KEY").build();
ApiRequestDecoratorFactory factory = new ApiRequestDecoratorFactory(providers, AuthorizationType.API_KEY, "", EndpointType.GRAPHQL, "CONFIG_API_KEY");
Request request = new Request.Builder().url("https://localhost/").build();
Request decoratedRequest = factory.forAuthType(AuthorizationType.API_KEY).decorate(request);
assertEquals("CUSTOM_API_KEY", decoratedRequest.header(X_API_KEY));
}
use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.
the class ApiRequestDecoratorFactoryTest method testCustomOverrideProvided.
/**
* Test to confirm that passing any custom implementation of
* {@link FunctionAuthProvider}
* prevents crashes while intercepting requests.
* @throws ApiException From API configuration
*/
@Test
public void testCustomOverrideProvided() throws ApiException {
final String customToken = "CUSTOM_TOKEN";
ApiAuthProviders providers = ApiAuthProviders.builder().functionAuthProvider(() -> customToken).build();
ApiRequestDecoratorFactory factory = new ApiRequestDecoratorFactory(providers, AuthorizationType.AWS_LAMBDA, "", EndpointType.GRAPHQL, "CONFIG_API_KEY");
Request request = new Request.Builder().url("https://localhost/").build();
Request decoratedRequest = factory.forAuthType(AuthorizationType.AWS_LAMBDA).decorate(request);
assertEquals(customToken, decoratedRequest.header(AUTHORIZATION));
}
use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.
the class OwnerBasedAuthTest method configurePlugin.
private void configurePlugin() throws ApiException {
ApiAuthProviders providers = ApiAuthProviders.builder().cognitoUserPoolsAuthProvider(cognitoProvider).oidcAuthProvider(oidcProvider).build();
JSONObject configuration = new JSONObject();
try {
configuration = configuration.put(GRAPHQL_API_WITH_API_KEY, new JSONObject().put("endpointType", "GraphQL").put("endpoint", baseUrl.url()).put("region", "us-east-1").put("authorizationType", "API_KEY").put("apiKey", "FAKE-API-KEY")).put(GRAPHQL_API_WITH_COGNITO, new JSONObject().put("endpointType", "GraphQL").put("endpoint", baseUrl.url()).put("region", "us-east-1").put("authorizationType", "AMAZON_COGNITO_USER_POOLS")).put(GRAPHQL_API_WITH_OIDC, new JSONObject().put("endpointType", "GraphQL").put("endpoint", baseUrl.url()).put("region", "us-east-1").put("authorizationType", "OPENID_CONNECT"));
} catch (JSONException exception) {
// This shouldn't happen...
}
plugin = AWSApiPlugin.builder().apiAuthProviders(providers).build();
plugin.configure(configuration, ApplicationProvider.getApplicationContext());
}
use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.
the class MultiAuthSyncEngineInstrumentationTest method configure.
/**
* Method used to configure each scenario.
* @param modelType The model type.
* @param signInToCognito Does the test scenario require the user to be logged in with user pools.
* @param signInWithOidc Does the test scenario require the user to be logged in with an OIDC provider.
* @param expectedAuthType The auth type that should succeed for the test.
* @throws AmplifyException No expected.
* @throws IOException Not expected.
*/
private void configure(Class<? extends Model> modelType, boolean signInToCognito, boolean signInWithOidc, AuthorizationType expectedAuthType) throws AmplifyException, IOException {
Amplify.addPlugin(new AndroidLoggingPlugin(LogLevel.VERBOSE));
String tag = modelType.getSimpleName();
MultiAuthTestModelProvider modelProvider = MultiAuthTestModelProvider.getInstance(Collections.singletonList(modelType));
SchemaRegistry schemaRegistry = SchemaRegistry.instance();
ModelSchema modelSchema = ModelSchema.fromModelClass(modelType);
schemaRegistry.register(modelType.getSimpleName(), modelSchema);
StrictMode.enable();
Context context = getApplicationContext();
@RawRes int configResourceId = Resources.getRawResourceId(context, "amplifyconfiguration");
AmplifyConfiguration amplifyConfiguration = AmplifyConfiguration.fromConfigFile(context, configResourceId);
readCredsFromConfig(context);
// Setup an auth plugin
CategoryConfiguration authCategoryConfiguration = amplifyConfiguration.forCategoryType(CategoryType.AUTH);
// Turn off persistence so the mobile client's state for one test does not interfere with the others.
try {
authCategoryConfiguration.getPluginConfig("awsCognitoAuthPlugin").getJSONObject("Auth").getJSONObject("Default").put("Persistence", false);
} catch (JSONException exception) {
exception.printStackTrace();
fail();
return;
}
AuthCategory authCategory = new AuthCategory();
AWSCognitoAuthPlugin authPlugin = new AWSCognitoAuthPlugin();
authCategory.addPlugin(authPlugin);
authCategory.configure(authCategoryConfiguration, context);
auth = SynchronousAuth.delegatingTo(authCategory);
if (signInToCognito) {
Log.v(tag, "Test requires signIn.");
AuthSignInResult authSignInResult = auth.signIn(cognitoUser, cognitoPassword);
if (!authSignInResult.isSignInComplete()) {
fail("Unable to complete initial sign-in");
}
}
if (signInWithOidc) {
oidcLogin();
if (token.get() == null) {
fail("Unable to autenticate with OIDC provider");
}
}
// Setup an API
DefaultCognitoUserPoolsAuthProvider cognitoProvider = new DefaultCognitoUserPoolsAuthProvider(authPlugin.getEscapeHatch());
CategoryConfiguration apiCategoryConfiguration = amplifyConfiguration.forCategoryType(CategoryType.API);
ApiAuthProviders apiAuthProviders = ApiAuthProviders.builder().cognitoUserPoolsAuthProvider(cognitoProvider).awsCredentialsProvider(authPlugin.getEscapeHatch()).oidcAuthProvider(token::get).build();
ApiCategory apiCategory = new ApiCategory();
requestInterceptor = new HttpRequestInterceptor(expectedAuthType);
apiCategory.addPlugin(AWSApiPlugin.builder().configureClient("DataStoreIntegTestsApi", okHttpClientBuilder -> okHttpClientBuilder.addInterceptor(requestInterceptor)).apiAuthProviders(apiAuthProviders).build());
apiCategory.configure(apiCategoryConfiguration, context);
api = SynchronousApi.delegatingTo(apiCategory);
// Setup DataStore
DataStoreConfiguration dsConfig = DataStoreConfiguration.builder().errorHandler(exception -> Log.e(tag, "DataStore error handler received an error.", exception)).syncExpression(modelSchema.getName(), () -> Where.id("FAKE_ID").getQueryPredicate()).build();
CategoryConfiguration dataStoreCategoryConfiguration = AmplifyConfiguration.fromConfigFile(context, configResourceId).forCategoryType(CategoryType.DATASTORE);
String databaseName = "IntegTest" + modelType.getSimpleName() + ".db";
SQLiteStorageAdapter sqLiteStorageAdapter = TestStorageAdapter.create(schemaRegistry, modelProvider, databaseName);
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder().storageAdapter(sqLiteStorageAdapter).modelProvider(modelProvider).apiCategory(apiCategory).authModeStrategy(AuthModeStrategyType.MULTIAUTH).schemaRegistry(schemaRegistry).dataStoreConfiguration(dsConfig).build();
DataStoreCategory dataStoreCategory = new DataStoreCategory();
dataStoreCategory.addPlugin(awsDataStorePlugin);
dataStoreCategory.configure(dataStoreCategoryConfiguration, context);
dataStoreCategory.initialize(context);
dataStore = SynchronousDataStore.delegatingTo(dataStoreCategory);
}
Aggregations