Skip to content

Commit

Permalink
Refactored SAML SP persistence and in-memory tracking.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dario authored and fhanik committed Feb 17, 2016
1 parent a277ed6 commit ba77fd1
Show file tree
Hide file tree
Showing 19 changed files with 482 additions and 1,070 deletions.
Expand Up @@ -109,12 +109,6 @@ public SamlServiceProviderDefinition getConfig() {


public SamlServiceProvider setConfig(SamlServiceProviderDefinition config) { public SamlServiceProvider setConfig(SamlServiceProviderDefinition config) {


if (StringUtils.hasText(getEntityId())) {
config.setSpEntityId(getEntityId());
}
if (StringUtils.hasText(getIdentityZoneId())) {
config.setZoneId(getIdentityZoneId());
}
this.config = config; this.config = config;
return this; return this;
} }
Expand All @@ -125,9 +119,6 @@ public String getEntityId() {


public SamlServiceProvider setEntityId(String entityId) { public SamlServiceProvider setEntityId(String entityId) {
this.entityId = entityId; this.entityId = entityId;
if (config != null) {
config.setSpEntityId(entityId);
}
return this; return this;
} }


Expand All @@ -146,9 +137,6 @@ public String getIdentityZoneId() {


public SamlServiceProvider setIdentityZoneId(String identityZoneId) { public SamlServiceProvider setIdentityZoneId(String identityZoneId) {
this.identityZoneId = identityZoneId; this.identityZoneId = identityZoneId;
if (config != null) {
config.setZoneId(identityZoneId);
}
return this; return this;
} }


Expand Down
Expand Up @@ -12,23 +12,21 @@
*******************************************************************************/ *******************************************************************************/
package org.cloudfoundry.identity.uaa.provider.saml.idp; package org.cloudfoundry.identity.uaa.provider.saml.idp;


import com.fasterxml.jackson.annotation.JsonIgnore;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Objects;


public class SamlServiceProviderDefinition { import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.fasterxml.jackson.annotation.JsonIgnore;


public static final String DEFAULT_HTTP_SOCKET_FACTORY = "org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory"; public class SamlServiceProviderDefinition {
public static final String DEFAULT_HTTPS_SOCKET_FACTORY = "org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory";


public enum MetadataLocation { public enum MetadataLocation {
URL, URL,
Expand All @@ -37,36 +35,27 @@ public enum MetadataLocation {
} }


private String metaDataLocation; private String metaDataLocation;
private String spEntityId;
private String zoneId;
private String nameID; private String nameID;
private int singleSignOnServiceIndex; private int singleSignOnServiceIndex;
private boolean metadataTrustCheck; private boolean metadataTrustCheck;
private String socketFactoryClassName;


public SamlServiceProviderDefinition clone() { public SamlServiceProviderDefinition clone() {
return new SamlServiceProviderDefinition(metaDataLocation, return new SamlServiceProviderDefinition(metaDataLocation,
spEntityId,
nameID, nameID,
singleSignOnServiceIndex, singleSignOnServiceIndex,
metadataTrustCheck, metadataTrustCheck);
zoneId);
} }


public SamlServiceProviderDefinition() {} public SamlServiceProviderDefinition() {}


public SamlServiceProviderDefinition(String metaDataLocation, public SamlServiceProviderDefinition(String metaDataLocation,
String spEntityAlias,
String nameID, String nameID,
int singleSignOnServiceIndex, int singleSignOnServiceIndex,
boolean metadataTrustCheck, boolean metadataTrustCheck) {
String zoneId) {
this.metaDataLocation = metaDataLocation; this.metaDataLocation = metaDataLocation;
this.spEntityId = spEntityAlias;
this.nameID = nameID; this.nameID = nameID;
this.singleSignOnServiceIndex = singleSignOnServiceIndex; this.singleSignOnServiceIndex = singleSignOnServiceIndex;
this.metadataTrustCheck = metadataTrustCheck; this.metadataTrustCheck = metadataTrustCheck;
this.zoneId = zoneId;
} }


@JsonIgnore @JsonIgnore
Expand Down Expand Up @@ -119,14 +108,6 @@ public void setMetaDataLocation(String metaDataLocation) {
this.metaDataLocation = metaDataLocation; this.metaDataLocation = metaDataLocation;
} }


public String getSpEntityId() {
return spEntityId;
}

public void setSpEntityId(String spEntityId) {
this.spEntityId = spEntityId;
}

public String getNameID() { public String getNameID() {
return nameID; return nameID;
} }
Expand All @@ -151,88 +132,59 @@ public void setMetadataTrustCheck(boolean metadataTrustCheck) {
this.metadataTrustCheck = metadataTrustCheck; this.metadataTrustCheck = metadataTrustCheck;
} }


public String getSocketFactoryClassName() {
if (socketFactoryClassName!=null && socketFactoryClassName.trim().length()>0) {
return socketFactoryClassName;
}
if (getMetaDataLocation()==null || getMetaDataLocation().trim().length()==0) {
throw new IllegalStateException("Invalid meta data URL[" + getMetaDataLocation() + "] cannot determine socket factory.");
}
if (getMetaDataLocation().startsWith("https")) {
return DEFAULT_HTTPS_SOCKET_FACTORY;
} else {
return DEFAULT_HTTP_SOCKET_FACTORY;
}
}

public void setSocketFactoryClassName(String socketFactoryClassName) {
this.socketFactoryClassName = socketFactoryClassName;
if (socketFactoryClassName!=null && socketFactoryClassName.trim().length()>0) {
try {
Class.forName(
socketFactoryClassName,
true,
Thread.currentThread().getContextClassLoader()
);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
} catch (ClassCastException e) {
throw new IllegalArgumentException(e);
}
}
}

public String getZoneId() {
return zoneId;
}

public void setZoneId(String zoneId) {
this.zoneId = zoneId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SamlServiceProviderDefinition that = (SamlServiceProviderDefinition) o;

return Objects.equals(getUniqueAlias(), that.getUniqueAlias());
}

@Override @Override
public int hashCode() { public int hashCode() {
String alias = getUniqueAlias(); final int prime = 31;
return alias==null ? 0 : alias.hashCode(); int result = 1;
result = prime * result + ((metaDataLocation == null) ? 0 : metaDataLocation.hashCode());
result = prime * result + (metadataTrustCheck ? 1231 : 1237);
result = prime * result + ((nameID == null) ? 0 : nameID.hashCode());
result = prime * result + singleSignOnServiceIndex;
return result;
} }


@JsonIgnore @Override
protected String getUniqueAlias() { public boolean equals(Object obj) {
return getSpEntityId()+"###"+getZoneId(); if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SamlServiceProviderDefinition other = (SamlServiceProviderDefinition) obj;
if (metaDataLocation == null) {
if (other.metaDataLocation != null)
return false;
} else if (!metaDataLocation.equals(other.metaDataLocation))
return false;
if (metadataTrustCheck != other.metadataTrustCheck)
return false;
if (nameID == null) {
if (other.nameID != null)
return false;
} else if (!nameID.equals(other.nameID))
return false;
if (singleSignOnServiceIndex != other.singleSignOnServiceIndex)
return false;
return true;
} }


@Override @Override
public String toString() { public String toString() {
return "SamlServiceProviderDefinition{" + return "SamlServiceProviderDefinition{" +
"spEntityAlias='" + spEntityId + '\'' +
", metaDataLocation='" + metaDataLocation + '\'' + ", metaDataLocation='" + metaDataLocation + '\'' +
", nameID='" + nameID + '\'' + ", nameID='" + nameID + '\'' +
", singleSignOnServiceIndex=" + singleSignOnServiceIndex + ", singleSignOnServiceIndex=" + singleSignOnServiceIndex +
", metadataTrustCheck=" + metadataTrustCheck + ", metadataTrustCheck=" + metadataTrustCheck +
", socketFactoryClassName='" + socketFactoryClassName + '\'' +
", zoneId='" + zoneId + '\'' +
'}'; '}';
} }


public static class Builder { public static class Builder {


private String metaDataLocation; private String metaDataLocation;
private String spEntityId;
private String zoneId;
private String nameID; private String nameID;
private int singleSignOnServiceIndex; private int singleSignOnServiceIndex;
private boolean metadataTrustCheck; private boolean metadataTrustCheck;
private String socketFactoryClassName;


private Builder(){} private Builder(){}


Expand All @@ -242,14 +194,10 @@ public static Builder get() {


public SamlServiceProviderDefinition build() { public SamlServiceProviderDefinition build() {
SamlServiceProviderDefinition def = new SamlServiceProviderDefinition(); SamlServiceProviderDefinition def = new SamlServiceProviderDefinition();

def.setMetaDataLocation(metaDataLocation); def.setMetaDataLocation(metaDataLocation);
def.setSpEntityId(spEntityId);
def.setZoneId(zoneId);
def.setNameID(nameID); def.setNameID(nameID);
def.setSingleSignOnServiceIndex(singleSignOnServiceIndex); def.setSingleSignOnServiceIndex(singleSignOnServiceIndex);
def.setMetadataTrustCheck(metadataTrustCheck); def.setMetadataTrustCheck(metadataTrustCheck);
def.setSocketFactoryClassName(socketFactoryClassName);
return def; return def;
} }


Expand All @@ -258,16 +206,6 @@ public Builder setMetaDataLocation(String metaDataLocation) {
return this; return this;
} }


public Builder setSpEntityId(String spEntityId) {
this.spEntityId = spEntityId;
return this;
}

public Builder setZoneId(String zoneId) {
this.zoneId = zoneId;
return this;
}

public Builder setNameID(String nameID) { public Builder setNameID(String nameID) {
this.nameID = nameID; this.nameID = nameID;
return this; return this;
Expand All @@ -282,10 +220,5 @@ public Builder setMetadataTrustCheck(boolean metadataTrustCheck) {
this.metadataTrustCheck = metadataTrustCheck; this.metadataTrustCheck = metadataTrustCheck;
return this; return this;
} }

public Builder setSocketFactoryClassName(String socketFactoryClassName) {
this.socketFactoryClassName = socketFactoryClassName;
return this;
}
} }
} }
Expand Up @@ -27,10 +27,8 @@
import org.cloudfoundry.identity.uaa.authentication.manager.LdapLoginAuthenticationManager; import org.cloudfoundry.identity.uaa.authentication.manager.LdapLoginAuthenticationManager;
import org.cloudfoundry.identity.uaa.provider.saml.idp.SamlServiceProvider; import org.cloudfoundry.identity.uaa.provider.saml.idp.SamlServiceProvider;
import org.cloudfoundry.identity.uaa.provider.saml.idp.SamlServiceProviderConfigurator; import org.cloudfoundry.identity.uaa.provider.saml.idp.SamlServiceProviderConfigurator;
import org.cloudfoundry.identity.uaa.provider.saml.idp.SamlServiceProviderDefinition;
import org.cloudfoundry.identity.uaa.provider.saml.idp.SamlServiceProviderProvisioning; import org.cloudfoundry.identity.uaa.provider.saml.idp.SamlServiceProviderProvisioning;
import org.cloudfoundry.identity.uaa.util.JsonUtils; import org.cloudfoundry.identity.uaa.util.JsonUtils;
import org.cloudfoundry.identity.uaa.util.ObjectUtils;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder; import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.opensaml.saml2.metadata.provider.MetadataProviderException; import org.opensaml.saml2.metadata.provider.MetadataProviderException;
import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.EmptyResultDataAccessException;
Expand Down Expand Up @@ -66,12 +64,7 @@ public ResponseEntity<SamlServiceProvider> createServiceProvider(@RequestBody Sa
String zoneId = IdentityZoneHolder.get().getId(); String zoneId = IdentityZoneHolder.get().getId();
body.setIdentityZoneId(zoneId); body.setIdentityZoneId(zoneId);


SamlServiceProviderDefinition definition = ObjectUtils.castInstance(body.getConfig(), samlConfigurator.addSamlServiceProvider(body);
SamlServiceProviderDefinition.class);
definition.setZoneId(zoneId);
definition.setSpEntityId(body.getEntityId());
samlConfigurator.addSamlServiceProviderDefinition(definition);
body.setConfig(definition);


SamlServiceProvider createdSp = serviceProviderProvisioning.create(body); SamlServiceProvider createdSp = serviceProviderProvisioning.create(body);
return new ResponseEntity<>(createdSp, HttpStatus.CREATED); return new ResponseEntity<>(createdSp, HttpStatus.CREATED);
Expand All @@ -88,12 +81,8 @@ public ResponseEntity<SamlServiceProvider> updateServiceProvider(@PathVariable S
return new ResponseEntity<>(UNPROCESSABLE_ENTITY); return new ResponseEntity<>(UNPROCESSABLE_ENTITY);
} }
body.setEntityId(existing.getEntityId()); body.setEntityId(existing.getEntityId());
SamlServiceProviderDefinition definition = ObjectUtils.castInstance(body.getConfig(),
SamlServiceProviderDefinition.class); samlConfigurator.addSamlServiceProvider(body);
definition.setZoneId(zoneId);
definition.setSpEntityId(body.getEntityId());
samlConfigurator.addSamlServiceProviderDefinition(definition);
body.setConfig(definition);


SamlServiceProvider updatedSp = serviceProviderProvisioning.update(body); SamlServiceProvider updatedSp = serviceProviderProvisioning.update(body);
return new ResponseEntity<>(updatedSp, OK); return new ResponseEntity<>(updatedSp, OK);
Expand Down

0 comments on commit ba77fd1

Please sign in to comment.