Skip to content

Instantly share code, notes, and snippets.

@harniman
Last active June 13, 2016 16:42
Show Gist options
  • Save harniman/36a004ddd5e1c0635edd to your computer and use it in GitHub Desktop.
Save harniman/36a004ddd5e1c0635edd to your computer and use it in GitHub Desktop.
A Jenkins Workflow shared library class to perform a Form Based authentication to a Jenkins Server and retrieve the body from a URL.
package net.harniman.workflow.jenkins
//import org.apache.commons.httpclient.*
//import org.apache.commons.httpclient.auth.*
import org.apache.commons.httpclient.Header
import org.apache.commons.httpclient.HostConfiguration
import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.NameValuePair
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.cookie.CookiePolicy
import org.apache.commons.httpclient.params.HttpClientParams
class Server {
String user
String password
String host
Integer port
String proto
HttpClient client = new HttpClient()
Server( String user, String password, String host, Integer port, String proto) {
this.user = user
this.password=password
this.host=host
this.port=port
this.proto=proto
client.getHostConfiguration().setHost(host, port, proto);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
}
HttpClient getHttpClient() {
return client
}
Integer logon() {
String logonIniationURL = proto + "://" + host + ":" + port + "/login?from=%2F"
String logonSubmitURL = proto + "://" + host + ":" + port + "/j_acegi_security_check"
// We need to make a call first to set up the session
// HttpClient will automatically handle cookies based on the CookiePolicy set above
GetMethod get = new GetMethod(logonIniationURL)
client.executeMethod(get);
get.releaseConnection()
PostMethod authpost = new PostMethod(logonSubmitURL)
def json='{"j_username": "'+user+'", "j_password": "'+password+'", "remember_me": false, "from": "/"}'
def param1=new NameValuePair("j_username", user)
def param2=new NameValuePair("j_password", password)
def param3=new NameValuePair("from", "/")
def param4=new NameValuePair("json", json)
def param5=new NameValuePair("Submit", "log in")
authpost.addParameters(param1)
authpost.addParameters(param2)
authpost.addParameters(param3)
authpost.addParameters(param4)
authpost.addParameters(param5)
client.executeMethod(authpost)
// We need to follow the redirect to understand whether authentication
// was successful
// 200 = Success
// 401 = Credentials failure
Header header = authpost.getResponseHeader("location");
def response
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
GetMethod redirect = new GetMethod(newuri);
client.executeMethod(redirect);
response=redirect.getStatusCode()
redirect.releaseConnection()
}
authpost.releaseConnection()
return response
}
String getURL(String URL) {
GetMethod get = new GetMethod(URL)
client.executeMethod(get)
def body = get.getResponseBodyAsString()
get.releaseConnection()
return body
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment