-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from tcplugins/webhook-authentication
Webhook authentication
- Loading branch information
Showing
27 changed files
with
761 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
48 changes: 48 additions & 0 deletions
48
tcwebhooks-core/src/main/java/webhook/teamcity/auth/UsernamePasswordAuthenticator.java
This file contains 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
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; | ||
|
||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
...bhooks-core/src/main/java/webhook/teamcity/auth/UsernamePasswordAuthenticatorFactory.java
This file contains 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
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(); | ||
} | ||
|
||
} | ||
|
10 changes: 10 additions & 0 deletions
10
tcwebhooks-core/src/main/java/webhook/teamcity/auth/WebHookAuthConfig.java
This file contains 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
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>(); | ||
} |
10 changes: 10 additions & 0 deletions
10
tcwebhooks-core/src/main/java/webhook/teamcity/auth/WebHookAuthenticator.java
This file contains 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
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); | ||
} |
8 changes: 8 additions & 0 deletions
8
tcwebhooks-core/src/main/java/webhook/teamcity/auth/WebHookAuthenticatorFactory.java
This file contains 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
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(); | ||
} |
42 changes: 42 additions & 0 deletions
42
tcwebhooks-core/src/main/java/webhook/teamcity/auth/WebHookAuthenticatorProvider.java
This file contains 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
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(); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.