Search in sources :

Example 6 with ApiAuthProviders

use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.

the class ApiRequestDecoratorFactoryTest method testOidcOverrideNotProvided.

/**
 * If a custom OIDC provider is not provided AND there is
 * an API that uses {@link AuthorizationType#OPENID_CONNECT}
 * as its auth mechanism, then the process will fail at runtime
 * while sending a request with that API.
 */
@Test
public void testOidcOverrideNotProvided() {
    ApiAuthProviders providers = ApiAuthProviders.noProviderOverrides();
    ApiRequestDecoratorFactory factory = new ApiRequestDecoratorFactory(providers, AuthorizationType.OPENID_CONNECT, "", EndpointType.GRAPHQL, "CONFIG_API_KEY");
    Request request = new Request.Builder().url("https://localhost/").build();
    assertThrows(ApiException.ApiAuthException.class, () -> factory.forAuthType(AuthorizationType.OPENID_CONNECT).decorate(request));
}
Also used : Request(okhttp3.Request) ApiAuthProviders(com.amplifyframework.api.aws.ApiAuthProviders) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 7 with ApiAuthProviders

use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.

the class ApiRequestDecoratorFactoryTest method testApiKeyProvidedInterceptSucceeds.

/**
 * If API key is not mentioned in API configuration AND auth mode is
 * API_KEY BUT a valid custom API key provider is provided via
 * ApiAuthProvider, then intercept succeeds.
 * @throws ApiException.ApiAuthException From factory.forAuthType
 */
@Test
public void testApiKeyProvidedInterceptSucceeds() throws ApiException.ApiAuthException {
    ApiAuthProviders providers = ApiAuthProviders.builder().apiKeyAuthProvider(() -> "CUSTOM_API_KEY").build();
    ApiRequestDecoratorFactory factory = new ApiRequestDecoratorFactory(providers, AuthorizationType.API_KEY, "", EndpointType.GRAPHQL, null);
    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));
}
Also used : Request(okhttp3.Request) ApiAuthProviders(com.amplifyframework.api.aws.ApiAuthProviders) Test(org.junit.Test)

Example 8 with ApiAuthProviders

use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.

the class ApiRequestDecoratorFactoryTest method testApiKeyNotProvidedInConfiguration.

/**
 * Test cases for when no API key is provided by the config file..
 */
@Test
public void testApiKeyNotProvidedInConfiguration() {
    ApiAuthProviders providers = ApiAuthProviders.noProviderOverrides();
    ApiRequestDecoratorFactory factory = new ApiRequestDecoratorFactory(providers, AuthorizationType.API_KEY, "", EndpointType.GRAPHQL, null);
    Request request = new Request.Builder().url("https://localhost/").build();
    assertThrows(ApiException.ApiAuthException.class, () -> factory.forAuthType(AuthorizationType.API_KEY).decorate(request));
}
Also used : Request(okhttp3.Request) ApiAuthProviders(com.amplifyframework.api.aws.ApiAuthProviders) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 9 with ApiAuthProviders

use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.

the class ApiRequestDecoratorFactoryTest method testOidcOverrideProvided.

/**
 * Test to confirm that passing any custom implementation of
 * {@link com.amplifyframework.api.aws.sigv4.OidcAuthProvider}
 * prevents crashes while intercepting requests.
 * @throws ApiException From API configuration
 */
@Test
public void testOidcOverrideProvided() throws ApiException {
    final String oidcJwtToken = "OIDC_JWT_TOKEN";
    ApiAuthProviders providers = ApiAuthProviders.builder().oidcAuthProvider(() -> oidcJwtToken).build();
    ApiRequestDecoratorFactory factory = new ApiRequestDecoratorFactory(providers, AuthorizationType.OPENID_CONNECT, "", EndpointType.GRAPHQL, "CONFIG_API_KEY");
    Request request = new Request.Builder().url("https://localhost/").build();
    Request decoratedRequest = factory.forAuthType(AuthorizationType.OPENID_CONNECT).decorate(request);
    assertEquals(oidcJwtToken, decoratedRequest.header(AUTHORIZATION));
}
Also used : Request(okhttp3.Request) ApiAuthProviders(com.amplifyframework.api.aws.ApiAuthProviders) Test(org.junit.Test)

Example 10 with ApiAuthProviders

use of com.amplifyframework.api.aws.ApiAuthProviders in project amplify-android by aws-amplify.

the class ApiRequestDecoratorFactoryTest method testCustomOverrideNotProvided.

/**
 * If a custom auth provider is not provided AND there is
 * an API that uses {@link AuthorizationType#AWS_LAMBDA}
 * as its auth mechanism, then the process will fail at runtime
 * while sending a request with that API.
 */
@Test
public void testCustomOverrideNotProvided() {
    ApiAuthProviders providers = ApiAuthProviders.noProviderOverrides();
    ApiRequestDecoratorFactory factory = new ApiRequestDecoratorFactory(providers, AuthorizationType.AWS_LAMBDA, "", EndpointType.GRAPHQL, "CONFIG_API_KEY");
    Request request = new Request.Builder().url("https://localhost/").build();
    assertThrows(ApiException.ApiAuthException.class, () -> factory.forAuthType(AuthorizationType.AWS_LAMBDA).decorate(request));
}
Also used : Request(okhttp3.Request) ApiAuthProviders(com.amplifyframework.api.aws.ApiAuthProviders) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Aggregations

ApiAuthProviders (com.amplifyframework.api.aws.ApiAuthProviders)11 Request (okhttp3.Request)9 Test (org.junit.Test)9 ApiException (com.amplifyframework.api.ApiException)3 Context (android.content.Context)1 Log (android.util.Log)1 RawRes (androidx.annotation.RawRes)1 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)1 CognitoJWTParser (com.amazonaws.mobileconnectors.cognitoidentityprovider.util.CognitoJWTParser)1 AmplifyException (com.amplifyframework.AmplifyException)1 ApiCategory (com.amplifyframework.api.ApiCategory)1 AWSApiPlugin (com.amplifyframework.api.aws.AWSApiPlugin)1 AuthModeStrategyType (com.amplifyframework.api.aws.AuthModeStrategyType)1 AuthorizationType (com.amplifyframework.api.aws.AuthorizationType)1 DefaultCognitoUserPoolsAuthProvider (com.amplifyframework.api.aws.sigv4.DefaultCognitoUserPoolsAuthProvider)1 AuthCategory (com.amplifyframework.auth.AuthCategory)1 AWSCognitoAuthPlugin (com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin)1 AuthSignOutOptions (com.amplifyframework.auth.options.AuthSignOutOptions)1 AuthSignInResult (com.amplifyframework.auth.result.AuthSignInResult)1 Amplify (com.amplifyframework.core.Amplify)1