Skip to content

Commit

Permalink
Merge pull request #21 from tcplugins/webhook-authentication
Browse files Browse the repository at this point in the history
Webhook authentication
  • Loading branch information
netwolfuk committed Mar 22, 2016
2 parents 8b6a33e + 5c006ca commit 1490b30
Show file tree
Hide file tree
Showing 27 changed files with 761 additions and 93 deletions.
16 changes: 16 additions & 0 deletions tcwebhooks-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@
<scope>provided</scope>
</dependency>

<!--Jetty dependencies start here -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.11.v20150529</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.2.11.v20150529</version>
<scope>test</scope>
</dependency>
<!--Jetty dependencies end here -->

<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions tcwebhooks-core/src/main/java/webhook/WebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.apache.commons.httpclient.NameValuePair;

import webhook.teamcity.BuildState;
import webhook.teamcity.auth.WebHookAuthConfig;
import webhook.teamcity.auth.WebHookAuthenticator;

public interface WebHook {

Expand Down Expand Up @@ -84,6 +86,8 @@ public interface WebHook {

public abstract void setCharset(String charset);

public abstract void setAuthentication(WebHookAuthenticator authenticator);



}
12 changes: 12 additions & 0 deletions tcwebhooks-core/src/main/java/webhook/WebHookImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.apache.commons.httpclient.methods.StringRequestEntity;

import webhook.teamcity.BuildState;
import webhook.teamcity.auth.WebHookAuthConfig;
import webhook.teamcity.auth.WebHookAuthenticator;


public class WebHookImpl implements WebHook {
Expand All @@ -38,6 +40,7 @@ public class WebHookImpl implements WebHook {
private String errorReason = "";
private List<NameValuePair> params;
private BuildState states;
private WebHookAuthenticator authenticator;

public WebHookImpl(){
this.client = new HttpClient();
Expand Down Expand Up @@ -122,6 +125,10 @@ public void post() throws FileNotFoundException, IOException{
NameValuePair[] paramsArray = this.params.toArray(new NameValuePair[this.params.size()]);
httppost.setRequestBody(paramsArray);
}
if(authenticator != null){
authenticator.addAuthentication(httppost, client, url);
}

try {
client.executeMethod(httppost);
this.resultCode = httppost.getStatusCode();
Expand Down Expand Up @@ -308,4 +315,9 @@ public BuildState getBuildStates() {
public void setBuildStates(BuildState states) {
this.states = states;
}

@Override
public void setAuthentication(WebHookAuthenticator authenticator) {
this.authenticator = authenticator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.jetbrains.annotations.Nullable;

import webhook.WebHook;
import webhook.teamcity.auth.WebHookAuthenticator;
import webhook.teamcity.auth.WebHookAuthenticatorProvider;
import webhook.teamcity.payload.WebHookPayload;
import webhook.teamcity.payload.WebHookPayloadManager;
import webhook.teamcity.settings.WebHookConfig;
Expand All @@ -49,17 +51,19 @@ public class WebHookListener extends BuildServerAdapter {
private final WebHookMainSettings myMainSettings;
private final WebHookPayloadManager myManager;
private final WebHookFactory webHookFactory;
private final WebHookAuthenticatorProvider webHookAuthenticatorProvider;


public WebHookListener(SBuildServer sBuildServer, ProjectSettingsManager settings,
WebHookMainSettings configSettings, WebHookPayloadManager manager,
WebHookFactory factory) {
WebHookFactory factory, WebHookAuthenticatorProvider webHookAuthenticationProvider) {

myBuildServer = sBuildServer;
mySettings = settings;
myMainSettings = configSettings;
myManager = manager;
webHookFactory = factory;
webHookAuthenticatorProvider = webHookAuthenticationProvider;

Loggers.SERVER.info("WebHookListener :: Starting");
}
Expand All @@ -74,6 +78,11 @@ public void getFromConfig(WebHook webHook, WebHookConfig webHookConfig){
webHook.setEnabled(webHookConfig.getEnabled());
//webHook.addParams(webHookConfig.getParams());
webHook.setBuildStates(webHookConfig.getBuildStates());
if (webHookConfig.getAuthenticationConfig() != null){
WebHookAuthenticator auth = webHookAuthenticatorProvider.getAuthenticator(webHookConfig.getAuthenticationConfig().type);
auth.setWebHookAuthConfig(webHookConfig.getAuthenticationConfig());
webHook.setAuthentication(auth);
}
webHook.setProxy(myMainSettings.getProxyConfigForUrl(webHookConfig.getUrl()));
Loggers.ACTIVITIES.debug("WebHookListener :: Webhook proxy set to "
+ webHook.getProxyHost() + " for " + webHookConfig.getUrl());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package webhook.teamcity.auth;

import java.net.URI;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jetbrains.annotations.NotNull;

public class UsernamePasswordAuthenticator implements WebHookAuthenticator {

private static final String REALM = "realm";
private static final String PASSWORD = "password";
private static final String USERNAME = "username";
WebHookAuthenticatorProvider myProvider;
WebHookAuthConfig config;


@Override
public void addAuthentication(PostMethod method, HttpClient client, String url) {
if (config.parameters.containsKey(USERNAME) && config.parameters.containsKey(PASSWORD)){
URI uri = URI.create(url);
AuthScope scope;
if (config.parameters.containsKey(REALM)){
scope = new AuthScope(uri.getHost(), uri.getPort(), config.parameters.get(REALM));
} else {
scope = new AuthScope(uri.getHost(), uri.getPort());
}
Credentials creds = new UsernamePasswordCredentials(config.parameters.get(USERNAME), config.parameters.get(PASSWORD));
client.getState().setCredentials(scope, creds);
client.getParams().setAuthenticationPreemptive(config.preemptive);
}
}

@Override @NotNull
public WebHookAuthConfig getWebHookAuthConfig() {
return config;
}

@Override
public void setWebHookAuthConfig(WebHookAuthConfig authenticationConfig) {
this.config = authenticationConfig;

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package webhook.teamcity.auth;


public class UsernamePasswordAuthenticatorFactory implements WebHookAuthenticatorFactory{

WebHookAuthenticatorProvider myProvider;
WebHookAuthConfig config;

public UsernamePasswordAuthenticatorFactory(WebHookAuthenticatorProvider provider) {
myProvider = provider;
}

@Override
public void register(){
myProvider.registerAuthType(this);
}

@Override
public String getName() {
return "userpass";
}

@Override
public WebHookAuthenticator getAuthenticatorInstance() {
return new UsernamePasswordAuthenticator();
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package webhook.teamcity.auth;

import java.util.Map;
import java.util.TreeMap;

public class WebHookAuthConfig {
public String type = "";
public Boolean preemptive = true;
public Map<String, String> parameters = new TreeMap<String, String>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package webhook.teamcity.auth;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

public interface WebHookAuthenticator {
public WebHookAuthConfig getWebHookAuthConfig();
public void addAuthentication (PostMethod httppost, HttpClient client, String url);
public void setWebHookAuthConfig(WebHookAuthConfig authenticationConfig);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package webhook.teamcity.auth;


public interface WebHookAuthenticatorFactory {
public String getName();
public void register();
public WebHookAuthenticator getAuthenticatorInstance();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package webhook.teamcity.auth;

import java.util.HashMap;
import java.util.Set;

import webhook.teamcity.Loggers;

public class WebHookAuthenticatorProvider {

HashMap<String, WebHookAuthenticatorFactory> types = new HashMap<String,WebHookAuthenticatorFactory>();

public WebHookAuthenticatorProvider(){
Loggers.SERVER.info("WebHookAuthenticatorProvider :: Starting");
}

public void registerAuthType(WebHookAuthenticatorFactory authType){
Loggers.SERVER.info(this.getClass().getSimpleName() + " :: Registering authentication type "
+ authType.getName());
types.put(authType.getName(),authType);
Loggers.SERVER.debug(this.getClass().getSimpleName() + " :: Authenticator list is " + this.types.size() + " items long.");
for (String auth : this.types.keySet()){
Loggers.SERVER.debug(this.getClass().getSimpleName() + " :: Authenticator Name: " + auth);
}
}

public WebHookAuthenticator getAuthenticator(String typeName){
if (types.containsKey(typeName)){
return types.get(typeName).getAuthenticatorInstance();
}
return null;
}

public Boolean isRegisteredType(String type){
return types.containsKey(type);
}

public Set<String> getRegisteredTypes(){
return types.keySet();
}


}
Loading

0 comments on commit 1490b30

Please sign in to comment.