Created
February 3, 2013 23:45
-
-
Save kastork/4704249 to your computer and use it in GitHub Desktop.
Basic example of a CAS login and logout route in a Vertx Route Matcher application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.vertx.groovy.core.http.RouteMatcher | |
import org.vertx.groovy.core.http.HttpClient | |
import org.vertx.groovy.core.http.HttpClientRequest | |
import org.vertx.groovy.core.http.HttpServerResponse | |
import org.vertx.groovy.core.http.HttpClientResponse | |
// made with with vertx 1.3.0 | |
// set your own CAS server details, | |
// | |
// Caution, this code requires the CAS server be on | |
// port 443, but it trusts all SSL certs | |
// when performing the cas ticket verification | |
casHost = "cas.example.com" | |
casPath = "mycasgroup" | |
def routes = new RouteMatcher() | |
def redirect(req, path) { | |
req.response.with { | |
statusCode = 302 | |
headers["Location"] = path | |
end() | |
} | |
} | |
routes.get("/not_authorized") { req -> | |
req.response.end "Not Authorized" | |
} | |
routes.noMatch{ req -> | |
req.response.end "Nothing matched" | |
} | |
routes.get("cas_fail"){ req -> | |
req.response.end "Could't complete the CAS transaction." | |
} | |
routes.get("/loggedin") { req -> | |
req.response.end "Logged in." | |
} | |
routes.get("/logout") { req -> | |
redirect(req, "https://${casHost}/${casPath}/logout") | |
} | |
routes.get("/login") { req -> | |
hostAddr = req.headers.host | |
serviceURL = URLEncoder.encode("http://${hostAddr}/login") | |
if (null == req.params.ticket) { | |
req.response.with { | |
statusCode = 302 | |
headers["Location"] = "https://${casHost}/${casPath}/login?service=${serviceURL}" | |
end() | |
} | |
return | |
} | |
casClient = vertx.createHttpClient( | |
port: 443, | |
host: casHost, | |
keepAlive: false, | |
SSL: true, | |
trustAll: true) | |
HttpClientRequest request = | |
casClient.getNow("/${casPath}/validate?ticket=${req.params.ticket}&service=${serviceURL}") { resp -> | |
if (resp.statusCode != 200) { | |
redirect(req, "/cas_fail") | |
} | |
resp.bodyHandler { body -> | |
bodyParts = body.toString().split('\n') | |
if (bodyParts[0].equalsIgnoreCase("no")) { | |
redirect(req, "/not_authorized") | |
} else if (bodyParts[0].equalsIgnoreCase("yes")) { | |
def loggedInAs = bodyParts[1] | |
println loggedInAs | |
// set up a session or something... | |
// redirect to get rid of the used CAS | |
// ticket query param (if you stay here, | |
// browser reloads will fail) | |
redirect(req, "/loggedin") | |
return | |
} | |
} | |
} | |
} | |
def httpServer = vertx.createHttpServer() | |
httpServer.requestHandler(routes.asClosure()) | |
httpServer.listen(9000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment