From 750e2e19dddb236c49d458ff27c2ab50b1848a63 Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 26 Feb 2009 04:41:26 +0000 Subject: Adding support for enabling and configuring authentication for embedded http server using policy git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@748014 13f79535-47bb-0310-9956-ffa450edef68 --- .../modules/policy-security-http/pom.xml | 6 + .../AuthenticationConfigurationPolicy.java | 45 ++++++ ...AuthenticationConfigurationPolicyProcessor.java | 156 +++++++++++++++++++++ ...ca.contribution.processor.StAXArtifactProcessor | 3 +- 4 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 branches/sca-java-1.x/modules/policy-security-http/src/main/java/org/apache/tuscany/sca/policy/authentication/AuthenticationConfigurationPolicy.java create mode 100644 branches/sca-java-1.x/modules/policy-security-http/src/main/java/org/apache/tuscany/sca/policy/authentication/AuthenticationConfigurationPolicyProcessor.java (limited to 'branches/sca-java-1.x/modules/policy-security-http') diff --git a/branches/sca-java-1.x/modules/policy-security-http/pom.xml b/branches/sca-java-1.x/modules/policy-security-http/pom.xml index 07c5112f55..b25db4ad5c 100644 --- a/branches/sca-java-1.x/modules/policy-security-http/pom.xml +++ b/branches/sca-java-1.x/modules/policy-security-http/pom.xml @@ -35,6 +35,12 @@ 1.5-SNAPSHOT + + org.apache.tuscany.sca + tuscany-host-http + 1.5-SNAPSHOT + + org.apache.tuscany.sca tuscany-policy-security diff --git a/branches/sca-java-1.x/modules/policy-security-http/src/main/java/org/apache/tuscany/sca/policy/authentication/AuthenticationConfigurationPolicy.java b/branches/sca-java-1.x/modules/policy-security-http/src/main/java/org/apache/tuscany/sca/policy/authentication/AuthenticationConfigurationPolicy.java new file mode 100644 index 0000000000..4ab7e582ac --- /dev/null +++ b/branches/sca-java-1.x/modules/policy-security-http/src/main/java/org/apache/tuscany/sca/policy/authentication/AuthenticationConfigurationPolicy.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.authentication; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.xml.Constants; +import org.apache.tuscany.sca.host.http.UserContext; + +/** + * This policy configures authentication/authorization + * in embedded http servers + * + * @version $Rev$ $Date$ + */ +public class AuthenticationConfigurationPolicy { + public static final QName NAME = new QName(Constants.SCA10_TUSCANY_NS, "basicAuthenticationConfiguration"); + + private List users = new ArrayList(); + + public List getUsers() { + return this.users; + } + +} diff --git a/branches/sca-java-1.x/modules/policy-security-http/src/main/java/org/apache/tuscany/sca/policy/authentication/AuthenticationConfigurationPolicyProcessor.java b/branches/sca-java-1.x/modules/policy-security-http/src/main/java/org/apache/tuscany/sca/policy/authentication/AuthenticationConfigurationPolicyProcessor.java new file mode 100644 index 0000000000..77d8fa6203 --- /dev/null +++ b/branches/sca-java-1.x/modules/policy-security-http/src/main/java/org/apache/tuscany/sca/policy/authentication/AuthenticationConfigurationPolicyProcessor.java @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.authentication; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import java.util.StringTokenizer; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.assembly.xml.Constants; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.host.http.UserContext; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + +/** + * + * + * + * + * + * + * + * + * + * @version $Rev$ $Date$ + */ + +public class AuthenticationConfigurationPolicyProcessor implements StAXArtifactProcessor { + private static final QName AUTHENTICATION_CONFIGURATION_QNAME = new QName(Constants.SCA10_TUSCANY_NS, "authenticationConfiguration"); + private static final QName USER_QNAME = new QName(Constants.SCA10_TUSCANY_NS, "user"); + + private Monitor monitor; + + public AuthenticationConfigurationPolicyProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) { + this.monitor = monitor; + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "policy-security-validation-messages", Severity.ERROR, model, message, (Object[])messageParameters); + monitor.problem(problem); + } + } + + public QName getArtifactType() { + return AuthenticationConfigurationPolicy.NAME; + } + + public Class getModelType() { + return AuthenticationConfigurationPolicy.class; + } + + public AuthenticationConfigurationPolicy read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + AuthenticationConfigurationPolicy authenticationConfiguration = new AuthenticationConfigurationPolicy(); + + int event = reader.getEventType(); + QName start = reader.getName(); + QName name = null; + while (true) { + switch (event) { + case START_ELEMENT: + name = reader.getName(); + if(USER_QNAME.equals(name)) { + UserContext user = new UserContext(); + // + String username = reader.getAttributeValue(null, "username"); + if(username == null) { + error("RequiredAttributeUsernameMissing", reader); + } else { + user.setUsername(username); + } + + String password = reader.getAttributeValue(null, "password"); + if(password == null) { + error("RequiredAttributePasswordMissing", reader); + } else { + user.setPassword(password); + } + + String roles = reader.getAttributeValue(null, "roles"); + if(roles == null) { + error("RequiredAttributeRolesMissing", reader); + } else { + for (StringTokenizer tokens = new StringTokenizer(roles, ","); tokens.hasMoreTokens();) { + user.getRoles().add(tokens.nextToken()); + } + } + + authenticationConfiguration.getUsers().add(user); + } + break; + case END_ELEMENT: + if (start.equals(reader.getName())) { + if (reader.hasNext()) { + reader.next(); + } + return authenticationConfiguration; + } + + } + if (reader.hasNext()) { + event = reader.next(); + } else { + return authenticationConfiguration; + } + } + } + + public void write(AuthenticationConfigurationPolicy model, XMLStreamWriter writer) throws ContributionWriteException, XMLStreamException { + // TODO Auto-generated method stub + } + + public void resolve(AuthenticationConfigurationPolicy model, ModelResolver resolver) throws ContributionResolveException { + + } + +} diff --git a/branches/sca-java-1.x/modules/policy-security-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor b/branches/sca-java-1.x/modules/policy-security-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor index bd44ec8681..65605d69fe 100644 --- a/branches/sca-java-1.x/modules/policy-security-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor +++ b/branches/sca-java-1.x/modules/policy-security-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor @@ -16,4 +16,5 @@ # under the License. # Implementation class for the artifact processor extension -org.apache.tuscany.sca.policy.confidentiality.ConfidentialityPolicyProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.0#confidentiality,model=org.apache.tuscany.sca.policy.confidentiality.ConfidentialityPolicy \ No newline at end of file +org.apache.tuscany.sca.policy.confidentiality.ConfidentialityPolicyProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.0#confidentiality,model=org.apache.tuscany.sca.policy.confidentiality.ConfidentialityPolicy +org.apache.tuscany.sca.policy.authentication.AuthenticationConfigurationPolicyProcessor;qname=http://tuscany.apache.org/xmlns/sca/1.0#authenticationConfiguration,model=org.apache.tuscany.sca.policy.authentication.AuthenticationConfigurationPolicy -- cgit v1.2.3