Author: anil.saldhana(a)jboss.com
Date: 2010-11-08 12:36:26 -0500 (Mon, 08 Nov 2010)
New Revision: 542
Added:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wsse/
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wsse/WSSecurityConstants.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSSecurityWriter.java
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustOnBehalfOfTestCase.java
Log:
PLFED-109: wst writing
Added:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wsse/WSSecurityConstants.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wsse/WSSecurityConstants.java
(rev 0)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wsse/WSSecurityConstants.java 2010-11-08
17:36:26 UTC (rev 542)
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.picketlink.identity.federation.core.wsse;
+
+/**
+ * Constants for WSS and WSSE
+ * @author Anil.Saldhana(a)redhat.com
+ * @since Nov 8, 2010
+ */
+public interface WSSecurityConstants
+{
+ String ID = "Id";
+ String USERNAME = "Username";
+ String USERNAME_TOKEN = "UsernameToken";
+ String WSSE_PREFIX = "wsse";
+ String WSU_PREFIX = "wsu";
+ String WSSE_NS =
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
+ String WSSE11_NS =
"http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd";
+ String WSU_NS =
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
+}
Added:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSSecurityWriter.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSSecurityWriter.java
(rev 0)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSSecurityWriter.java 2010-11-08
17:36:26 UTC (rev 542)
@@ -0,0 +1,83 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.picketlink.identity.federation.core.wstrust.writers;
+
+
+import static org.picketlink.identity.federation.core.wsse.WSSecurityConstants.ID;
+import static org.picketlink.identity.federation.core.wsse.WSSecurityConstants.USERNAME;
+import static
org.picketlink.identity.federation.core.wsse.WSSecurityConstants.USERNAME_TOKEN;
+import static org.picketlink.identity.federation.core.wsse.WSSecurityConstants.WSSE_NS;
+import static
org.picketlink.identity.federation.core.wsse.WSSecurityConstants.WSSE_PREFIX;
+import static org.picketlink.identity.federation.core.wsse.WSSecurityConstants.WSU_NS;
+import static
org.picketlink.identity.federation.core.wsse.WSSecurityConstants.WSU_PREFIX;
+
+import java.io.OutputStream;
+
+import javax.xml.namespace.QName;
+
+import org.picketlink.identity.federation.core.exceptions.ProcessingException;
+import org.picketlink.identity.federation.core.util.StaxUtil;
+import org.picketlink.identity.federation.core.util.StringUtil;
+import org.picketlink.identity.federation.ws.wss.secext.AttributedString;
+import org.picketlink.identity.federation.ws.wss.secext.UsernameTokenType;
+
+/**
+ * Write WS-Security Elements
+ * @author Anil.Saldhana(a)redhat.com
+ * @since Nov 8, 2010
+ */
+public class WSSecurityWriter extends AbstractWSWriter
+{
+ /**
+ * Write an {@code UsernameTokenType} to the stream
+ * @param appliesTo
+ * @param out
+ * @throws ProcessingException
+ */
+ public void write( UsernameTokenType usernameToken, OutputStream out ) throws
ProcessingException
+ {
+ verifyWriter(out);
+
+ StaxUtil.writeStartElement( writer, WSSE_PREFIX, USERNAME_TOKEN, WSSE_NS );
+ StaxUtil.writeNameSpace( writer, WSSE_PREFIX, WSSE_NS );
+
+ String id = usernameToken.getId();
+ if( StringUtil.isNullOrEmpty( id ))
+ throw new ProcessingException( " Id on the UsernameToken is null" );
+
+ StaxUtil.setPrefix(writer, WSU_PREFIX, WSU_NS );
+ QName wsuIDQName = new QName( WSU_NS, ID, WSU_PREFIX );
+ StaxUtil.writeAttribute(writer, wsuIDQName, id );
+ StaxUtil.writeNameSpace(writer, WSU_PREFIX, WSU_NS );
+
+ AttributedString userNameAttr = usernameToken.getUsername();
+ if( userNameAttr == null )
+ throw new ProcessingException( " User Name is null on the
UsernameToken" );
+
+ StaxUtil.writeStartElement( writer, WSSE_PREFIX, USERNAME, WSSE_NS );
+ StaxUtil.writeCharacters(writer, userNameAttr.getValue() );
+ StaxUtil.writeEndElement( writer );
+
+ StaxUtil.writeEndElement( writer );
+ StaxUtil.flush( writer );
+ }
+}
\ No newline at end of file
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java 2010-11-08
17:08:34 UTC (rev 541)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java 2010-11-08
17:36:26 UTC (rev 542)
@@ -36,7 +36,9 @@
import org.picketlink.identity.federation.core.wstrust.WSTrustConstants;
import org.picketlink.identity.federation.core.wstrust.wrappers.RequestSecurityToken;
import org.picketlink.identity.federation.ws.policy.AppliesTo;
+import org.picketlink.identity.federation.ws.trust.OnBehalfOfType;
import org.picketlink.identity.federation.ws.trust.UseKeyType;
+import org.picketlink.identity.federation.ws.wss.secext.UsernameTokenType;
import org.w3c.dom.Element;
/**
@@ -100,6 +102,18 @@
throw new RuntimeException( " Unknown use key type:" +
useKeyTypeValue.getClass().getName() );
}
+ OnBehalfOfType onBehalfOf = requestToken.getOnBehalfOf();
+ if( onBehalfOf != null )
+ {
+ StaxUtil.writeStartElement( writer, PREFIX, WSTrustConstants.On_BEHALF_OF,
BASE_NAMESPACE);
+ StaxUtil.writeCharacters(writer, "" );
+
+ UsernameTokenType usernameToken = (UsernameTokenType) onBehalfOf.getAny();
+ WSSecurityWriter wsseWriter = new WSSecurityWriter();
+ wsseWriter.write( usernameToken, out );
+ StaxUtil.writeEndElement( writer );
+ }
+
StaxUtil.writeEndElement( writer );
StaxUtil.flush( writer );
}
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustOnBehalfOfTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustOnBehalfOfTestCase.java 2010-11-08
17:08:34 UTC (rev 541)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustOnBehalfOfTestCase.java 2010-11-08
17:36:26 UTC (rev 542)
@@ -23,12 +23,16 @@
import static org.junit.Assert.assertEquals;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.junit.Test;
import org.picketlink.identity.federation.core.parsers.wst.WSTrustParser;
+import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
import org.picketlink.identity.federation.core.wstrust.WSTrustConstants;
import org.picketlink.identity.federation.core.wstrust.wrappers.RequestSecurityToken;
+import org.picketlink.identity.federation.core.wstrust.writers.WSTrustRSTWriter;
import org.picketlink.identity.federation.ws.trust.OnBehalfOfType;
import org.picketlink.identity.federation.ws.wss.secext.UsernameTokenType;
@@ -55,5 +59,14 @@
UsernameTokenType userNameToken = (UsernameTokenType) onBehalfOf.getAny();
assertEquals( "id", userNameToken.getId() );
assertEquals( "anotherduke", userNameToken.getUsername().getValue() );
+
+ //Now for the writing part
+ WSTrustRSTWriter rstWriter = new WSTrustRSTWriter();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ rstWriter.write(requestToken, baos );
+
+ System.out.println( new String( baos.toByteArray() ));
+ DocumentUtil.getDocument( new ByteArrayInputStream( baos.toByteArray() ));
}
}
\ No newline at end of file