Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
giosil committed Jul 23, 2021
1 parent 33804c8 commit 782034b
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 31 deletions.
7 changes: 6 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
5 changes: 5 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
10 changes: 10 additions & 0 deletions .settings/org.eclipse.wst.common.component
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="saml-idp">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<property name="context-root" value="saml-idp"/>
<property name="java-output-path" value="/saml-idp/target/classes"/>
</wb-module>
</project-modules>
7 changes: 7 additions & 0 deletions .settings/org.eclipse.wst.common.project.facet.core.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.web" version="3.1"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.wst.validation.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
disabled=06target
eclipse.preferences.version=1
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/lastpass/saml/IdPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ private Certificate certFromString(String b64data)
/** Certificate used to validate assertions */
private Certificate cert;

/** Host to replace in login URL and logout URL*/
private String host;

/**
* Set the Idp Entity Id.
*/
Expand Down Expand Up @@ -240,6 +243,9 @@ public void setLoginUrl(String loginUrl)
*/
public String getLoginUrl()
{
if(host != null && host.length() > 0) {
return SAMLUtils.replaceHost(loginUrl, host);
}
return this.loginUrl;
}

Expand Down Expand Up @@ -277,6 +283,25 @@ public void setLogoutUrl(String logoutUrl)
*/
public String getLogoutUrl()
{
if(host != null && host.length() > 0) {
return SAMLUtils.replaceHost(loginUrl, host);
}
return this.logoutUrl;
}

/**
* Set the replace host of login URL and logout URL.
*/
public void setHost(String host)
{
this.host = host;
}

/**
* Get the replace host of login URL and logout URL.
*/
public String getHost()
{
return this.host;
}
}
23 changes: 17 additions & 6 deletions src/main/java/com/lastpass/saml/SAMLClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ public static SAMLClient getInstance()
return _instance;
}

// [dew]
public static SAMLClient getInstance(String host)
throws Exception
{
if(_instance != null) return _instance;
SAMLInit.initialize();
IdPConfig _idpConfig = new IdPConfig(Thread.currentThread().getContextClassLoader().getResourceAsStream("idp-metadata.xml"));
SPConfig _spConfig = new SPConfig(Thread.currentThread().getContextClassLoader().getResourceAsStream("sp-metadata.xml"));
_idpConfig.setHost(host);
_spConfig.setHost(host);
_instance = new SAMLClient(_spConfig, _idpConfig);
return _instance;
}

/**
* Get the configured IdpConfig.
*
Expand Down Expand Up @@ -224,10 +238,8 @@ private Assertion decrypt(EncryptedAssertion encrypted)
throw new DecryptionException("Encrypted assertion found but no SP key available");
BasicCredential cred = new BasicCredential();
cred.setPrivateKey(spConfig.getPrivateKey());
StaticKeyInfoCredentialResolver resolver =
new StaticKeyInfoCredentialResolver(cred);
Decrypter decrypter =
new Decrypter(null, resolver, new InlineEncryptedKeyResolver());
StaticKeyInfoCredentialResolver resolver = new StaticKeyInfoCredentialResolver(cred);
Decrypter decrypter = new Decrypter(null, resolver, new InlineEncryptedKeyResolver());
decrypter.setRootInNewDocument(true);

return decrypter.decrypt(encrypted);
Expand Down Expand Up @@ -261,8 +273,7 @@ private void validate(Response response)
// response must be successful
if (response.getStatus() == null ||
response.getStatus().getStatusCode() == null ||
!(StatusCode.SUCCESS_URI
.equals(response.getStatus().getStatusCode().getValue()))) {
!(StatusCode.SUCCESS_URI.equals(response.getStatus().getStatusCode().getValue()))) {
throw new ValidationException(
"Response has an unsuccessful status code");
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/lastpass/saml/SAMLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,24 @@ else if(iMill < 100) {
}
return sYear + "-" + sMonth + "-" + sDay + "T" + sHour + ":" + sMin + ":" + sSec + "Z";
}

public static
String replaceHost(String url, String host) {
if(host == null || host.length() == 0) {
return url;
}
int sepCtx = url.indexOf('/', 8);
if(sepCtx < 0) return url;
int sepCtxHost = host.indexOf('/', 8);
if(sepCtxHost > 0) {
host = host.substring(0, sepCtxHost);
}
if(host.startsWith("http://") || host.startsWith("https://")) {
return host + url.substring(sepCtx);
}
if(url.startsWith("https://")) {
return "https://" + host + url.substring(sepCtx);
}
return "http://" + host + url.substring(sepCtx);
}
}
43 changes: 33 additions & 10 deletions src/main/java/com/lastpass/saml/SPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class SPConfig
/** Private key used for decrypting assertions */
private PrivateKey privateKey;

/** Host to replace in ACS URL*/
private String host;

/**
* Construct a new, empty SPConfig.
*/
Expand All @@ -62,8 +65,7 @@ public SPConfig()
*
* @param metadataFile File where the metadata lives
*
* @throws SAMLException if an error condition occurs while trying to parse and process
* the metadata
* @throws SAMLException if an error condition occurs while trying to parse and process the metadata
*/
public SPConfig(File metadataFile)
throws SAMLException
Expand Down Expand Up @@ -93,8 +95,7 @@ public SPConfig(File metadataFile)
*
* @param inputStream An input stream containing a metadata XML document
*
* @throws SAMLException if an error condition occurs while trying to parse and process
* the metadata
* @throws SAMLException if an error condition occurs while trying to parse and process the metadata
*/
public SPConfig(InputStream inputStream)
throws SAMLException
Expand All @@ -114,8 +115,7 @@ private void init(InputStream inputStream)
Document doc = parsers.parse(inputStream);
Element root = doc.getDocumentElement();

UnmarshallerFactory unmarshallerFactory =
Configuration.getUnmarshallerFactory();
UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();

edesc = (EntityDescriptor) unmarshallerFactory
.getUnmarshaller(root)
Expand All @@ -129,8 +129,7 @@ private void init(InputStream inputStream)
}

// fetch sp information
SPSSODescriptor spDesc = edesc.getSPSSODescriptor(
"urn:oasis:names:tc:SAML:2.0:protocol");
SPSSODescriptor spDesc = edesc.getSPSSODescriptor("urn:oasis:names:tc:SAML:2.0:protocol");

if (spDesc == null)
throw new SAMLException("No SP SSO descriptor found");
Expand Down Expand Up @@ -165,12 +164,16 @@ public void setEntityId(String entityId)
*/
public String getEntityId()
{
if(host != null && host.length() > 0) {
if(entityId != null && entityId.startsWith("http")) {
return SAMLUtils.replaceHost(entityId, host);
}
}
return this.entityId;
}

/**
* Set the SP ACS URL. Auth responses are posted
* here.
* Set the SP ACS URL. Auth responses are posted here.
*/
public void setAcs(String acs)
{
Expand All @@ -182,6 +185,9 @@ public void setAcs(String acs)
*/
public String getAcs()
{
if(host != null && host.length() > 0) {
return SAMLUtils.replaceHost(acs, host);
}
return this.acs;
}

Expand All @@ -200,4 +206,21 @@ public PrivateKey getPrivateKey()
{
return this.privateKey;
}


/**
* Set the replace host of ACS URL.
*/
public void setHost(String host)
{
this.host = host;
}

/**
* Get the replace host of ACS URL.
*/
public String getHost()
{
return this.host;
}
}
13 changes: 9 additions & 4 deletions src/main/java/org/dew/saml/web/WebLogin.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.dew.saml.web;

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.opensaml.saml2.core.AuthnRequest;

import com.lastpass.saml.SAMLIdP;

import javax.servlet.*;

public
class WebLogin extends HttpServlet
{
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/dew/saml/web/WebSLO.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.dew.saml.web;

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.opensaml.saml2.core.LogoutRequest;

import com.lastpass.saml.SAMLIdP;

import javax.servlet.*;

public
class WebSLO extends HttpServlet
{
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/dew/saml/web/WebSSO.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.dew.saml.web;

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.opensaml.saml2.core.AuthnRequest;

import com.lastpass.saml.SAMLIdP;

import javax.servlet.*;

public
class WebSSO extends HttpServlet
{
Expand Down

0 comments on commit 782034b

Please sign in to comment.