Author: nandana Date: Mon Sep 20 06:23:35 2010 New Revision: 998793 URL: http://svn.apache.org/viewvc?rev=998793&view=rev Log: RAMPART-304 Applying the patch. Thanks Amila
Modified: axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/RampartMessageData.java axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rahas/SimpleTokenStoreTest.java axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/EncryptedKeyToken.java axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/SimpleTokenStore.java axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/Token.java Modified: axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/RampartMessageData.java URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/RampartMessageData.java?rev=998793&r1=998792&r2=998793&view=diff ============================================================================== --- axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/RampartMessageData.java (original) +++ axis/axis2/java/rampart/trunk/modules/rampart-core/src/main/java/org/apache/rampart/RampartMessageData.java Mon Sep 20 06:23:35 2010 @@ -56,6 +56,7 @@ import org.apache.ws.security.util.WSSec import org.opensaml.SAMLAssertion; import org.w3c.dom.Document; +import java.util.Date; import java.util.List; import java.util.Vector; import java.util.ArrayList; @@ -621,18 +622,17 @@ public class RampartMessageData { return this.tokenStorage; } - TokenStorage storage = (TokenStorage) this.msgContext.getProperty( + TokenStorage storage = (TokenStorage) this.msgContext.getConfigurationContext().getProperty( TokenStorage.TOKEN_STORAGE_KEY); if (storage != null) { this.tokenStorage = storage; } else { - if (this.policyData.getRampartConfig() != null && this.policyData.getRampartConfig().getTokenStoreClass() != null) { Class stClass = null; String storageClass = this.policyData.getRampartConfig() - .getTokenStoreClass(); + .getTokenStoreClass(); try { stClass = Loader.loadClass(msgContext.getAxisService() .getClassLoader(), storageClass); Modified: axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rahas/SimpleTokenStoreTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rahas/SimpleTokenStoreTest.java?rev=998793&r1=998792&r2=998793&view=diff ============================================================================== --- axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rahas/SimpleTokenStoreTest.java (original) +++ axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rahas/SimpleTokenStoreTest.java Mon Sep 20 06:23:35 2010 @@ -16,11 +16,19 @@ package org.apache.rahas; -import junit.framework.TestCase; +import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; +import org.apache.axiom.om.OMNamespace; import org.apache.axiom.om.impl.dom.DOOMAbstractFactory; +import junit.framework.TestCase; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.util.Date; public class SimpleTokenStoreTest extends TestCase { @@ -30,8 +38,7 @@ public class SimpleTokenStoreTest extend try { store.add(getTestToken("id-1")); } catch (TrustException e) { - fail("Adding a new token to an empty store should not fail, " + - "message : " + e.getMessage()); + fail("Adding a new token to an empty store should not fail, " + "message : " + e.getMessage()); } Token token = null; try { @@ -40,8 +47,7 @@ public class SimpleTokenStoreTest extend fail("Adding an existing token must throw an exception"); } catch (TrustException e) { assertEquals("Incorrect exception message", - TrustException.getMessage("tokenAlreadyExists", - new String[]{token.getId()}), e.getMessage()); + TrustException.getMessage("tokenAlreadyExists", new String[]{token.getId()}), e.getMessage()); } } @@ -76,9 +82,8 @@ public class SimpleTokenStoreTest extend store.update(token1); fail("An exception must be thrown at this point : noTokenToUpdate"); } catch (TrustException e) { - assertEquals("Incorrect exception message", TrustException - .getMessage("noTokenToUpdate", new String[]{token1 - .getId()}), e.getMessage()); + assertEquals("Incorrect exception message", + TrustException.getMessage("noTokenToUpdate", new String[]{token1.getId()}), e.getMessage()); } try { store.add(token1); @@ -133,11 +138,13 @@ public class SimpleTokenStoreTest extend } } - private Token getTestToken(String tokenId) throws TrustException { + private Token getTestToken(String tokenId) + throws TrustException { return getTestToken(tokenId, new Date()); } - private Token getTestToken(String tokenId, Date expiry) throws TrustException { + private Token getTestToken(String tokenId, Date expiry) + throws TrustException { OMFactory factory = DOOMAbstractFactory.getOMFactory(); OMElement tokenEle = factory.createOMElement("testToken", "", ""); Token token = new Token(tokenId, tokenEle, new Date(), expiry); @@ -147,4 +154,48 @@ public class SimpleTokenStoreTest extend token.setSecret("Top secret!".getBytes()); return token; } + + public void testSerialize() + throws Exception { + String fileName = "test.ser"; + + OMFactory factory = OMAbstractFactory.getOMFactory(); + OMNamespace ns1 = factory.createOMNamespace("bar", "x"); + OMElement elt11 = factory.createOMElement("foo1", ns1); + + Token t = new Token("#1232122", elt11, new Date(), new Date()); + + SimpleTokenStore store = new SimpleTokenStore(); + store.add(t); + + FileOutputStream fos = null; + ObjectOutputStream out = null; + + try { + fos = new FileOutputStream(fileName); + out = new ObjectOutputStream(fos); + out.writeObject(store); + } finally { + out.close(); + } + + SimpleTokenStore store2 = null; + FileInputStream fis = null; + ObjectInputStream in = null; + try { + fis = new FileInputStream(fileName); + in = new ObjectInputStream(fis); + store2 = (SimpleTokenStore)in.readObject(); + in.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } catch (ClassNotFoundException ex) { + ex.printStackTrace(); + } + + assertEquals(store.getToken("#1232122").getId(), store2.getToken("#1232122").getId()); + assertEquals(store.getToken("#1232122").getCreated(), store2.getToken("#1232122").getCreated()); + + } + } Modified: axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/EncryptedKeyToken.java URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/EncryptedKeyToken.java?rev=998793&r1=998792&r2=998793&view=diff ============================================================================== --- axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/EncryptedKeyToken.java (original) +++ axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/EncryptedKeyToken.java Mon Sep 20 06:23:35 2010 @@ -16,6 +16,9 @@ package org.apache.rahas; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; import java.util.Date; import org.apache.axiom.om.OMElement; @@ -35,6 +38,10 @@ public class EncryptedKeyToken extends T * SHA1 value of the encrypted key */ private String sha; + + public EncryptedKeyToken(){ + super(); + } public EncryptedKeyToken (String id,Date created, Date expires) { super(id,created,expires); @@ -59,4 +66,20 @@ public class EncryptedKeyToken extends T return sha; } + public void writeExternal(ObjectOutput out) + throws IOException { + + super.writeExternal(out); + out.writeObject(this.sha); + } + + public void readExternal(ObjectInput in) + throws ClassNotFoundException, IOException { + + super.readExternal(in); + this.sha = (String)in.readObject(); + + } + + } Modified: axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/SimpleTokenStore.java URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/SimpleTokenStore.java?rev=998793&r1=998792&r2=998793&view=diff ============================================================================== --- axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/SimpleTokenStore.java (original) +++ axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/SimpleTokenStore.java Mon Sep 20 06:23:35 2010 @@ -21,6 +21,8 @@ import org.apache.ws.security.WSConstant import org.apache.ws.security.message.token.Reference; import javax.xml.namespace.QName; + +import java.io.Serializable; import java.util.*; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; @@ -29,7 +31,7 @@ import java.util.concurrent.locks.Reentr /** * In-memory implementation of the token storage */ -public class SimpleTokenStore implements TokenStorage { +public class SimpleTokenStore implements TokenStorage, Serializable { protected Map tokens = new Hashtable(); @@ -185,8 +187,7 @@ public class SimpleTokenStore implements } finally { readLock.unlock(); - } - + } return token; } Modified: axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/Token.java URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/Token.java?rev=998793&r1=998792&r2=998793&view=diff ============================================================================== --- axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/Token.java (original) +++ axis/axis2/java/rampart/trunk/modules/rampart-trust/src/main/java/org/apache/rahas/Token.java Mon Sep 20 06:23:35 2010 @@ -18,164 +18,167 @@ package org.apache.rahas; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMException; +import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axiom.om.impl.dom.DOOMAbstractFactory; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.ws.security.WSConstants; import org.apache.ws.security.util.XmlSchemaDateFormat; import javax.xml.namespace.QName; - +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import java.io.ByteArrayInputStream; +import java.io.Externalizable; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.io.UnsupportedEncodingException; import java.text.DateFormat; import java.text.ParseException; import java.util.Date; import java.util.Properties; /** - * This represents a security token which can have either one of 4 states. - * <ul> - * <li>ISSUED</li> - * <li>EXPIRED</li> - * <li>CACELLED</li> - * <li>RENEWED</li> - * </ul> - * Also this holds the <code>OMElement</code>s representing the token in its + * This represents a security token which can have either one of 4 states. <ul> <li>ISSUED</li> <li>EXPIRED</li> + * <li>CACELLED</li> <li>RENEWED</li> </ul> Also this holds the <code>OMElement</code>s representing the token in its * present state and the previous state. - * - * These tokens are stored using the storage mechanism provided via the - * <code>TokenStorage</code> interface. + * <p/> + * These tokens are stored using the storage mechanism provided via the <code>TokenStorage</code> interface. + * * @see org.apache.rahas.TokenStorage */ -public class Token { - +public class Token implements Externalizable { + + private static Log log = LogFactory.getLog(Token.class); + public final static int ISSUED = 1; + public final static int EXPIRED = 2; + public final static int CANCELLED = 3; + public final static int RENEWED = 4; - + /** * Token identifier */ private String id; - + /** * Current state of the token */ private int state = -1; - + /** * The actual token in its current state */ private OMElement token; - + /** * The token in its previous state */ private OMElement previousToken; - + /** - * The RequestedAttachedReference element - * NOTE : The oasis-200401-wss-soap-message-security-1.0 spec allows - * an extensibility mechanism for wsse:SecurityTokenReference and - * wsse:Reference. Hence we cannot limit to the - * wsse:SecurityTokenReference\wsse:Reference case and only hold the URI and - * the ValueType values. + * The RequestedAttachedReference element NOTE : The oasis-200401-wss-soap-message-security-1.0 spec allows an + * extensibility mechanism for wsse:SecurityTokenReference and wsse:Reference. Hence we cannot limit to the + * wsse:SecurityTokenReference\wsse:Reference case and only hold the URI and the ValueType values. */ private OMElement attachedReference; - + /** - * The RequestedUnattachedReference element - * NOTE : The oasis-200401-wss-soap-message-security-1.0 spec allows - * an extensibility mechanism for wsse:SecurityTokenRefence and - * wsse:Reference. Hence we cannot limit to the - * wsse:SecurityTokenReference\wsse:Reference case and only hold the URI and - * the ValueType values. + * The RequestedUnattachedReference element NOTE : The oasis-200401-wss-soap-message-security-1.0 spec allows an + * extensibility mechanism for wsse:SecurityTokenRefence and wsse:Reference. Hence we cannot limit to the + * wsse:SecurityTokenReference\wsse:Reference case and only hold the URI and the ValueType values. */ private OMElement unattachedReference; - + /** * A bag to hold any other properties */ - private Properties properties; + private Properties properties; /** * A flag to assist the TokenStorage */ private boolean changed; - + /** * The secret associated with the Token */ private byte[] secret; - + /** * Created time */ private Date created; - + /** * Expiration time */ private Date expires; - + /** * Issuer end point address */ private String issuerAddress; - + private String encrKeySha1Value; - + + public Token() { + } + public Token(String id, Date created, Date expires) { - this.id = id; - this.created = created; - this.expires = expires; - } - - public Token(String id, - OMElement tokenElem, - Date created, - Date expires) throws TrustException { this.id = id; - StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(DOOMAbstractFactory.getOMFactory(), - tokenElem.getXMLStreamReader()); + this.created = created; + this.expires = expires; + } + + public Token(String id, OMElement tokenElem, Date created, Date expires) + throws TrustException { + this.id = id; + StAXOMBuilder stAXOMBuilder = + new StAXOMBuilder(DOOMAbstractFactory.getOMFactory(), tokenElem.getXMLStreamReader()); stAXOMBuilder.setNamespaceURIInterning(true); this.token = stAXOMBuilder.getDocumentElement(); this.created = created; this.expires = expires; } - public Token(String id, - OMElement tokenElem, - OMElement lifetimeElem) throws TrustException { + public Token(String id, OMElement tokenElem, OMElement lifetimeElem) + throws TrustException { this.id = id; - StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(DOOMAbstractFactory.getOMFactory(), - tokenElem.getXMLStreamReader()); + StAXOMBuilder stAXOMBuilder = + new StAXOMBuilder(DOOMAbstractFactory.getOMFactory(), tokenElem.getXMLStreamReader()); stAXOMBuilder.setNamespaceURIInterning(true); this.token = stAXOMBuilder.getDocumentElement(); this.processLifeTime(lifetimeElem); } - + /** * @param lifetimeElem - * @throws TrustException + * @throws TrustException */ - private void processLifeTime(OMElement lifetimeElem) throws TrustException { + private void processLifeTime(OMElement lifetimeElem) + throws TrustException { try { DateFormat zulu = new XmlSchemaDateFormat(); OMElement createdElem = - lifetimeElem.getFirstChildWithName(new QName(WSConstants.WSU_NS, - WSConstants.CREATED_LN)); + lifetimeElem.getFirstChildWithName(new QName(WSConstants.WSU_NS, WSConstants.CREATED_LN)); this.created = zulu.parse(createdElem.getText()); - + OMElement expiresElem = - lifetimeElem.getFirstChildWithName(new QName(WSConstants.WSU_NS, - WSConstants.EXPIRES_LN)); + lifetimeElem.getFirstChildWithName(new QName(WSConstants.WSU_NS, WSConstants.EXPIRES_LN)); this.expires = zulu.parse(expiresElem.getText()); } catch (OMException e) { - throw new TrustException("lifeTimeProcessingError", - new String[]{lifetimeElem.toString()}, e); + throw new TrustException("lifeTimeProcessingError", new String[]{lifetimeElem.toString()}, e); } catch (ParseException e) { - throw new TrustException("lifeTimeProcessingError", - new String[]{lifetimeElem.toString()}, e); + throw new TrustException("lifeTimeProcessingError", new String[]{lifetimeElem.toString()}, e); } } @@ -192,7 +195,7 @@ public class Token { public void setChanged(boolean chnaged) { this.changed = chnaged; } - + /** * @return Returns the properties. */ @@ -253,8 +256,8 @@ public class Token { * @param presivousToken The presivousToken to set. */ public void setPreviousToken(OMElement presivousToken) { - this.previousToken = new StAXOMBuilder(DOOMAbstractFactory.getOMFactory(), - presivousToken.getXMLStreamReader()).getDocumentElement(); + this.previousToken = new StAXOMBuilder(DOOMAbstractFactory.getOMFactory(), presivousToken.getXMLStreamReader()) + .getDocumentElement(); } /** @@ -282,9 +285,9 @@ public class Token { * @param attachedReference The attachedReference to set. */ public void setAttachedReference(OMElement attachedReference) { - if(attachedReference != null) { - this.attachedReference = new StAXOMBuilder(DOOMAbstractFactory - .getOMFactory(), attachedReference.getXMLStreamReader()) + if (attachedReference != null) { + this.attachedReference = + new StAXOMBuilder(DOOMAbstractFactory.getOMFactory(), attachedReference.getXMLStreamReader()) .getDocumentElement(); } } @@ -300,9 +303,9 @@ public class Token { * @param unattachedReference The unattachedReference to set. */ public void setUnattachedReference(OMElement unattachedReference) { - if(unattachedReference != null) { - this.unattachedReference = new StAXOMBuilder(DOOMAbstractFactory - .getOMFactory(), unattachedReference.getXMLStreamReader()) + if (unattachedReference != null) { + this.unattachedReference = + new StAXOMBuilder(DOOMAbstractFactory.getOMFactory(), unattachedReference.getXMLStreamReader()) .getDocumentElement(); } } @@ -335,4 +338,150 @@ public class Token { public void setIssuerAddress(String issuerAddress) { this.issuerAddress = issuerAddress; } + + /** + * Implementing serialize logic according to our own protocol. We had to follow this, because + * OMElement class is not serializable. Making OMElement serializable will have an huge impact + * on other components. Therefore implementing serialization logic according to a manual + * protocol. + * @param out Stream which writes serialized bytes. + * @throws IOException If unable to serialize particular member. + */ + public void writeExternal(ObjectOutput out) + throws IOException { + + out.writeObject(this.id); + + out.writeInt(this.state); + + String stringElement = convertOMElementToString(this.token); + out.writeObject(stringElement); + + stringElement = convertOMElementToString(this.previousToken); + out.writeObject(stringElement); + + stringElement = convertOMElementToString(this.attachedReference); + out.writeObject(stringElement); + + stringElement = convertOMElementToString(this.unattachedReference); + out.writeObject(stringElement); + + out.writeObject(this.properties); + + out.writeBoolean(this.changed); + + int secretLength = 0; + if (null != this.secret) { + secretLength = this.secret.length; + } + + // First write the length of secret + out.writeInt(secretLength); + if (0 != secretLength) { + out.write(this.secret); + } + + out.writeObject(this.created); + + out.writeObject(this.expires); + + out.writeObject(this.issuerAddress); + + out.writeObject(this.encrKeySha1Value); + } + + /** + * Implementing de-serialization logic in accordance with the serialization logic. + * @param in Stream which used to read data. + * @throws IOException If unable to de-serialize particular data member. + * @throws ClassNotFoundException + */ + public void readExternal(ObjectInput in) + throws IOException, ClassNotFoundException { + + this.id = (String)in.readObject(); + + this.state = in.readInt(); + + String stringElement = (String)in.readObject(); + this.token = convertStringToOMElement(stringElement); + + stringElement = (String)in.readObject(); + this.previousToken = convertStringToOMElement(stringElement); + + stringElement = (String)in.readObject(); + this.attachedReference = convertStringToOMElement(stringElement); + + stringElement = (String)in.readObject(); + this.unattachedReference = convertStringToOMElement(stringElement); + + this.properties = (Properties)in.readObject(); + + this.changed = in.readBoolean(); + + // Read the length of the secret + int secretLength = in.readInt(); + + if (0 != secretLength) { + byte[] buffer = new byte[secretLength]; + if (secretLength != in.read(buffer)) { + throw new IllegalStateException("Bytes read from the secret key is not equal to serialized length"); + } + this.secret = buffer; + }else{ + this.secret = null; + } + + this.created = (Date)in.readObject(); + + this.expires = (Date)in.readObject(); + + this.issuerAddress = (String)in.readObject(); + + this.encrKeySha1Value = (String)in.readObject(); + } + + private String convertOMElementToString(OMElement element) + throws IOException { + String serializedToken = ""; + + if (null == element) { + return serializedToken; + } + + try { + serializedToken = element.toStringWithConsume(); + } catch (XMLStreamException e) { + throw new IOException("Could not serialize token OM element"); + } + + return serializedToken; + } + + private OMElement convertStringToOMElement(String stringElement) + throws IOException { + + if (null == stringElement || stringElement.trim().equals("")) { + return null; + } + + try { + InputStream is = new ByteArrayInputStream(stringElement.getBytes("UTF-8")); + XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(is); + StAXOMBuilder builder = new StAXOMBuilder(parser); + OMElement documentElement = builder.getDocumentElement(); + + XMLStreamReader llomReader = documentElement.getXMLStreamReader(); + OMFactory doomFactory = DOOMAbstractFactory.getOMFactory(); + StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory, llomReader); + return doomBuilder.getDocumentElement(); + + } catch (UnsupportedEncodingException e) { + log.error("Cannot convert de-serialized string to OMElement. Incorrect encoding format", e); + throw new IOException("Cannot convert de-serialized string to OMElement. Incorrect encoding format", e); + } catch (XMLStreamException e) { + log.error("Cannot convert de-serialized string to OMElement. Could not create XML stream.", e); + throw new IOException("Cannot convert de-serialized string to OMElement. Could not create XML stream.", e); + } + } }