diff --git a/org.wso2.carbon.component/src/main/java/org/wso2/carbon/sts/resources/AbstractResource.java b/org.wso2.carbon.component/src/main/java/org/wso2/carbon/sts/resources/AbstractResource.java index 4ccb108..f0ef532 100644 --- a/org.wso2.carbon.component/src/main/java/org/wso2/carbon/sts/resources/AbstractResource.java +++ b/org.wso2.carbon.component/src/main/java/org/wso2/carbon/sts/resources/AbstractResource.java @@ -1,7 +1,9 @@ package org.wso2.carbon.sts.resources; import java.io.ByteArrayInputStream; +import java.io.UnsupportedEncodingException; +import javax.ws.rs.core.Context; import javax.xml.stream.XMLStreamReader; import org.apache.cxf.binding.soap.SoapMessage; @@ -9,15 +11,20 @@ import org.apache.cxf.message.ExchangeImpl; import org.apache.cxf.message.MessageImpl; import org.apache.cxf.staxutils.StaxUtils; +import org.wso2.msf4j.Request; public abstract class AbstractResource { - - private SoapMessage setUpMessage(byte[] msg) throws Exception { - SoapMessage message = new SoapMessage(new MessageImpl()); - Exchange ex = new ExchangeImpl(); - ex.setInMessage(message); - message.setContent(XMLStreamReader.class, StaxUtils.createXMLStreamReader(new ByteArrayInputStream(msg))); - return message; - } + + protected SoapMessage setUpMessage(byte[] msg) throws Exception { + SoapMessage message = new SoapMessage(new MessageImpl()); + Exchange ex = new ExchangeImpl(); + ex.setInMessage(message); + message.setContent(XMLStreamReader.class, + StaxUtils.createXMLStreamReader(new ByteArrayInputStream(msg))); + return message; + } + + public abstract void processRequest(@Context Request request) + throws UnsupportedEncodingException; } diff --git a/org.wso2.carbon.component/src/main/java/org/wso2/carbon/sts/resources/STSResource.java b/org.wso2.carbon.component/src/main/java/org/wso2/carbon/sts/resources/STSResource.java index 143bc70..8395429 100644 --- a/org.wso2.carbon.component/src/main/java/org/wso2/carbon/sts/resources/STSResource.java +++ b/org.wso2.carbon.component/src/main/java/org/wso2/carbon/sts/resources/STSResource.java @@ -1,6 +1,13 @@ package org.wso2.carbon.sts.resources; +import io.swagger.annotations.Api; +import io.swagger.annotations.Info; +import io.swagger.annotations.License; +import io.swagger.annotations.SwaggerDefinition; + import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.POST; @@ -8,15 +15,24 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; +import org.apache.cxf.binding.soap.SoapMessage; import org.osgi.service.component.annotations.Component; import org.wso2.msf4j.Microservice; import org.wso2.msf4j.Request; +import org.wso2.msf4j.util.BufferUtil; @Component( name = "org.wso2.carbon.sts.resources.STSResource", service = Microservice.class, immediate = true ) +@Api(value = "scim/v2/ServiceProviderConfig") +@SwaggerDefinition( + info = @Info( + title = "/SecurityTokenService Endpoint Swagger Definition", version = "1.0", + description = "STS /SecurityTokenService endpoint", + license = @License(name = "Apache 2.0", url = "http://www.apache.org/licenses/LICENSE-2.0")) +) @Path("/services") public class STSResource extends AbstractResource { @@ -24,7 +40,15 @@ public class STSResource extends AbstractResource { @POST @Consumes(MediaType.TEXT_XML) public void processRequest(@Context Request request) throws UnsupportedEncodingException{ + List fullMessageBody = request.getFullMessageBody(); + ByteBuffer buffer = BufferUtil.merge(fullMessageBody); + SoapMessage soap = null; + try { + soap = setUpMessage(buffer.array()); + } catch (Exception e) { + e.printStackTrace(); + } } } diff --git a/org.wso2.carbon.sts.policy.mgt/.classpath b/org.wso2.carbon.sts.policy.mgt/.classpath new file mode 100644 index 0000000..f619a53 --- /dev/null +++ b/org.wso2.carbon.sts.policy.mgt/.classpath @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.wso2.carbon.sts.policy.mgt/pom.xml b/org.wso2.carbon.sts.policy.mgt/pom.xml new file mode 100644 index 0000000..f887fa2 --- /dev/null +++ b/org.wso2.carbon.sts.policy.mgt/pom.xml @@ -0,0 +1,51 @@ + + + + + + org.wso2.carbon.sts + identity-inbound-auth-sts + 1.0.0 + + 4.0.0 + org.wso2.carbon.sts.policy.mgt + bundle + WSO2 Carbon - Sample Carbon Component + + + + + org.apache.cxf + cxf-rt-ws-policy + 3.1.11 + + + org.wso2.msf4j + msf4j-core + 2.1.0 + + + + org.apache.cxf + cxf-core + 3.1.11 + + + + + org.wso2.carbon.sts.policy.mgt.internal + !org.wso2.carbon.sts.policy.mgt.internal, + org.wso2.carbon.sts.policy.mgt.*;version="5.1.1" + org.osgi.framework.*;version="[1.8.0, 2.0.0)", + org.wso2.carbon.kernel;version="[5.0.0, 6.0.0)" + + + diff --git a/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/SecurityPolicyManager.java b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/SecurityPolicyManager.java new file mode 100644 index 0000000..9250205 --- /dev/null +++ b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/SecurityPolicyManager.java @@ -0,0 +1,48 @@ +package org.wso2.carbon.sts.policy.mgt; + +import java.io.FileNotFoundException; +import java.io.FileReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.neethi.PolicyBuilder; +import org.apache.neethi.Policy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.wso2.carbon.sts.policy.mgt.internal.DataHolder; +import org.wso2.carbon.sts.policy.util.PolicyConstants; + +public class SecurityPolicyManager implements SecurityPolicyProvider { + + private static final Logger logger = LoggerFactory + .getLogger(DataHolder.class); + + private Policy effectivePolicy; + + @Override + public void updateEffectivePolicy(int scenario) { + + XMLInputFactory factory = XMLInputFactory.newInstance(); + XMLStreamReader streamReader = null; + try { + streamReader = factory.createXMLStreamReader(new FileReader( + PolicyConstants.POLICY_REPOSITORY_PATH + Integer.toString(scenario) + + ".xml")); + } catch (FileNotFoundException | XMLStreamException e) { + logger.error("Policy File Not Found"); + } + PolicyBuilder builder = new PolicyBuilder(); + setEffectivePolicy(builder.getPolicy(streamReader)); + } + + public Policy getEffectivePolicy() { + return effectivePolicy; + } + + public void setEffectivePolicy(Policy effectivePolicy) { + this.effectivePolicy = effectivePolicy; + } + +} diff --git a/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/SecurityPolicyProvider.java b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/SecurityPolicyProvider.java new file mode 100644 index 0000000..4a05442 --- /dev/null +++ b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/SecurityPolicyProvider.java @@ -0,0 +1,6 @@ +package org.wso2.carbon.sts.policy.mgt; + +public interface SecurityPolicyProvider { + + public void updateEffectivePolicy(int scenario); +} diff --git a/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/internal/DataHolder.java b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/internal/DataHolder.java new file mode 100644 index 0000000..a96b8c7 --- /dev/null +++ b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/internal/DataHolder.java @@ -0,0 +1,23 @@ +package org.wso2.carbon.sts.policy.mgt.internal; + +public class DataHolder { + private int policyScenario; + + private static DataHolder instance = new DataHolder(); + + private DataHolder() { + + } + + public static DataHolder getInstance() { + return instance; + } + + public int getPolicyScenario() { + return policyScenario; + } + + public void setPolicyScenario(int i) { + policyScenario = i; + } +} diff --git a/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/internal/ServiceComponent.java b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/internal/ServiceComponent.java new file mode 100644 index 0000000..ea29277 --- /dev/null +++ b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/mgt/internal/ServiceComponent.java @@ -0,0 +1,37 @@ +package org.wso2.carbon.sts.policy.mgt.internal; + +import java.util.logging.Logger; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.wso2.carbon.sts.policy.mgt.SecurityPolicyManager; +import org.wso2.carbon.sts.policy.mgt.SecurityPolicyProvider; + +@Component(name = "org.wso2.carbon.sts.policy.mgt.internal.ServiceComponent", immediate = true) +public class ServiceComponent { + + Logger logger = Logger.getLogger(ServiceComponent.class.getName()); + private ServiceRegistration serviceRegistration; + + @Activate + protected void start(BundleContext bundleContext) throws Exception { + logger.info("Policy Management Component is activated"); + + //Replace this with appropriate policy retrieval method + DataHolder.getInstance().setPolicyScenario(1); + + serviceRegistration = bundleContext.registerService( + SecurityPolicyProvider.class.getName(), + new SecurityPolicyManager(), null); + } + + @Deactivate + protected void stop() throws Exception { + logger.info("Service Component is deactivated"); + + serviceRegistration.unregister(); + } +} diff --git a/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/util/PolicyConstants.java b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/util/PolicyConstants.java new file mode 100644 index 0000000..66cd42e --- /dev/null +++ b/org.wso2.carbon.sts.policy.mgt/src/main/java/org/wso2/carbon/sts/policy/util/PolicyConstants.java @@ -0,0 +1,7 @@ +package org.wso2.carbon.sts.policy.util; + +public class PolicyConstants { + + //Need to replace with policy repository path + public static String POLICY_REPOSITORY_PATH ; +} diff --git a/pom.xml b/pom.xml index ed28440..b5c6112 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ org.wso2.carbon.sts.claim.mgt org.wso2.carbon.sts.token.provider org.wso2.carbon.sts2 + org.wso2.carbon.sts.policy.mgt