@Test public void basicAuthInputStreamTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePost(getTargetUrl()) .setBody(new ByteArrayInputStream("test".getBytes())) .setRealm(basicAuthRealm(USER, ADMIN).build()) .execute(); Response resp = f.get(30, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), "test"); } }
@Test public void digestAuthNegativeTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet("http://localhost:" + port1 + "/") .setRealm(digestAuthRealm("fake", ADMIN).build()) .execute(); Response resp = f.get(20, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), 401); } }
@Test public void digestAuthTestWithoutScheme() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet("http://localhost:" + port1 + "/") .setRealm(digestAuthRealm(USER, ADMIN).setRealmName("MyRealm").build()) .execute(); Response resp = f.get(60, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertNotNull(resp.getHeader("X-Auth")); } }
@Test public void testStrongDigest() throws Exception { String user = "user"; String pass = "pass"; String realm = "realm"; String nonce = "nonce"; String method = "GET"; Uri uri = Uri.create("http://ahc.io/foo"); String qop = "auth"; Realm orig = digestAuthRealm(user, pass) .setNonce(nonce) .setUri(uri) .setMethodName(method) .setRealmName(realm) .setQop(qop) .build(); String nc = orig.getNc(); String cnonce = orig.getCnonce(); String ha1 = getMd5(user + ":" + realm + ":" + pass); String ha2 = getMd5(method + ":" + uri.getPath()); String expectedResponse = getMd5(ha1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + ha2); assertEquals(orig.getResponse(), expectedResponse); }
private void testOldDigest(String qop) throws Exception { String user = "user"; String pass = "pass"; String realm = "realm"; String nonce = "nonce"; String method = "GET"; Uri uri = Uri.create("http://ahc.io/foo"); Realm orig = digestAuthRealm(user, pass) .setNonce(nonce) .setUri(uri) .setMethodName(method) .setRealmName(realm) .setQop(qop) .build(); String ha1 = getMd5(user + ":" + realm + ":" + pass); String ha2 = getMd5(method + ":" + uri.getPath()); String expectedResponse = getMd5(ha1 + ":" + nonce + ":" + ha2); assertEquals(orig.getResponse(), expectedResponse); }
public SimpleAsyncHttpClient build() { if (realmBuilder != null) { configBuilder.setRealm(realmBuilder.build()); } if (proxyHost != null) { Realm realm = null; if (proxyPrincipal != null) { AuthScheme proxyAuthScheme = withDefault(this.proxyAuthScheme, AuthScheme.BASIC); realm = realm(proxyAuthScheme, proxyPrincipal, proxyPassword).build(); } configBuilder.setProxyServer(proxyServer(proxyHost, proxyPort).setRealm(realm).build()); } configBuilder.addIOExceptionFilter(new ResumableIOExceptionFilter()); SimpleAsyncHttpClient sc = new SimpleAsyncHttpClient(configBuilder.build(), requestBuilder, defaultThrowableHandler, errorDocumentBehaviour, enableResumableDownload, ahc, listener); return sc; } }
protected Future<Response> execute(AsyncHttpClient client, boolean basic, boolean preemptive) { Realm.Builder realm; String url; if (basic) { realm = basicAuthRealm(USER, ADMIN); url = getTargetUrl(); } else { realm = digestAuthRealm(USER, ADMIN); url = getTargetUrl2(); if (preemptive) { realm.setRealmName("MyRealm"); realm.setAlgorithm("MD5"); realm.setQop("auth"); realm.setNonce("fFDVc60re9zt8fFDvht0tNrYuvqrcchN"); } } return client.prepareGet(url).setRealm(realm.setUsePreemptiveAuth(preemptive).build()).execute(); }
@Test public void testClone() { Realm orig = basicAuthRealm("user", "pass").setCharset(UTF_16) .setUsePreemptiveAuth(true) .setRealmName("realm") .setAlgorithm("algo").build(); Realm clone = realm(orig).build(); assertEquals(clone.getPrincipal(), orig.getPrincipal()); assertEquals(clone.getPassword(), orig.getPassword()); assertEquals(clone.getCharset(), orig.getCharset()); assertEquals(clone.isUsePreemptiveAuth(), orig.isUsePreemptiveAuth()); assertEquals(clone.getRealmName(), orig.getRealmName()); assertEquals(clone.getAlgorithm(), orig.getAlgorithm()); assertEquals(clone.getScheme(), orig.getScheme()); }
@Test public void basicAuthFileTest() throws Exception { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.preparePost(getTargetUrl()) .setBody(SIMPLE_TEXT_FILE) .setRealm(basicAuthRealm(USER, ADMIN).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@Test public void basicAuthFileNoKeepAliveTest() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(false))) { Future<Response> f = client.preparePost(getTargetUrl()) .setBody(SIMPLE_TEXT_FILE) .setRealm(basicAuthRealm(USER, ADMIN).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING); } }
@Test public void basicAuthTestPreemtiveTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { // send the request to the no-auth endpoint to be able to verify the // auth header is really sent preemptively for the initial call. Future<Response> f = client.prepareGet(getTargetUrlNoAuth()) .setRealm(basicAuthRealm(USER, ADMIN).setUsePreemptiveAuth(true).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); } }
public Builder setRealm(Realm.Builder realmBuilder) { this.realm = realmBuilder.build(); return this; }
public Builder setRealm(Realm.Builder realm) { this.realm = realm.build(); return this; }
public T setRealm(Realm.Builder realm) { this.realm = realm.build(); return asDerivedType(); }
@Test public void noneAuthTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { BoundRequestBuilder r = client.prepareGet(getTargetUrl()).setRealm(basicAuthRealm(USER, ADMIN).build()); Future<Response> f = r.execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); } }
@Test public void basicAuthNegativeTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet(getTargetUrl()) .setRealm(basicAuthRealm("fake", ADMIN).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), 401); } }
@Test public void basicAuthTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet(getTargetUrl()) .setRealm(basicAuthRealm(USER, ADMIN).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); } }
@Test public void digestAuthTest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet("http://localhost:" + port1 + "/") .setRealm(digestAuthRealm(USER, ADMIN).setRealmName("MyRealm").build()) .execute(); Response resp = f.get(60, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertNotNull(resp.getHeader("X-Auth")); } }
@Test public void redirectAndBasicAuthTest() throws Exception { try (AsyncHttpClient client = asyncHttpClient(config().setFollowRedirect(true).setMaxRedirects(10))) { Future<Response> f = client.prepareGet(getTargetUrl2()) .setRealm(basicAuthRealm(USER, ADMIN).build()) .execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertNotNull(resp); assertNotNull(resp.getHeader("X-Auth")); } }
private ProxyServer ntlmProxy() { Realm realm = ntlmAuthRealm("Zaphod", "Beeblebrox") .setNtlmDomain("Ursa-Minor") .setNtlmHost("LightCity") .build(); return proxyServer("localhost", port2).setRealm(realm).build(); }