Search in sources :

Example 1 with HttpAuthentication

use of org.projectnessie.client.http.HttpAuthentication in project nessie by projectnessie.

the class TestAwsAuthProvider method checkAuth.

void checkAuth(NessieAuthentication authentication, String expectedRegion, String expectedKeyId) {
    assertThat(authentication).isInstanceOf(HttpAuthentication.class);
    HttpAuthentication httpAuthentication = (HttpAuthentication) authentication;
    // Intercept the call to HttpClient.register(RequestFilter) and extract the RequestFilter for
    // our test
    RequestFilter[] authFilter = new RequestFilter[1];
    HttpClient.Builder client = Mockito.mock(HttpClient.Builder.class);
    Mockito.doAnswer(invocationOnMock -> {
        Object[] args = invocationOnMock.getArguments();
        if (args.length == 1 && args[0] instanceof RequestFilter && args[0].getClass().getSimpleName().equalsIgnoreCase("AwsHttpAuthenticationFilter")) {
            authFilter[0] = (RequestFilter) args[0];
        }
        return null;
    }).when(client).addRequestFilter(Mockito.any());
    httpAuthentication.applyToHttpClient(client);
    // Check that the registered RequestFilter works as expected (sets the right HTTP headers)
    assertThat(authFilter[0]).isInstanceOf(RequestFilter.class);
    HttpHeaders headers = new HttpHeaders();
    RequestContext context = new RequestContext(headers, URI.create("http://localhost/"), Method.GET, null);
    authFilter[0].filter(context);
    assertThat(headers.asMap()).containsKey("Authorization").containsKey("X-Amz-Date").extracting("Authorization", InstanceOfAssertFactories.iterable(String.class)).first(InstanceOfAssertFactories.STRING).containsSequence(expectedRegion).containsSequence(expectedKeyId).containsSequence("Signature=").containsSequence("Credential=");
}
Also used : HttpHeaders(org.projectnessie.client.http.HttpHeaders) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) HttpClient(org.projectnessie.client.http.HttpClient) RequestContext(org.projectnessie.client.http.RequestContext) RequestFilter(org.projectnessie.client.http.RequestFilter)

Example 2 with HttpAuthentication

use of org.projectnessie.client.http.HttpAuthentication in project nessie by projectnessie.

the class TestBearerAuthenticationProvider method checkAuth.

void checkAuth(NessieAuthentication authentication) {
    assertThat(authentication).isInstanceOf(HttpAuthentication.class);
    HttpAuthentication httpAuthentication = (HttpAuthentication) authentication;
    // Intercept the call to HttpClient.register(RequestFilter) and extract the RequestFilter for
    // our test
    RequestFilter[] authFilter = new RequestFilter[1];
    HttpClient.Builder client = Mockito.mock(HttpClient.Builder.class);
    Mockito.doAnswer(invocationOnMock -> {
        Object[] args = invocationOnMock.getArguments();
        if (args.length == 1 && args[0] instanceof RequestFilter) {
            authFilter[0] = (RequestFilter) args[0];
        }
        return null;
    }).when(client).addRequestFilter(Mockito.any());
    httpAuthentication.applyToHttpClient(client);
    // Check that the registered RequestFilter works as expected (sets the right HTTP header)
    assertThat(authFilter[0]).isInstanceOf(RequestFilter.class);
    HttpHeaders headers = new HttpHeaders();
    RequestContext context = new RequestContext(headers, null, null, null);
    authFilter[0].filter(context);
    assertThat(headers.asMap()).containsKey("Authorization").extracting("Authorization", InstanceOfAssertFactories.iterable(String.class)).containsExactly("Bearer token123");
}
Also used : HttpHeaders(org.projectnessie.client.http.HttpHeaders) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) HttpClient(org.projectnessie.client.http.HttpClient) RequestContext(org.projectnessie.client.http.RequestContext) RequestFilter(org.projectnessie.client.http.RequestFilter)

Example 3 with HttpAuthentication

use of org.projectnessie.client.http.HttpAuthentication in project nessie by projectnessie.

the class TestJerseyRestNaiveClientInMemory method init.

@Override
protected void init(NessieApiV1 api, @Nullable HttpClient.Builder httpClient, URI uri) {
    assumeThat(httpClient).isNotNull();
    // Intentionally remove the `Accept` header from requests.
    // Service endpoints should declare the content type for their return values,
    // which should allow the Web Container to properly format output even in the absence
    // of `Accept` HTTP headers.
    RequestFilter noAcceptFilter = context -> context.removeHeader(HEADER_ACCEPT);
    httpClient.addRequestFilter(noAcceptFilter);
    api = HttpClientBuilder.builder().withAuthentication((HttpAuthentication) client -> client.addRequestFilter(noAcceptFilter)).withUri(httpClient.getBaseUri()).build(NessieApiV1.class);
    super.init(api, httpClient, uri);
}
Also used : HttpClientBuilder(org.projectnessie.client.http.HttpClientBuilder) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) InmemoryDatabaseAdapterFactory(org.projectnessie.versioned.persist.inmem.InmemoryDatabaseAdapterFactory) NessieDbAdapterName(org.projectnessie.versioned.persist.tests.extension.NessieDbAdapterName) InmemoryTestConnectionProviderSource(org.projectnessie.versioned.persist.inmem.InmemoryTestConnectionProviderSource) NessieExternalDatabase(org.projectnessie.versioned.persist.tests.extension.NessieExternalDatabase) NessieApiV1(org.projectnessie.client.api.NessieApiV1) RequestFilter(org.projectnessie.client.http.RequestFilter) HEADER_ACCEPT(org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT) URI(java.net.URI) Nullable(javax.annotation.Nullable) HttpClient(org.projectnessie.client.http.HttpClient) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) RequestFilter(org.projectnessie.client.http.RequestFilter) NessieApiV1(org.projectnessie.client.api.NessieApiV1)

Example 4 with HttpAuthentication

use of org.projectnessie.client.http.HttpAuthentication in project nessie by projectnessie.

the class TestBasicAuthProvider method checkAuth.

void checkAuth(NessieAuthentication authentication) {
    assertThat(authentication).isInstanceOf(HttpAuthentication.class);
    HttpAuthentication httpAuthentication = (HttpAuthentication) authentication;
    // Intercept the call to HttpClient.register(RequestFilter) and extract the RequestFilter for
    // our test
    RequestFilter[] authFilter = new RequestFilter[1];
    HttpClient.Builder client = Mockito.mock(HttpClient.Builder.class);
    Mockito.doAnswer(invocationOnMock -> {
        Object[] args = invocationOnMock.getArguments();
        if (args.length == 1 && args[0] instanceof RequestFilter) {
            authFilter[0] = (RequestFilter) args[0];
        }
        return null;
    }).when(client).addRequestFilter(Mockito.any());
    httpAuthentication.applyToHttpClient(client);
    // Check that the registered RequestFilter works as expected (sets the right HTTP header)
    assertThat(authFilter[0]).isInstanceOf(RequestFilter.class);
    HttpHeaders headers = new HttpHeaders();
    RequestContext context = new RequestContext(headers, null, null, null);
    authFilter[0].filter(context);
    assertThat(headers.asMap()).containsKey("Authorization").extracting("Authorization", InstanceOfAssertFactories.iterable(String.class)).containsExactly("Basic QWxhZGRpbjpPcGVuU2VzYW1l");
}
Also used : HttpHeaders(org.projectnessie.client.http.HttpHeaders) HttpAuthentication(org.projectnessie.client.http.HttpAuthentication) HttpClient(org.projectnessie.client.http.HttpClient) RequestContext(org.projectnessie.client.http.RequestContext) RequestFilter(org.projectnessie.client.http.RequestFilter)

Aggregations

HttpAuthentication (org.projectnessie.client.http.HttpAuthentication)4 HttpClient (org.projectnessie.client.http.HttpClient)4 RequestFilter (org.projectnessie.client.http.RequestFilter)4 HttpHeaders (org.projectnessie.client.http.HttpHeaders)3 RequestContext (org.projectnessie.client.http.RequestContext)3 URI (java.net.URI)1 Nullable (javax.annotation.Nullable)1 Assumptions.assumeThat (org.assertj.core.api.Assumptions.assumeThat)1 NessieApiV1 (org.projectnessie.client.api.NessieApiV1)1 HttpClientBuilder (org.projectnessie.client.http.HttpClientBuilder)1 HEADER_ACCEPT (org.projectnessie.client.http.HttpUtils.HEADER_ACCEPT)1 InmemoryDatabaseAdapterFactory (org.projectnessie.versioned.persist.inmem.InmemoryDatabaseAdapterFactory)1 InmemoryTestConnectionProviderSource (org.projectnessie.versioned.persist.inmem.InmemoryTestConnectionProviderSource)1 NessieDbAdapterName (org.projectnessie.versioned.persist.tests.extension.NessieDbAdapterName)1 NessieExternalDatabase (org.projectnessie.versioned.persist.tests.extension.NessieExternalDatabase)1