Friday, December 13, 2013

How to authenticate to grails from R using RCurl

authenticate <- function(baseUrl="http://localhost:8080/GrailsApp/", user, password, verbose=F){
require(RCurl)
loginUrl = paste(baseUrl, "login/auth", sep="")
authenticateUrl = paste(baseUrl, "j_spring_security_check", sep="")
cat(paste("trying to authenticate user", user))
agent="Mozilla/5.0"
#Set RCurl pars
curl = getCurlHandle()
curlSetOpt(ssl.verifypeer=FALSE, timeout=60, cookiefile=tempfile(), cookiejar=tempfile(), useragent = agent, followlocation = TRUE, curl=curl, verbose=verbose)
#open login page
getURL(loginUrl, curl=curl)
#Post login form
postForm(authenticateUrl, .params= list(j_username=user, j_password=password), curl=curl, style="POST")
return(curl)
}
view raw Rcurl_grails.r hosted with ❤ by GitHub
The trick is to open the login page before actually trying to post the login data. I assume something is stored in the cookie, but I only know this is how it works. See also my question on stackoverflow: http://stackoverflow.com/questions/20355874/how-to-use-rcurl-to-authenticate-to-spring-security-grails-app/20370222#20370222

No comments:

Post a Comment