private PasswordAuthentication getAuthInfo() { final HttpConfig.Auth auth = requestConfig.getChainedRequest().actualAuth(); if (auth == null) { return null; } if (auth.getAuthType() == HttpConfig.AuthType.BASIC || auth.getAuthType() == HttpConfig.AuthType.DIGEST) { return new PasswordAuthentication(auth.getUser(), auth.getPassword().toCharArray()); } else { throw new UnsupportedOperationException("HttpURLConnection does not support " + auth.getAuthType() + " authentication"); } }
protected OkHttpBuilder(final HttpObjectConfig config) { super(config); this.config = new HttpConfigs.ThreadSafeHttpConfig(config.getChainedConfig()); this.clientConfig = config.getClient(); this.executor = config.getExecution().getExecutor(); final OkHttpClient.Builder builder = new OkHttpClient.Builder(); final SSLContext sslContext = config.getExecution().getSslContext(); if (sslContext != null) { builder.sslSocketFactory(sslContext.getSocketFactory()/*, (X509TrustManager) TRUST_MANAGERS[0]*/); builder.hostnameVerifier(config.getExecution().getHostnameVerifier()); } // DIGEST support - defining this here only allows DIGEST config on the HttpBuilder configuration, not for individual methods. final HttpConfig.Auth auth = config.getRequest().getAuth(); if (auth != null && auth.getAuthType() == DIGEST) { Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>(); builder.addInterceptor(new AuthenticationCacheInterceptor(authCache)); builder.authenticator(new CachingAuthenticatorDecorator( new DigestAuthenticator(new com.burgstaller.okhttp.digest.Credentials(auth.getUser(), auth.getPassword())), authCache) ); } final Consumer<Object> clientCustomizer = clientConfig.getClientCustomizer(); if (clientCustomizer != null) { clientCustomizer.accept(builder); } final ProxyInfo pinfo = config.getExecution().getProxyInfo(); if (usesProxy(pinfo)) { builder.proxy(pinfo.getProxy()); } this.client = builder.build(); }
/** * Configures the request to use BASIC authentication with the given `username` and `password`. The authentication will not be preemptive. This * method is an alias for calling: `basic(String, String, false)`. * * [source,groovy] * ---- * def http = HttpBuilder.configure { * request.uri = 'http://localhost:10101' * request.auth.basic 'admin', '$3cr3t' * } * ---- * * @param user the username * @param password the user's password */ default void basic(String user, String password) { basic(user, password, false); }
/** * Configures the request to use DIGEST authentication with the given username and password. The authentication will not be preemptive. This * method is an alias for calling: `digest(String, String, false)`. * * [source,groovy] * ---- * def http = HttpBuilder.configure { * request.uri = 'http://localhost:10101' * request.auth.digest 'admin', '$3cr3t' * } * ---- * * @param user the username * @param password the user's password */ default void digest(String user, String password) { digest(user, password, false); }
/** * Configures the request to use BASIC authentication with the given `username` and `password`. The authentication will not be preemptive. This * method is an alias for calling: `basic(String, String, false)`. * * [source,groovy] * ---- * def http = HttpBuilder.configure { * request.uri = 'http://localhost:10101' * request.auth.basic 'admin', '$3cr3t' * } * ---- * * @param user the username * @param password the user's password */ default void basic(String user, String password) { basic(user, password, false); }
/** * Configures the request to use DIGEST authentication with the given username and password. The authentication will not be preemptive. This * method is an alias for calling: `digest(String, String, false)`. * * [source,groovy] * ---- * def http = HttpBuilder.configure { * request.uri = 'http://localhost:10101' * request.auth.digest 'admin', '$3cr3t' * } * ---- * * @param user the username * @param password the user's password */ default void digest(String user, String password) { digest(user, password, false); }
/** * Configures the request to use DIGEST authentication with the given username and password. The authentication will not be preemptive. This * method is an alias for calling: `digest(String, String, false)`. * * [source,groovy] * ---- * def http = HttpBuilder.configure { * request.uri = 'http://localhost:10101' * request.auth.digest 'admin', '$3cr3t' * } * ---- * * @param user the username * @param password the user's password */ default void digest(String user, String password) { digest(user, password, false); }
/** * Configures the request to use BASIC authentication with the given `username` and `password`. The authentication will not be preemptive. This * method is an alias for calling: `basic(String, String, false)`. * * [source,groovy] * ---- * def http = HttpBuilder.configure { * request.uri = 'http://localhost:10101' * request.auth.basic 'admin', '$3cr3t' * } * ---- * * @param user the username * @param password the user's password */ default void basic(String user, String password) { basic(user, password, false); }
private HttpClientContext context(final ChainedHttpConfig requestConfig) throws URISyntaxException { final HttpClientContext c = HttpClientContext.create(); final ChainedHttpConfig.ChainedRequest cr = requestConfig.getChainedRequest(); final HttpConfig.Auth auth = cr.actualAuth(); if (auth != null) { final URI uri = requestConfig.getRequest().getUri().toURI(); if (auth.getAuthType() == HttpConfig.AuthType.BASIC) { basicAuth(c, auth, uri); } else if (auth.getAuthType() == HttpConfig.AuthType.DIGEST) { digestAuth(c, auth, uri); } } return c; }
default Auth actualAuth() { final Predicate<Auth> choose = (a) -> a != null && a.getAuthType() != null; return traverse(this, ChainedRequest::getParent, Request::getAuth, choose); }
default Auth actualAuth() { final Predicate<Auth> choose = (a) -> a != null && a.getAuthType() != null; return traverse(this, (cr) -> cr.getParent(), (cr) -> cr.getAuth(), choose); }
private HttpClientContext context(final ChainedHttpConfig requestConfig) throws URISyntaxException { final HttpClientContext c = HttpClientContext.create(); final ChainedHttpConfig.ChainedRequest cr = requestConfig.getChainedRequest(); final HttpConfig.Auth auth = cr.actualAuth(); if (auth != null) { final URI uri = requestConfig.getRequest().getUri().toURI(); if (auth.getAuthType() == HttpConfig.AuthType.BASIC) { basicAuth(c, auth, uri); } else if (auth.getAuthType() == HttpConfig.AuthType.DIGEST) { digestAuth(c, auth, uri); } } return c; }
default Auth actualAuth() { final Predicate<Auth> choose = (a) -> a != null && a.getAuthType() != null; return traverse(this, ChainedRequest::getParent, Request::getAuth, choose); }
private void basicAuth(final HttpClientContext c, final HttpConfig.Auth auth, final URI uri) { CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, //new AuthScope(uri.getHost(), port(uri)), new UsernamePasswordCredentials(auth.getUser(), auth.getPassword())); c.setCredentialsProvider(provider); }
private void basicAuth(final HttpClientContext c, final HttpConfig.Auth auth, final URI uri) { CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, //new AuthScope(uri.getHost(), port(uri)), new UsernamePasswordCredentials(auth.getUser(), auth.getPassword())); c.setCredentialsProvider(provider); }
private static void applyAuth(final Request.Builder requestBuilder, final ChainedHttpConfig chainedConfig) { final HttpConfig.Auth auth = chainedConfig.getChainedRequest().actualAuth(); if (auth != null) { switch (auth.getAuthType()) { case BASIC: requestBuilder.addHeader("Authorization", Credentials.basic(auth.getUser(), auth.getPassword())); break; case DIGEST: // supported in constructor with an interceptor } } }
private PasswordAuthentication getAuthInfo() { final HttpConfig.Auth auth = requestConfig.getChainedRequest().actualAuth(); if (auth == null) { return null; } if (auth.getAuthType() == HttpConfig.AuthType.BASIC || auth.getAuthType() == HttpConfig.AuthType.DIGEST) { return new PasswordAuthentication(auth.getUser(), auth.getPassword().toCharArray()); } else { throw new UnsupportedOperationException("HttpURLConnection does not support " + auth.getAuthType() + " authentication"); } }
private PasswordAuthentication getAuthInfo() { final HttpConfig.Auth auth = requestConfig.getChainedRequest().actualAuth(); if (auth == null) { return null; } if (auth.getAuthType() == HttpConfig.AuthType.BASIC || auth.getAuthType() == HttpConfig.AuthType.DIGEST) { return new PasswordAuthentication(auth.getUser(), auth.getPassword().toCharArray()); } else { throw new UnsupportedOperationException("HttpURLConnection does not support " + auth.getAuthType() + " authentication"); } }