Search in sources :

Example 6 with ProxyAuthenticator

use of org.littleshoot.proxy.ProxyAuthenticator in project java-cloudant by cloudant.

the class HttpProxyTest method setupAndStartProxy.

/**
 * Starts a littleproxy instance that will proxy the requests. Applies appropriate configuration
 * options to the proxy based on the test parameters.
 *
 * @throws Exception
 */
@BeforeEach
public void setupAndStartProxy(final boolean okUsable, final boolean useSecureProxy, final boolean useHttpsServer, final boolean useProxyAuth) throws Exception {
    HttpProxyServerBootstrap proxyBoostrap = DefaultHttpProxyServer.bootstrap().withAllowLocalOnly(// only run on localhost
    true).withAuthenticateSslClients(// we aren't checking client certs
    false);
    if (useProxyAuth) {
        // check the proxy user and password
        ProxyAuthenticator pa = new ProxyAuthenticator() {

            @Override
            public boolean authenticate(String userName, String password) {
                return (mockProxyUser.equals(userName) && mockProxyPass.equals(password));
            }

            @Override
            public String getRealm() {
                return null;
            }
        };
        proxyBoostrap.withProxyAuthenticator(pa);
    }
    if (useSecureProxy) {
        proxyBoostrap.withSslEngineSource(new SslEngineSource() {

            @Override
            public SSLEngine newSslEngine() {
                return MockWebServerResources.getSSLContext().createSSLEngine();
            }

            @Override
            public SSLEngine newSslEngine(String peerHost, int peerPort) {
                return MockWebServerResources.getSSLContext().createSSLEngine(peerHost, peerPort);
            }
        });
    }
    // Start the proxy server
    proxy = proxyBoostrap.start();
}
Also used : ProxyAuthenticator(org.littleshoot.proxy.ProxyAuthenticator) SslEngineSource(org.littleshoot.proxy.SslEngineSource) SSLEngine(javax.net.ssl.SSLEngine) HttpProxyServerBootstrap(org.littleshoot.proxy.HttpProxyServerBootstrap) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with ProxyAuthenticator

use of org.littleshoot.proxy.ProxyAuthenticator in project jackrabbit by apache.

the class ConnectionTest method testObtainViaProxyWithValidCredentials.

public void testObtainViaProxyWithValidCredentials() throws URISyntaxException, RepositoryException {
    ProxyAuthenticator authenticator = new ProxyAuthenticator() {

        @Override
        public String getRealm() {
            return null;
        }

        @Override
        public boolean authenticate(String userName, String password) {
            if (userName.equals("test") && password.equals("valid")) {
                return true;
            }
            return false;
        }
    };
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(// use arbitrary port
    0).withProxyAuthenticator(authenticator).start();
    try {
        Map<String, String> parameters = new HashMap<>();
        ConnectionOptions.Builder builder = ConnectionOptions.builder();
        builder.proxyHost("127.0.0.1");
        builder.proxyPort(proxyServer.getListenAddress().getPort());
        builder.proxyUsername("test");
        builder.proxyPassword("valid");
        parameters.putAll(builder.build().toServiceFactoryParameters());
        RepositoryService repositoryService = createRepositoryService(false, parameters);
        repositoryService.obtain(new SimpleCredentials("admin", "admin".toCharArray()), null);
    } finally {
        proxyServer.stop();
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) ProxyAuthenticator(org.littleshoot.proxy.ProxyAuthenticator) HashMap(java.util.HashMap) RepositoryService(org.apache.jackrabbit.spi.RepositoryService)

Aggregations

ProxyAuthenticator (org.littleshoot.proxy.ProxyAuthenticator)7 HttpProxyServerBootstrap (org.littleshoot.proxy.HttpProxyServerBootstrap)3 HashMap (java.util.HashMap)2 SimpleCredentials (javax.jcr.SimpleCredentials)2 RepositoryService (org.apache.jackrabbit.spi.RepositoryService)2 HttpProxyServer (org.littleshoot.proxy.HttpProxyServer)2 DefaultHttpProxyServer (org.littleshoot.proxy.impl.DefaultHttpProxyServer)2 ServerSocket (java.net.ServerSocket)1 RepositoryException (javax.jcr.RepositoryException)1 SSLEngine (javax.net.ssl.SSLEngine)1 BeforeClass (org.junit.BeforeClass)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 SslEngineSource (org.littleshoot.proxy.SslEngineSource)1 ThreadPoolConfiguration (org.littleshoot.proxy.impl.ThreadPoolConfiguration)1