AuthService extends SimpleDecoratingHttpService { public static AuthServiceBuilder builder() { return new AuthServiceBuilder(); } AuthService(HttpService delegate, Authorizer<HttpRequest> authorizer,
                AuthSuccessHandler successHandler, AuthFailureHandler failureHandler); static Function<? super HttpService, AuthService> newDecorator(
            Iterable<? extends Authorizer<HttpRequest>> authorizers); @SafeVarargs static Function<? super HttpService, AuthService> newDecorator(Authorizer<HttpRequest>... authorizers); static AuthServiceBuilder builder(); @Override HttpResponse serve(ServiceRequestContext ctx, HttpRequest req);  }
@Test void testBasicAuth() throws Exception { final WebClient webClient = WebClient.builder(server.httpUri()) .auth(BasicToken.of("brown", "cony")) .build(); assertThat(webClient.get("/basic").aggregate().join().status()).isEqualTo(HttpStatus.OK); try (CloseableHttpClient hc = HttpClients.createMinimal()) { try (CloseableHttpResponse res = hc.execute( basicGetRequest("/basic", BasicToken.of("pangyo", "choco"), AUTHORIZATION))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); } try (CloseableHttpResponse res = hc.execute( basicGetRequest("/basic-custom", BasicToken.of("brown", "cony"), CUSTOM_TOKEN_HEADER))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); } try (CloseableHttpResponse res = hc.execute(new HttpGet(server.httpUri() + "/basic"))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 401 Unauthorized"); } try (CloseableHttpResponse res = hc.execute( basicGetRequest("/basic", BasicToken.of("choco", "pangyo"), AUTHORIZATION))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 401 Unauthorized"); } } }
@Test void testOAuth1a() throws Exception { final OAuth1aToken passToken = OAuth1aToken.builder() .realm("dummy_realm") .consumerKey("dummy_consumer_key@#$!") .token("dummy_oauth1a_token") .signatureMethod("dummy") .signature("dummy_signature") .timestamp("0") .nonce("dummy_nonce") .version("1.0") .build(); final WebClient webClient = WebClient.builder(server.httpUri()) .auth(passToken) .build(); assertThat(webClient.get("/oauth1a").aggregate().join().status()).isEqualTo(HttpStatus.OK); try (CloseableHttpClient hc = HttpClients.createMinimal()) { try (CloseableHttpResponse res = hc.execute( oauth1aGetRequest("/oauth1a-custom", passToken, CUSTOM_TOKEN_HEADER))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); } final OAuth1aToken failToken = OAuth1aToken.builder() .realm("dummy_realm") .consumerKey("dummy_consumer_key@#$!") .token("dummy_oauth1a_token") .signatureMethod("dummy") .signature("DUMMY_signature") .timestamp("0") .nonce("dummy_nonce") .version("1.0") .build(); try (CloseableHttpResponse res = hc.execute( oauth1aGetRequest("/oauth1a", failToken, AUTHORIZATION))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 401 Unauthorized"); } } }
@Test void testOAuth2() throws Exception { final WebClient webClient = WebClient.builder(server.httpUri()) .auth(OAuth2Token.of("dummy_oauth2_token")) .build(); assertThat(webClient.get("/oauth2").aggregate().join().status()).isEqualTo(HttpStatus.OK); try (CloseableHttpClient hc = HttpClients.createMinimal()) { try (CloseableHttpResponse res = hc.execute( oauth2GetRequest("/oauth2-custom", OAuth2Token.of("dummy_oauth2_token"), CUSTOM_TOKEN_HEADER))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); } try (CloseableHttpResponse res = hc.execute( oauth2GetRequest("/oauth2", OAuth2Token.of("DUMMY_oauth2_token"), AUTHORIZATION))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 401 Unauthorized"); } } }
@Test void testCompositeAuth() throws Exception { try (CloseableHttpClient hc = HttpClients.createMinimal()) { try (CloseableHttpResponse res = hc.execute( getRequest("/composite", "unit test"))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); } try (CloseableHttpResponse res = hc.execute( basicGetRequest("/composite", BasicToken.of("brown", "cony"), AUTHORIZATION))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); } final Map<String, String> passToken = ImmutableMap.<String, String>builder() .put("realm", "dummy_realm") .put("oauth_consumer_key", "dummy_consumer_key@#$!") .put("oauth_token", "dummy_oauth1a_token") .put("oauth_signature_method", "dummy") .put("oauth_signature", "dummy_signature") .put("oauth_timestamp", "0") .put("oauth_nonce", "dummy_nonce") .put("version", "1.0") .build(); final OAuth1aToken oAuth1aToken = OAuth1aToken.builder().putAll(passToken).build(); try (CloseableHttpResponse res = hc.execute( oauth1aGetRequest("/composite", oAuth1aToken, AUTHORIZATION))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); } try (CloseableHttpResponse res = hc.execute( oauth2GetRequest("/composite", OAuth2Token.of("dummy_oauth2_token"), AUTHORIZATION))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK"); } try (CloseableHttpResponse res = hc.execute(new HttpGet(server.httpUri() + "/composite"))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 401 Unauthorized"); } try (CloseableHttpResponse res = hc.execute( basicGetRequest("/composite", BasicToken.of("choco", "pangyo"), AUTHORIZATION))) { assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 401 Unauthorized"); } } }