Skip to content

Instantly share code, notes, and snippets.

@ldaley
Created February 18, 2016 20:08
Show Gist options
  • Save ldaley/ad103477882af9c0851d to your computer and use it in GitHub Desktop.
Save ldaley/ad103477882af9c0851d to your computer and use it in GitHub Desktop.
Ratpack authing proxy
package com.gradle.receipts.test.browser.util
import ratpack.groovy.test.embed.GroovyEmbeddedApp
import ratpack.http.client.HttpClient
import ratpack.test.ApplicationUnderTest
import ratpack.test.CloseableApplicationUnderTest
final class AuthingProxy {
private AuthingProxy() {
}
static CloseableApplicationUnderTest admin(ApplicationUnderTest app) {
to(app, "admin", "admin")
}
static CloseableApplicationUnderTest to(ApplicationUnderTest app, String username, String password) {
GroovyEmbeddedApp.of {
handlers {
all { HttpClient httpClient ->
def targetUri = new URI(app.address.toASCIIString() + request.rawUri.replaceFirst("/", ""))
httpClient.requestStream(targetUri) {
it.headers.copy(request.headers)
it.basicAuth(username, password)
it.redirects(0)
} then {
it.forwardTo(response)
}
}
}
}
}
}
@pledbrook
Copy link

Thanks, looks useful. It's not quite what I was after as I need the app to send a 407 challenge. This looks like it passes on any authentication through to the target URL. I guess it probably wouldn't be too hard to add the 407 handling. I just need a cheap way to store dummy user credentials.

@pledbrook
Copy link

For reference, I was after something like this:

    server = GroovyEmbeddedApp.of {
        handlers {
            all { HttpClient httpClient ->
                // Is there a Proxy-Authorization header?
                final authRequestHeader = request.headers.get("Proxy-Authorization")
                if (authRequestHeader) {
                    // If yes, are the credentials correct?
                    def (username, password) = new String(
                            (authRequestHeader - "Basic ").decodeBase64(),
                            Charset.forName("UTF-8")).split(/:/)

                    if (username != "dummy" || password != "password") {
                        response.headers.add("Proxy-Authenticate", 'Basic realm="test.proxy"')
                        response.status(407).send()
                    } else {
                        next()
                    }
                }

                // If there is no header, send a challenge.
                response.headers.add("Proxy-Authenticate", 'Basic realm="test.proxy"')
                response.status(407).send()
            }

            all { HttpClient httpClient ->
                def targetUri = new URI(request.rawUri)

                httpClient.requestStream(targetUri) {
                    it.headers.copy(request.headers)
                    it.redirects(0)
                } then {
                    it.forwardTo(response)
                }
            }
        }
    }.server

    server.start()
    initProxy(new InetSocketAddress(server.bindHost ?: "localhost", server.bindPort))

Unfortunately, I think I now need to work out how to run the proxy over SSL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment