@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); } }
@Test public void preemptiveNTLMAuthTest() throws IOException, InterruptedException, ExecutionException { ntlmAuthTest(realmBuilderBase().setUsePreemptiveAuth(true)); }
@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()); }
private synchronized AsyncHttpClient getAsyncHttpClient(Message message) { if (ahcclient == null) { DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder(); AuthorizationPolicy ap = getEffectiveAuthPolicy(message); if (ap != null && (!StringUtils.isEmpty(ap.getAuthorizationType()) || !StringUtils.isEmpty(ap.getUserName()))) { Realm.Builder rb = new Realm.Builder(ap.getUserName(), ap.getPassword()); if (ap.getAuthorizationType() == null) { rb.setScheme(AuthScheme.BASIC); } else { rb.setScheme(AuthScheme.valueOf(ap.getAuthorizationType().toUpperCase())); } rb.setUsePreemptiveAuth(true); builder.setRealm(rb.build()); } AsyncHttpClientConfig config = builder.build(); ahcclient = new DefaultAsyncHttpClient(config); } return ahcclient; }
private synchronized AsyncHttpClient getAsyncHttpClient(Message message) { if (ahcclient == null) { DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder(); AuthorizationPolicy ap = getEffectiveAuthPolicy(message); if (ap != null && (!StringUtils.isEmpty(ap.getAuthorizationType()) || !StringUtils.isEmpty(ap.getUserName()))) { Realm.Builder rb = new Realm.Builder(ap.getUserName(), ap.getPassword()); if (ap.getAuthorizationType() == null) { rb.setScheme(AuthScheme.BASIC); } else { rb.setScheme(AuthScheme.valueOf(ap.getAuthorizationType().toUpperCase())); } rb.setUsePreemptiveAuth(true); builder.setRealm(rb.build()); } AsyncHttpClientConfig config = builder.build(); ahcclient = new DefaultAsyncHttpClient(config); } return ahcclient; }
private Zendesk(AsyncHttpClient client, String url, String username, String password) { this.logger = LoggerFactory.getLogger(Zendesk.class); this.closeClient = client == null; this.oauthToken = null; this.client = client == null ? new DefaultAsyncHttpClient() : client; this.url = url.endsWith("/") ? url + "api/v2" : url + "/api/v2"; if (username != null) { this.realm = new Realm.Builder(username, password) .setScheme(Realm.AuthScheme.BASIC) .setUsePreemptiveAuth(true) .build(); } else { if (password != null) { throw new IllegalStateException("Cannot specify token or password without specifying username"); } this.realm = null; } this.mapper = createMapper(); }
private void addAuth(BoundRequestBuilder requestBuilder) { if (credentials != null && credentials.getLogin() != null) { String password = Optional.ofNullable(credentials.getPassword()).orElse(""); Realm realm = new Realm.Builder(credentials.getLogin(), password).setUsePreemptiveAuth(true) .setScheme(Realm.AuthScheme.BASIC).build(); requestBuilder.setRealm(realm); } }
Realm auth(String username, String password, WSAuthScheme scheme) { Realm.AuthScheme authScheme = Realm.AuthScheme.valueOf(scheme.name()); return (new Realm.Builder(username, password)) .setScheme(authScheme) .setUsePreemptiveAuth(true) .build(); } }
.setUsePreemptiveAuth(true) .build(); future.setProxyRealm(newBasicRealm); .setUri(request.getUri()) .setMethodName(request.getMethod()) .setUsePreemptiveAuth(true) .parseProxyAuthenticateHeader(digestHeader) .build(); .setUsePreemptiveAuth(true) .build(); future.setProxyRealm(newNtlmRealm); Realm newNtlmRealm2 = realm(proxyRealm) .setScheme(AuthScheme.NTLM) .setUsePreemptiveAuth(true) .build(); future.setProxyRealm(newNtlmRealm2);
.setUsePreemptiveAuth(true) .build(); future.setRealm(newBasicRealm); .setUri(request.getUri()) .setMethodName(request.getMethod()) .setUsePreemptiveAuth(true) .parseWWWAuthenticateHeader(digestHeader) .build(); .setUsePreemptiveAuth(true) .build(); future.setRealm(newNtlmRealm); Realm newNtlmRealm2 = realm(realm) .setScheme(AuthScheme.NTLM) .setUsePreemptiveAuth(true) .build(); future.setRealm(newNtlmRealm2);
public static Realm.Builder realm(Realm prototype) { return new Realm.Builder(prototype.getPrincipal(), prototype.getPassword()) .setRealmName(prototype.getRealmName()) .setAlgorithm(prototype.getAlgorithm()) .setNc(prototype.getNc()) .setNonce(prototype.getNonce()) .setCharset(prototype.getCharset()) .setOpaque(prototype.getOpaque()) .setQop(prototype.getQop()) .setScheme(prototype.getScheme()) .setUri(prototype.getUri()) .setUsePreemptiveAuth(prototype.isUsePreemptiveAuth()) .setNtlmDomain(prototype.getNtlmDomain()) .setNtlmHost(prototype.getNtlmHost()) .setUseAbsoluteURI(prototype.isUseAbsoluteURI()) .setOmitQuery(prototype.isOmitQuery()) .setServicePrincipalName(prototype.getServicePrincipalName()) .setUseCanonicalHostname(prototype.isUseCanonicalHostname()) .setCustomLoginConfig(prototype.getCustomLoginConfig()) .setLoginContextName(prototype.getLoginContextName()); }
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 authorizedPreemptiveRealmWorks() throws Exception { expectSuccess(rb -> rb.setRealm(basicAuthRealm(USER, ADMIN).setUsePreemptiveAuth(true))); }