| /** |
| * 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.swssf.policy.test; |
| |
| import org.swssf.policy.PolicyEnforcer; |
| import org.swssf.policy.PolicyViolationException; |
| import org.swssf.wss.ext.WSSConstants; |
| import org.swssf.wss.ext.WSSecurityContext; |
| import org.swssf.wss.ext.WSSecurityException; |
| import org.swssf.wss.impl.securityToken.UsernameSecurityToken; |
| import org.swssf.wss.securityEvent.OperationSecurityEvent; |
| import org.swssf.wss.securityEvent.SignedPartSecurityEvent; |
| import org.swssf.wss.securityEvent.UsernameTokenSecurityEvent; |
| import org.apache.xml.security.stax.ext.SecurityToken; |
| import org.apache.xml.security.stax.ext.XMLSecurityConstants; |
| import org.apache.xml.security.stax.securityEvent.ContentEncryptedElementSecurityEvent; |
| import org.testng.Assert; |
| import org.testng.annotations.Test; |
| |
| import javax.xml.namespace.QName; |
| import java.util.Date; |
| import java.util.LinkedList; |
| import java.util.List; |
| |
| /** |
| * @author $Author$ |
| * @version $Revision$ $Date$ |
| */ |
| public class UsernameTokenTest extends AbstractPolicyTestBase { |
| |
| @Test |
| public void testPolicy() throws Exception { |
| String policyString = |
| "<sp:SymmetricBinding xmlns:sp=\"http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702\" xmlns:sp3=\"http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200802\">\n" + |
| "<wsp:Policy xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">\n" + |
| "<sp:EncryptionToken>\n" + |
| " <wsp:Policy>\n" + |
| " <sp:UsernameToken>\n" + |
| " <sp:IssuerName>xs:anyURI</sp:IssuerName>\n" + |
| " <wsp:Policy xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">\n" + |
| " <sp:HashPassword/>\n" + |
| " <sp:WssUsernameToken11/>\n" + |
| " </wsp:Policy>\n" + |
| " </sp:UsernameToken>\n" + |
| " </wsp:Policy>\n" + |
| "</sp:EncryptionToken>\n" + |
| "<sp:SignatureToken>\n" + |
| " <wsp:Policy>\n" + |
| " <sp:UsernameToken>\n" + |
| " <sp:IssuerName>xs:anyURI</sp:IssuerName>\n" + |
| " <wsp:Policy xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">\n" + |
| " <sp:HashPassword/>\n" + |
| " <sp:WssUsernameToken11/>\n" + |
| " </wsp:Policy>\n" + |
| " </sp:UsernameToken>\n" + |
| " </wsp:Policy>\n" + |
| "</sp:SignatureToken>\n" + |
| " <sp:AlgorithmSuite>\n" + |
| " <wsp:Policy>\n" + |
| " <sp:Basic256/>\n" + |
| " </wsp:Policy>\n" + |
| " </sp:AlgorithmSuite>\n" + |
| "</wsp:Policy>\n" + |
| "</sp:SymmetricBinding>"; |
| |
| PolicyEnforcer policyEnforcer = buildAndStartPolicyEngine(policyString); |
| UsernameTokenSecurityEvent initiatorTokenSecurityEvent = new UsernameTokenSecurityEvent(); |
| initiatorTokenSecurityEvent.setUsernameTokenPasswordType(WSSConstants.UsernameTokenPasswordType.PASSWORD_DIGEST); |
| initiatorTokenSecurityEvent.setUsernameTokenProfile(WSSConstants.NS_USERNAMETOKEN_PROFILE11); |
| SecurityToken securityToken = new UsernameSecurityToken( |
| "username", "password", new Date().toString(), new byte[10], new byte[10], Long.valueOf(10), |
| (WSSecurityContext) null, null, null); |
| securityToken.addTokenUsage(SecurityToken.TokenUsage.MainSignature); |
| initiatorTokenSecurityEvent.setSecurityToken(securityToken); |
| policyEnforcer.registerSecurityEvent(initiatorTokenSecurityEvent); |
| |
| UsernameTokenSecurityEvent recipientTokenSecurityEvent = new UsernameTokenSecurityEvent(); |
| recipientTokenSecurityEvent.setUsernameTokenPasswordType(WSSConstants.UsernameTokenPasswordType.PASSWORD_DIGEST); |
| recipientTokenSecurityEvent.setUsernameTokenProfile(WSSConstants.NS_USERNAMETOKEN_PROFILE11); |
| securityToken = new UsernameSecurityToken( |
| "username", "password", new Date().toString(), new byte[10], new byte[10], Long.valueOf(10), |
| (WSSecurityContext) null, null, null); |
| securityToken.addTokenUsage(SecurityToken.TokenUsage.MainEncryption); |
| recipientTokenSecurityEvent.setSecurityToken(securityToken); |
| policyEnforcer.registerSecurityEvent(recipientTokenSecurityEvent); |
| |
| List<XMLSecurityConstants.ContentType> protectionOrder = new LinkedList<XMLSecurityConstants.ContentType>(); |
| protectionOrder.add(XMLSecurityConstants.ContentType.SIGNATURE); |
| protectionOrder.add(XMLSecurityConstants.ContentType.ENCRYPTION); |
| SignedPartSecurityEvent signedPartSecurityEvent = new SignedPartSecurityEvent(recipientTokenSecurityEvent.getSecurityToken(), true, protectionOrder); |
| signedPartSecurityEvent.setElementPath(WSSConstants.SOAP_11_BODY_PATH); |
| policyEnforcer.registerSecurityEvent(signedPartSecurityEvent); |
| |
| ContentEncryptedElementSecurityEvent contentEncryptedElementSecurityEvent = new ContentEncryptedElementSecurityEvent(recipientTokenSecurityEvent.getSecurityToken(), true, protectionOrder); |
| contentEncryptedElementSecurityEvent.setElementPath(WSSConstants.SOAP_11_BODY_PATH); |
| policyEnforcer.registerSecurityEvent(contentEncryptedElementSecurityEvent); |
| |
| OperationSecurityEvent operationSecurityEvent = new OperationSecurityEvent(); |
| operationSecurityEvent.setOperation(new QName("definitions")); |
| policyEnforcer.registerSecurityEvent(operationSecurityEvent); |
| |
| policyEnforcer.doFinal(); |
| } |
| |
| @Test |
| public void testPolicyNegative() throws Exception { |
| String policyString = |
| "<sp:SymmetricBinding xmlns:sp=\"http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702\" xmlns:sp3=\"http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200802\">\n" + |
| "<wsp:Policy xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">\n" + |
| "<sp:EncryptionToken>\n" + |
| " <wsp:Policy>\n" + |
| " <sp:UsernameToken>\n" + |
| " <sp:IssuerName>xs:anyURI</sp:IssuerName>\n" + |
| " <wsp:Policy xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">\n" + |
| " <sp:HashPassword/>\n" + |
| " <sp:WssUsernameToken11/>\n" + |
| " </wsp:Policy>\n" + |
| " </sp:UsernameToken>\n" + |
| " </wsp:Policy>\n" + |
| "</sp:EncryptionToken>\n" + |
| "<sp:SignatureToken>\n" + |
| " <wsp:Policy>\n" + |
| " <sp:UsernameToken>\n" + |
| " <sp:IssuerName>xs:anyURI</sp:IssuerName>\n" + |
| " <wsp:Policy xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">\n" + |
| " <sp:HashPassword/>\n" + |
| " <sp:WssUsernameToken11/>\n" + |
| " </wsp:Policy>\n" + |
| " </sp:UsernameToken>\n" + |
| " </wsp:Policy>\n" + |
| "</sp:SignatureToken>\n" + |
| " <sp:AlgorithmSuite>\n" + |
| " <wsp:Policy>\n" + |
| " <sp:Basic256/>\n" + |
| " </wsp:Policy>\n" + |
| " </sp:AlgorithmSuite>\n" + |
| "</wsp:Policy>\n" + |
| "</sp:SymmetricBinding>"; |
| |
| PolicyEnforcer policyEnforcer = buildAndStartPolicyEngine(policyString); |
| UsernameTokenSecurityEvent usernameTokenSecurityEvent = new UsernameTokenSecurityEvent(); |
| usernameTokenSecurityEvent.setUsernameTokenPasswordType(WSSConstants.UsernameTokenPasswordType.PASSWORD_TEXT); |
| usernameTokenSecurityEvent.setUsernameTokenProfile(WSSConstants.NS_USERNAMETOKEN_PROFILE11); |
| SecurityToken securityToken = new UsernameSecurityToken( |
| "username", "password", new Date().toString(), new byte[10], new byte[10], Long.valueOf(10), |
| (WSSecurityContext) null, null, null); |
| securityToken.addTokenUsage(SecurityToken.TokenUsage.MainSignature); |
| usernameTokenSecurityEvent.setSecurityToken(securityToken); |
| policyEnforcer.registerSecurityEvent(usernameTokenSecurityEvent); |
| |
| UsernameTokenSecurityEvent recipientTokenSecurityEvent = new UsernameTokenSecurityEvent(); |
| recipientTokenSecurityEvent.setUsernameTokenPasswordType(WSSConstants.UsernameTokenPasswordType.PASSWORD_TEXT); |
| recipientTokenSecurityEvent.setUsernameTokenProfile(WSSConstants.NS_USERNAMETOKEN_PROFILE11); |
| securityToken = new UsernameSecurityToken( |
| "username", "password", new Date().toString(), new byte[10], new byte[10], Long.valueOf(10), |
| (WSSecurityContext) null, null, null); |
| securityToken.addTokenUsage(SecurityToken.TokenUsage.MainEncryption); |
| recipientTokenSecurityEvent.setSecurityToken(securityToken); |
| policyEnforcer.registerSecurityEvent(recipientTokenSecurityEvent); |
| |
| List<XMLSecurityConstants.ContentType> protectionOrder = new LinkedList<XMLSecurityConstants.ContentType>(); |
| protectionOrder.add(XMLSecurityConstants.ContentType.SIGNATURE); |
| protectionOrder.add(XMLSecurityConstants.ContentType.ENCRYPTION); |
| SignedPartSecurityEvent signedPartSecurityEvent = new SignedPartSecurityEvent(recipientTokenSecurityEvent.getSecurityToken(), true, protectionOrder); |
| signedPartSecurityEvent.setElementPath(WSSConstants.SOAP_11_BODY_PATH); |
| policyEnforcer.registerSecurityEvent(signedPartSecurityEvent); |
| |
| ContentEncryptedElementSecurityEvent contentEncryptedElementSecurityEvent = new ContentEncryptedElementSecurityEvent(recipientTokenSecurityEvent.getSecurityToken(), true, protectionOrder); |
| contentEncryptedElementSecurityEvent.setElementPath(WSSConstants.SOAP_11_BODY_PATH); |
| policyEnforcer.registerSecurityEvent(contentEncryptedElementSecurityEvent); |
| |
| OperationSecurityEvent operationSecurityEvent = new OperationSecurityEvent(); |
| operationSecurityEvent.setOperation(new QName("definitions")); |
| |
| try { |
| policyEnforcer.registerSecurityEvent(operationSecurityEvent); |
| Assert.fail("Exception expected"); |
| } catch (WSSecurityException e) { |
| Assert.assertTrue(e.getCause() instanceof PolicyViolationException); |
| Assert.assertEquals(e.getCause().getMessage(), "\n" + |
| "UsernameToken does not contain a hashed password"); |
| } |
| } |
| } |