-
Notifications
You must be signed in to change notification settings - Fork 3
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 #8 from dcrissman/spring
Extract out environment verification logic and add optional Spring Security component
- Loading branch information
Showing
28 changed files
with
949 additions
and
683 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ www/*/src/main/webapp/gwt | |
.springBeans | ||
*.versionsBackup | ||
.idea/ | ||
dependency-reduced-pom.xml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.esbtools.auth</groupId> | ||
<artifactId>cert-ldap-login-module</artifactId> | ||
<version>1.3.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>cert-ldap-login-module-common</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-lang</groupId> | ||
<artifactId>commons-lang</artifactId> | ||
<version>2.6</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>19.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.unboundid</groupId> | ||
<artifactId>unboundid-ldapsdk</artifactId> | ||
<version>3.1.1</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
File renamed without changes.
File renamed without changes.
75 changes: 75 additions & 0 deletions
75
...ule-common/src/main/java/org/esbtools/auth/servlet/CertEnvironmentVerificationFilter.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,75 @@ | ||
package org.esbtools.auth.servlet; | ||
|
||
import java.io.IOException; | ||
import java.security.cert.X509Certificate; | ||
|
||
import javax.naming.NamingException; | ||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.esbtools.auth.util.Environment; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class CertEnvironmentVerificationFilter implements Filter { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CertEnvironmentVerificationFilter.class); | ||
|
||
private final Environment env; | ||
|
||
public CertEnvironmentVerificationFilter(String environment) { | ||
env = new Environment(environment); | ||
|
||
LOGGER.info("Cert Environment: " + ((environment == null) ? "Not Set" : environment)); | ||
} | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
//Do Nothing! | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
//Do Nothing! | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
LOGGER.debug("Attempting Environment Cert verification"); | ||
X509Certificate certChain[] = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate"); | ||
|
||
if ((null != certChain) && (certChain.length > 0)) { | ||
LOGGER.debug("Verifying environment on cert"); | ||
String dn = certChain[0].getSubjectDN().getName(); | ||
try { | ||
env.validate(dn); | ||
} | ||
catch (NamingException e) { | ||
unsuccessfulAuthentication(request, response, e); | ||
return; //end the chain | ||
} | ||
} | ||
else { | ||
LOGGER.debug("Cert not found. Skipping Environment Cert verification."); | ||
} | ||
|
||
chain.doFilter(request, response); | ||
} | ||
|
||
protected void unsuccessfulAuthentication(ServletRequest request, | ||
ServletResponse response, NamingException failed) throws IOException, ServletException { | ||
if (response instanceof HttpServletResponse) { | ||
((HttpServletResponse) response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
} | ||
|
||
response.setContentType("text/html"); | ||
response.getWriter().write("<html><head><title>Error</title></head><body>" + failed.getMessage() + "</body></html>"); | ||
response.getWriter().close(); | ||
} | ||
|
||
} |
File renamed without changes.
99 changes: 99 additions & 0 deletions
99
cert-ldap-login-module-common/src/main/java/org/esbtools/auth/util/Environment.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,99 @@ | ||
package org.esbtools.auth.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import javax.naming.NamingException; | ||
import javax.naming.directory.NoSuchAttributeException; | ||
import javax.naming.ldap.LdapName; | ||
import javax.naming.ldap.Rdn; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Environment { | ||
|
||
private final Logger LOGGER = LoggerFactory.getLogger(Environment.class); | ||
|
||
public static final String ENVIRONMENT_SEPARATOR = ","; | ||
public static final String LOCATION = "l"; | ||
public static final String OU = "ou"; | ||
|
||
private final String environment; | ||
private final String allAccessOu; | ||
|
||
public String getEnvironment() { | ||
return environment; | ||
} | ||
|
||
public String getAllAccessOu() { | ||
return allAccessOu; | ||
} | ||
|
||
public Environment(String environment) { | ||
this(environment, null); | ||
} | ||
|
||
public Environment(String environment, String allAccessOu) { | ||
this.environment = environment; | ||
this.allAccessOu = allAccessOu; | ||
} | ||
|
||
public void validate(String certificatePrincipal) throws NamingException { | ||
if (StringUtils.isBlank(getEnvironment())) { | ||
LOGGER.debug("No environment configured. Skipping Environment Cert verification."); | ||
return; | ||
} | ||
|
||
String ou = getLDAPAttribute(certificatePrincipal, OU); | ||
LOGGER.debug("OU from certificate: ", ou); | ||
String location = getLDAPAttribute(certificatePrincipal, LOCATION); | ||
LOGGER.debug("Location from certificate: ", location); | ||
|
||
if(StringUtils.isBlank(ou)) { | ||
throw new NoSuchAttributeException("No ou in dn, you may need to update your certificate: " + certificatePrincipal); | ||
} else { | ||
if(getAllAccessOu() != null && getAllAccessOu().equalsIgnoreCase(StringUtils.replace(ou, " ", ""))){ | ||
LOGGER.debug("Skipping environment validation, user ou matches {} ", getAllAccessOu()); | ||
} else { | ||
//if dn not from allAccessOu, verify the location (l) field | ||
//in the cert matches the configured environment | ||
if(StringUtils.isBlank(location)) { | ||
throw new NoSuchAttributeException("No location in dn, you may need to update your certificate: " + certificatePrincipal); | ||
} else if(!locationMatchesEnvironment(location)){ | ||
throw new NoSuchAttributeException("Invalid location from dn, expected " + getEnvironment() + " but found l=" + location); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public String getLDAPAttribute(String certificatePrincipal, String searchAttribute) throws NamingException { | ||
String searchName = new String(); | ||
LdapName name = new LdapName(certificatePrincipal); | ||
for (Rdn rdn : name.getRdns()) { | ||
if (rdn.getType().equalsIgnoreCase(searchAttribute)) { | ||
searchName = (String) rdn.getValue(); | ||
break; | ||
} | ||
} | ||
return searchName; | ||
} | ||
|
||
public boolean locationMatchesEnvironment(String location) { | ||
List<String> environments; | ||
if(getEnvironment().contains(ENVIRONMENT_SEPARATOR)) { | ||
environments = Arrays.asList(getEnvironment().split(ENVIRONMENT_SEPARATOR)); | ||
|
||
} else { | ||
environments = Arrays.asList(new String[] {getEnvironment()}); | ||
} | ||
for(String environment : environments) { | ||
if(environment.equalsIgnoreCase(location)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
Oops, something went wrong.