Author: anil.saldhana(a)jboss.com
Date: 2010-11-04 16:40:06 -0400 (Thu, 04 Nov 2010)
New Revision: 531
Added:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/util/SAMLParserUtil.java
federation/trunk/picketlink-fed-core/src/test/resources/parser/saml2/saml2-response-assertion-subject.xml
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLAssertionParser.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLResponseParser.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLSloResponseParser.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLSubjectParser.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLAssertionParserTestCase.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLResponseParserTestCase.java
Log:
take care of subjects and attributes
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLAssertionParser.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLAssertionParser.java 2010-11-04
17:16:37 UTC (rev 530)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLAssertionParser.java 2010-11-04
20:40:06 UTC (rev 531)
@@ -21,7 +21,6 @@
*/
package org.picketlink.identity.federation.core.parsers.saml;
-import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.events.Attribute;
@@ -31,15 +30,13 @@
import org.picketlink.identity.federation.core.exceptions.ParsingException;
import org.picketlink.identity.federation.core.parsers.ParserNamespaceSupport;
+import org.picketlink.identity.federation.core.parsers.util.SAMLParserUtil;
import org.picketlink.identity.federation.core.parsers.util.StaxParserUtil;
import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLConstants;
import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLURIConstants;
-import org.picketlink.identity.federation.core.saml.v2.factories.SAMLAssertionFactory;
import org.picketlink.identity.federation.core.saml.v2.util.XMLTimeUtil;
import org.picketlink.identity.federation.saml.v2.assertion.AssertionType;
import org.picketlink.identity.federation.saml.v2.assertion.AttributeStatementType;
-import org.picketlink.identity.federation.saml.v2.assertion.AttributeType;
-import org.picketlink.identity.federation.saml.v2.assertion.AuthnContextType;
import org.picketlink.identity.federation.saml.v2.assertion.AuthnStatementType;
import org.picketlink.identity.federation.saml.v2.assertion.ConditionsType;
import org.picketlink.identity.federation.saml.v2.assertion.NameIDType;
@@ -123,12 +120,12 @@
}
else if( JBossSAMLConstants.AUTHN_STATEMENT.get().equalsIgnoreCase( tag ) )
{
- AuthnStatementType authnStatementType = parseAuthnStatement( xmlEventReader
);
+ AuthnStatementType authnStatementType = SAMLParserUtil.parseAuthnStatement(
xmlEventReader );
assertion.getStatementOrAuthnStatementOrAuthzDecisionStatement().add(
authnStatementType );
}
else if( JBossSAMLConstants.ATTRIBUTE_STATEMENT.get().equalsIgnoreCase( tag ) )
{
- AttributeStatementType attributeStatementType = parseAttributeStatement(
xmlEventReader );
+ AttributeStatementType attributeStatementType =
SAMLParserUtil.parseAttributeStatement( xmlEventReader );
assertion.getStatementOrAuthnStatementOrAuthzDecisionStatement().add(
attributeStatementType );
}
else throw new RuntimeException( "SAMLAssertionParser:: unknown: " +
tag );
@@ -165,179 +162,4 @@
return assertion;
}
-
- /**
- * Parse the AuthnStatement inside the assertion
- * @param xmlEventReader
- * @return
- * @throws ParsingException
- */
- private AuthnStatementType parseAuthnStatement( XMLEventReader xmlEventReader ) throws
ParsingException
- {
- AuthnStatementType authnStatementType = new AuthnStatementType();
-
- StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
- String AUTHNSTATEMENT = JBossSAMLConstants.AUTHN_STATEMENT.get();
- StaxParserUtil.validate( startElement, AUTHNSTATEMENT );
-
- Attribute authnInstant = startElement.getAttributeByName( new QName(
"AuthnInstant" ));
- if( authnInstant == null )
- throw new RuntimeException( "Required attribute AuthnInstant in " +
AUTHNSTATEMENT );
- authnStatementType.setAuthnInstant( XMLTimeUtil.parse(
StaxParserUtil.getAttributeValue( authnInstant )));
-
- Attribute sessionIndex = startElement.getAttributeByName( new QName(
"SessionIndex" ));
- if( sessionIndex != null )
- authnStatementType.setSessionIndex( StaxParserUtil.getAttributeValue(
sessionIndex ));
-
- //Get the next start element
- startElement = StaxParserUtil.peekNextStartElement( xmlEventReader );
- String tag = startElement.getName().getLocalPart();
- if( JBossSAMLConstants.AUTHN_CONTEXT.get().equals( tag ) )
- {
- authnStatementType.setAuthnContext( parseAuthnContextType( xmlEventReader ) );
- }
- else throw new RuntimeException( "Unknown tag:" + tag );
-
- EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
- StaxParserUtil.validate(endElement, AUTHNSTATEMENT );
-
- return authnStatementType;
- }
-
- /**
- * Parse the AuthnStatement inside the assertion
- * @param xmlEventReader
- * @return
- * @throws ParsingException
- */
- private AttributeStatementType parseAttributeStatement( XMLEventReader xmlEventReader
) throws ParsingException
- {
- AttributeStatementType attributeStatementType = new AttributeStatementType();
-
- StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
- String AUTHNSTATEMENT = JBossSAMLConstants.ATTRIBUTE_STATEMENT.get();
- StaxParserUtil.validate( startElement, AUTHNSTATEMENT );
-
- while( xmlEventReader.hasNext() )
- {
- //Get the next start element
- startElement = StaxParserUtil.peekNextStartElement( xmlEventReader );
- String tag = startElement.getName().getLocalPart();
- if( JBossSAMLConstants.ATTRIBUTE.get().equals( tag ) )
- {
- AttributeType attribute = parseAttribute(xmlEventReader);
- attributeStatementType.getAttributeOrEncryptedAttribute().add( attribute );
- }
- else throw new RuntimeException( "Unknown tag:" + tag );
- }
-
- /* EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
- StaxParserUtil.validate(endElement,JBossSAMLConstants.ATTRIBUTE_STATEMENT.get() );
- */
- return attributeStatementType;
- }
-
- /**
- * Parse an {@code AttributeType}
- * @param xmlEventReader
- * @return
- * @throws ParsingException
- */
- private AttributeType parseAttribute( XMLEventReader xmlEventReader ) throws
ParsingException
- {
- AttributeType attributeType = new AttributeType();
-
- StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
- StaxParserUtil.validate( startElement, JBossSAMLConstants.ATTRIBUTE.get() );
-
- Attribute name = startElement.getAttributeByName( new QName(
JBossSAMLConstants.NAME.get() ));
- if( name == null )
- throw new RuntimeException( "Required attribute Name in Attribute" );
- attributeType.setName( StaxParserUtil.getAttributeValue( name ));
-
- Attribute friendlyName = startElement.getAttributeByName( new QName(
JBossSAMLConstants.FRIENDLY_NAME.get() ));
- if( friendlyName != null )
- attributeType.setFriendlyName( StaxParserUtil.getAttributeValue( friendlyName
));
-
- Attribute nameFormat = startElement.getAttributeByName( new QName(
JBossSAMLConstants.NAME_FORMAT.get() ));
- if( nameFormat != null )
- attributeType.setNameFormat( StaxParserUtil.getAttributeValue( nameFormat ));
-
- while( xmlEventReader.hasNext() )
- {
- startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
- if( startElement == null )
- break;
- String tag = StaxParserUtil.getStartElementName(startElement);
-
- if( JBossSAMLConstants.ATTRIBUTE.get().equals( tag ))
- break;
-
- if( JBossSAMLConstants.ATTRIBUTE_VALUE.get().equals( tag ) )
- {
- Object attributeValue = parseAttributeValue(xmlEventReader);
- attributeType.getAttributeValue().add( attributeValue );
- }
- else throw new RuntimeException( "Unknown tag:" + tag );
- }
-
- return attributeType;
- }
-
- /**
- * Parse the AuthnContext Type inside the AuthnStatement
- * @param xmlEventReader
- * @return
- * @throws ParsingException
- */
- private AuthnContextType parseAuthnContextType( XMLEventReader xmlEventReader ) throws
ParsingException
- {
- AuthnContextType authnContextType = new AuthnContextType();
-
- StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
- StaxParserUtil.validate( startElement, JBossSAMLConstants.AUTHN_CONTEXT.get() );
-
- //Get the next start element
- startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
- String tag = startElement.getName().getLocalPart();
-
- if( JBossSAMLConstants.AUTHN_CONTEXT_DECLARATION_REF.get().equals( tag ))
- {
- String text = StaxParserUtil.getElementText( xmlEventReader );
-
- JAXBElement<?> acDeclRef =
SAMLAssertionFactory.getObjectFactory().createAuthnContextDeclRef( text );
- authnContextType.getContent().add(acDeclRef);
- EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
- StaxParserUtil.validate(endElement, JBossSAMLConstants.AUTHN_CONTEXT.get() );
- }
- else
- throw new RuntimeException( "Unknown Tag:" + tag );
-
- return authnContextType;
- }
-
- /**
- * Parse Attribute value
- * @param xmlEventReader
- * @return
- * @throws ParsingException
- */
- private Object parseAttributeValue( XMLEventReader xmlEventReader ) throws
ParsingException
- {
- StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
- StaxParserUtil.validate( startElement, JBossSAMLConstants.ATTRIBUTE_VALUE.get() );
-
- Attribute type = startElement.getAttributeByName( new QName(
JBossSAMLURIConstants.XSI_NSURI.get(),
- "type", "xsi"));
- if( type == null )
- throw new RuntimeException( "attribute value has no xsi type" );
-
- String typeValue = StaxParserUtil.getAttributeValue(type);
- if( typeValue.contains( ":string" ))
- {
- return StaxParserUtil.getElementText(xmlEventReader);
- }
-
- throw new RuntimeException( "Unsupported xsi:type=" + typeValue );
- }
}
\ No newline at end of file
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLResponseParser.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLResponseParser.java 2010-11-04
17:16:37 UTC (rev 530)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLResponseParser.java 2010-11-04
20:40:06 UTC (rev 531)
@@ -108,7 +108,5 @@
super.parseBaseAttributes( startElement, response );
return response;
- }
-
-
+ }
}
\ No newline at end of file
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLSloResponseParser.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLSloResponseParser.java 2010-11-04
17:16:37 UTC (rev 530)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLSloResponseParser.java 2010-11-04
20:40:06 UTC (rev 531)
@@ -103,4 +103,4 @@
return JBossSAMLURIConstants.PROTOCOL_NSURI.get().equals( qname.getNamespaceURI()
)
&& LOGOUT_RESPONSE.equals( qname.getLocalPart() );
}
-}
+}
\ No newline at end of file
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLSubjectParser.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLSubjectParser.java 2010-11-04
17:16:37 UTC (rev 530)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/saml/SAMLSubjectParser.java 2010-11-04
20:40:06 UTC (rev 531)
@@ -24,7 +24,6 @@
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
-import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
@@ -32,10 +31,13 @@
import org.picketlink.identity.federation.core.exceptions.ParsingException;
import org.picketlink.identity.federation.core.parsers.ParserNamespaceSupport;
+import org.picketlink.identity.federation.core.parsers.util.SAMLParserUtil;
import org.picketlink.identity.federation.core.parsers.util.StaxParserUtil;
import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLConstants;
import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLURIConstants;
+import org.picketlink.identity.federation.core.saml.v2.factories.SAMLAssertionFactory;
import org.picketlink.identity.federation.core.saml.v2.util.XMLTimeUtil;
+import org.picketlink.identity.federation.saml.v2.assertion.AttributeStatementType;
import org.picketlink.identity.federation.saml.v2.assertion.NameIDType;
import org.picketlink.identity.federation.saml.v2.assertion.ObjectFactory;
import org.picketlink.identity.federation.saml.v2.assertion.SubjectConfirmationDataType;
@@ -79,32 +81,9 @@
if( JBossSAMLConstants.NAMEID.get().equalsIgnoreCase( tag ) )
{
- try
- {
- StartElement nameIDElement = StaxParserUtil.getNextStartElement(
xmlEventReader );
- Attribute nameQualifier = nameIDElement.getAttributeByName( new QName(
JBossSAMLConstants.NAME_QUALIFIER.get() ));
- if( nameQualifier == null )
- nameQualifier = nameIDElement.getAttributeByName( new QName(
JBossSAMLURIConstants.ASSERTION_NSURI.get(),
- JBossSAMLConstants.NAME_QUALIFIER.get() ));
-
- String nameIDValue = xmlEventReader.getElementText();
-
- NameIDType nameID = new NameIDType();
- nameID.setValue( nameIDValue );
- if( nameQualifier != null )
- {
- nameID.setNameQualifier(
StaxParserUtil.getAttributeValue(nameQualifier) );
- }
-
- JAXBElement<NameIDType> jaxbNameID = objectFactory.createNameID(
nameID );
- subject.getContent().add( jaxbNameID );
-
- //There is no need to get the end tag as the "getElementText"
call above puts us past that
- }
- catch (XMLStreamException e)
- {
- throw new ParsingException( e );
- }
+ NameIDType nameID = SAMLParserUtil.parseNameIDType(xmlEventReader);
+ JAXBElement<NameIDType> jaxbNameID = objectFactory.createNameID(
nameID );
+ subject.getContent().add( jaxbNameID );
}
else if( JBossSAMLConstants.SUBJECT_CONFIRMATION.get().equalsIgnoreCase( tag )
)
{
@@ -138,7 +117,13 @@
//Get the end tag
EndElement endElement = (EndElement)
StaxParserUtil.getNextEvent(xmlEventReader);
StaxParserUtil.matches(endElement,
JBossSAMLConstants.SUBJECT_CONFIRMATION.get() );
- }
+ }
+ else if( JBossSAMLConstants.ATTRIBUTE_STATEMENT.get().equals( tag ))
+ {
+ AttributeStatementType attributeStatement =
SAMLParserUtil.parseAttributeStatement(xmlEventReader);
+ JAXBElement<?> jaxbEl =
SAMLAssertionFactory.getObjectFactory().createAttributeStatement(attributeStatement);
+ subject.getContent().add( jaxbEl );
+ }
else throw new RuntimeException( "Unknown tag:" + tag );
}
Added:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/util/SAMLParserUtil.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/util/SAMLParserUtil.java
(rev 0)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/parsers/util/SAMLParserUtil.java 2010-11-04
20:40:06 UTC (rev 531)
@@ -0,0 +1,249 @@
+/*
+ * 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.parsers.util;
+
+import javax.xml.bind.JAXBElement;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.StartElement;
+
+import org.picketlink.identity.federation.core.exceptions.ParsingException;
+import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLConstants;
+import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLURIConstants;
+import org.picketlink.identity.federation.core.saml.v2.factories.SAMLAssertionFactory;
+import org.picketlink.identity.federation.core.saml.v2.util.XMLTimeUtil;
+import org.picketlink.identity.federation.saml.v2.assertion.AttributeStatementType;
+import org.picketlink.identity.federation.saml.v2.assertion.AttributeType;
+import org.picketlink.identity.federation.saml.v2.assertion.AuthnContextType;
+import org.picketlink.identity.federation.saml.v2.assertion.AuthnStatementType;
+import org.picketlink.identity.federation.saml.v2.assertion.NameIDType;
+
+/**
+ * Utility methods for SAML Parser
+ * @author Anil.Saldhana(a)redhat.com
+ * @since Nov 4, 2010
+ */
+public class SAMLParserUtil
+{
+ public static AttributeStatementType parseAttributeStatement( XMLEventReader
xmlEventReader ) throws ParsingException
+ {
+ AttributeStatementType attributeStatementType = new AttributeStatementType();
+
+ StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
+ String AUTHNSTATEMENT = JBossSAMLConstants.ATTRIBUTE_STATEMENT.get();
+ StaxParserUtil.validate( startElement, AUTHNSTATEMENT );
+
+ while( xmlEventReader.hasNext() )
+ {
+ //Get the next start element
+ startElement = StaxParserUtil.peekNextStartElement( xmlEventReader );
+ String tag = startElement.getName().getLocalPart();
+ if( JBossSAMLConstants.ATTRIBUTE.get().equals( tag ) )
+ {
+ AttributeType attribute = parseAttribute(xmlEventReader);
+ attributeStatementType.getAttributeOrEncryptedAttribute().add( attribute );
+ }
+ else throw new RuntimeException( "Unknown tag:" + tag );
+ }
+ return attributeStatementType;
+ }
+
+ /**
+ * Parse an {@code AttributeType}
+ * @param xmlEventReader
+ * @return
+ * @throws ParsingException
+ */
+ public static AttributeType parseAttribute( XMLEventReader xmlEventReader ) throws
ParsingException
+ {
+ AttributeType attributeType = new AttributeType();
+
+ StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
+ StaxParserUtil.validate( startElement, JBossSAMLConstants.ATTRIBUTE.get() );
+
+ Attribute name = startElement.getAttributeByName( new QName(
JBossSAMLConstants.NAME.get() ));
+ if( name == null )
+ throw new RuntimeException( "Required attribute Name in Attribute" );
+ attributeType.setName( StaxParserUtil.getAttributeValue( name ));
+
+ Attribute friendlyName = startElement.getAttributeByName( new QName(
JBossSAMLConstants.FRIENDLY_NAME.get() ));
+ if( friendlyName != null )
+ attributeType.setFriendlyName( StaxParserUtil.getAttributeValue( friendlyName
));
+
+ Attribute nameFormat = startElement.getAttributeByName( new QName(
JBossSAMLConstants.NAME_FORMAT.get() ));
+ if( nameFormat != null )
+ attributeType.setNameFormat( StaxParserUtil.getAttributeValue( nameFormat ));
+
+ while( xmlEventReader.hasNext() )
+ {
+ startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
+ if( startElement == null )
+ break;
+ String tag = StaxParserUtil.getStartElementName(startElement);
+
+ if( JBossSAMLConstants.ATTRIBUTE.get().equals( tag ))
+ break;
+
+ if( JBossSAMLConstants.ATTRIBUTE_VALUE.get().equals( tag ) )
+ {
+ Object attributeValue = parseAttributeValue(xmlEventReader);
+ attributeType.getAttributeValue().add( attributeValue );
+ }
+ else throw new RuntimeException( "Unknown tag:" + tag );
+ }
+
+ return attributeType;
+ }
+
+ /**
+ * Parse Attribute value
+ * @param xmlEventReader
+ * @return
+ * @throws ParsingException
+ */
+ public static Object parseAttributeValue( XMLEventReader xmlEventReader ) throws
ParsingException
+ {
+ StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
+ StaxParserUtil.validate( startElement, JBossSAMLConstants.ATTRIBUTE_VALUE.get() );
+
+ Attribute type = startElement.getAttributeByName( new QName(
JBossSAMLURIConstants.XSI_NSURI.get(),
+ "type", "xsi"));
+ if( type == null )
+ throw new RuntimeException( "attribute value has no xsi type" );
+
+ String typeValue = StaxParserUtil.getAttributeValue(type);
+ if( typeValue.contains( ":string" ))
+ {
+ return StaxParserUtil.getElementText(xmlEventReader);
+ }
+
+ throw new RuntimeException( "Unsupported xsi:type=" + typeValue );
+ }
+
+ /**
+ * Parse the AuthnStatement inside the assertion
+ * @param xmlEventReader
+ * @return
+ * @throws ParsingException
+ */
+ public static AuthnStatementType parseAuthnStatement( XMLEventReader xmlEventReader )
throws ParsingException
+ {
+ AuthnStatementType authnStatementType = new AuthnStatementType();
+
+ StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
+ String AUTHNSTATEMENT = JBossSAMLConstants.AUTHN_STATEMENT.get();
+ StaxParserUtil.validate( startElement, AUTHNSTATEMENT );
+
+ Attribute authnInstant = startElement.getAttributeByName( new QName(
"AuthnInstant" ));
+ if( authnInstant == null )
+ throw new RuntimeException( "Required attribute AuthnInstant in " +
AUTHNSTATEMENT );
+ authnStatementType.setAuthnInstant( XMLTimeUtil.parse(
StaxParserUtil.getAttributeValue( authnInstant )));
+
+ Attribute sessionIndex = startElement.getAttributeByName( new QName(
"SessionIndex" ));
+ if( sessionIndex != null )
+ authnStatementType.setSessionIndex( StaxParserUtil.getAttributeValue(
sessionIndex ));
+
+ //Get the next start element
+ startElement = StaxParserUtil.peekNextStartElement( xmlEventReader );
+ String tag = startElement.getName().getLocalPart();
+ if( JBossSAMLConstants.AUTHN_CONTEXT.get().equals( tag ) )
+ {
+ authnStatementType.setAuthnContext( parseAuthnContextType( xmlEventReader ) );
+ }
+ else throw new RuntimeException( "Unknown tag:" + tag );
+
+ EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
+ StaxParserUtil.validate(endElement, AUTHNSTATEMENT );
+
+ return authnStatementType;
+ }
+
+ /**
+ * Parse the AuthnContext Type inside the AuthnStatement
+ * @param xmlEventReader
+ * @return
+ * @throws ParsingException
+ */
+ public static AuthnContextType parseAuthnContextType( XMLEventReader xmlEventReader )
throws ParsingException
+ {
+ AuthnContextType authnContextType = new AuthnContextType();
+
+ StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
+ StaxParserUtil.validate( startElement, JBossSAMLConstants.AUTHN_CONTEXT.get() );
+
+ //Get the next start element
+ startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
+ String tag = startElement.getName().getLocalPart();
+
+ if( JBossSAMLConstants.AUTHN_CONTEXT_DECLARATION_REF.get().equals( tag ))
+ {
+ String text = StaxParserUtil.getElementText( xmlEventReader );
+
+ JAXBElement<?> acDeclRef =
SAMLAssertionFactory.getObjectFactory().createAuthnContextDeclRef( text );
+ authnContextType.getContent().add(acDeclRef);
+ EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
+ StaxParserUtil.validate(endElement, JBossSAMLConstants.AUTHN_CONTEXT.get() );
+ }
+ else
+ throw new RuntimeException( "Unknown Tag:" + tag );
+
+ return authnContextType;
+ }
+
+ public static NameIDType parseNameIDType( XMLEventReader xmlEventReader ) throws
ParsingException
+ {
+ StartElement nameIDElement = StaxParserUtil.getNextStartElement( xmlEventReader );
+ NameIDType nameID = new NameIDType();
+
+ Attribute nameQualifier = nameIDElement.getAttributeByName( new QName(
JBossSAMLConstants.NAME_QUALIFIER.get() ));
+ if( nameQualifier != null )
+ {
+ nameID.setNameQualifier( StaxParserUtil.getAttributeValue(nameQualifier) );
+ }
+
+ Attribute format = nameIDElement.getAttributeByName( new QName(
JBossSAMLConstants.FORMAT.get() ));
+ if( format != null )
+ {
+ nameID.setFormat( StaxParserUtil.getAttributeValue( format ));
+ }
+
+ Attribute spProvidedID = nameIDElement.getAttributeByName( new QName(
JBossSAMLConstants.SP_PROVIDED_ID.get() ));
+ if( spProvidedID != null )
+ {
+ nameID.setSPProvidedID( StaxParserUtil.getAttributeValue( spProvidedID ));
+ }
+
+ Attribute spNameQualifier = nameIDElement.getAttributeByName( new QName(
JBossSAMLConstants.SP_NAME_QUALIFIER.get() ));
+ if( spNameQualifier != null )
+ {
+ nameID.setSPNameQualifier( StaxParserUtil.getAttributeValue( spNameQualifier
));
+ }
+
+ String nameIDValue = StaxParserUtil.getElementText( xmlEventReader );
+ nameID.setValue( nameIDValue );
+
+
+ return nameID;
+ }
+}
\ No newline at end of file
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLAssertionParserTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLAssertionParserTestCase.java 2010-11-04
17:16:37 UTC (rev 530)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLAssertionParserTestCase.java 2010-11-04
20:40:06 UTC (rev 531)
@@ -113,10 +113,8 @@
//Subject
SubjectType subject = assertion.getSubject();
- List<JAXBElement<?>> content = subject.getContent();
+ List<JAXBElement<?>> content = subject.getContent();
-
-
int size = content.size();
for( int i = 0 ; i < size; i++ )
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLResponseParserTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLResponseParserTestCase.java 2010-11-04
17:16:37 UTC (rev 530)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLResponseParserTestCase.java 2010-11-04
20:40:06 UTC (rev 531)
@@ -28,13 +28,19 @@
import java.util.List;
import javax.xml.bind.JAXBElement;
-
+
import org.junit.Test;
import org.picketlink.identity.federation.core.parsers.saml.SAMLParser;
import org.picketlink.identity.federation.core.saml.v2.util.XMLTimeUtil;
import org.picketlink.identity.federation.core.saml.v2.writers.SAMLResponseWriter;
import org.picketlink.identity.federation.saml.v2.assertion.AssertionType;
+import org.picketlink.identity.federation.saml.v2.assertion.AttributeStatementType;
+import org.picketlink.identity.federation.saml.v2.assertion.AttributeType;
import org.picketlink.identity.federation.saml.v2.assertion.AuthnStatementType;
+import org.picketlink.identity.federation.saml.v2.assertion.NameIDType;
+import org.picketlink.identity.federation.saml.v2.assertion.SubjectConfirmationDataType;
+import org.picketlink.identity.federation.saml.v2.assertion.SubjectConfirmationType;
+import org.picketlink.identity.federation.saml.v2.assertion.SubjectType;
import org.picketlink.identity.federation.saml.v2.protocol.ResponseType;
import org.picketlink.identity.federation.saml.v2.protocol.StatusType;
@@ -95,4 +101,91 @@
SAMLResponseWriter writer = new SAMLResponseWriter();
writer.write(response, System.out );
}
+
+ @Test
+ public void testAssertionWithSubjectAndAttributes() throws Exception
+ {
+ ClassLoader tcl = Thread.currentThread().getContextClassLoader();
+ InputStream configStream = tcl.getResourceAsStream(
"parser/saml2/saml2-response-assertion-subject.xml" );
+
+ SAMLParser parser = new SAMLParser();
+ ResponseType response = ( ResponseType ) parser.parse(configStream);
+ assertNotNull( response );
+
+ assertEquals( "ID_45df1ea5-81e4-4147-a39a-43a4ef613f4e", response.getID()
);
+ assertEquals( XMLTimeUtil.parse( "2010-11-04T00:19:16.847-05:00" ),
response.getIssueInstant() );
+ assertEquals( "2.0", response.getVersion() );
+ assertEquals( "http://localhost:8080/employee/",
response.getDestination() );
+ assertEquals( "ID_04ded476-d73c-48af-b3a9-232a52905ffb",
response.getInResponseTo() );
+
+ //Issuer
+ assertEquals( "http://localhost:8080/idp/",
response.getIssuer().getValue() );
+
+ //Status
+ StatusType status = response.getStatus();
+ assertEquals( "urn:oasis:names:tc:SAML:2.0:status:Success",
status.getStatusCode().getValue() );
+
+ //Get the assertion
+ AssertionType assertion = (AssertionType)
response.getAssertionOrEncryptedAssertion().get(0);
+ assertEquals( "ID_8be1534d-9155-4837-9f26-70ea2c15e327",
assertion.getID() );
+ assertEquals( XMLTimeUtil.parse( "2010-11-04T00:19:16.842-05:00" ),
assertion.getIssueInstant() );
+ assertEquals( "2.0", assertion.getVersion() );
+
+ assertEquals( "http://localhost:8080/idp/",
assertion.getIssuer().getValue() );
+
+ //Subject
+ SubjectType subject = assertion.getSubject();
+ List<JAXBElement<?>> content = subject.getContent();
+
+ int size = content.size();
+
+ for( int i = 0 ; i < size; i++ )
+ {
+ JAXBElement<?> node = content.get(i);
+ Class<?> clazz = node.getDeclaredType();
+
+ if( clazz.equals( NameIDType.class ))
+ {
+ NameIDType subjectNameID = (NameIDType) node.getValue();
+
+ assertEquals( "anil", subjectNameID.getValue() );
+ assertEquals(
"urn:oasis:names:tc:SAML:2.0:nameid-format:persistent",
subjectNameID.getFormat() );
+ }
+
+ else if( clazz.equals( SubjectConfirmationType.class ))
+ {
+ SubjectConfirmationType subjectConfirmation = (SubjectConfirmationType)
node.getValue();
+ assertEquals( "urn:oasis:names:tc:SAML:2.0:cm:bearer",
subjectConfirmation.getMethod() );
+
+ SubjectConfirmationDataType subjectConfirmationData =
subjectConfirmation.getSubjectConfirmationData();
+ assertEquals( "ID_04ded476-d73c-48af-b3a9-232a52905ffb",
subjectConfirmationData.getInResponseTo() );
+ assertEquals( XMLTimeUtil.parse( "2010-11-04T00:19:16.842-05:00" ),
subjectConfirmationData.getNotBefore() );
+ assertEquals( XMLTimeUtil.parse( "2010-11-04T00:19:16.842-05:00"
), subjectConfirmationData.getNotOnOrAfter() );
+ assertEquals( "http://localhost:8080/employee/",
subjectConfirmationData.getRecipient());
+ }
+
+ else if( clazz.equals( AttributeStatementType.class ))
+ {
+ AttributeStatementType attributeStatement = (AttributeStatementType)
node.getValue();
+ List<Object> attributes =
attributeStatement.getAttributeOrEncryptedAttribute();
+ assertEquals( 2, attributes.size() );
+
+ for( Object attr: attributes )
+ {
+ AttributeType attribute = (AttributeType) attr;
+ assertEquals( "role", attribute.getFriendlyName() );
+ assertEquals( "role", attribute.getName() );
+ assertEquals( "role", attribute.getNameFormat() );
+ List<Object> attributeValues = attribute.getAttributeValue();
+ assertEquals( 1, attributeValues.size() );
+
+ String str = (String ) attributeValues.get( 0 );
+ if( ! ( str.equals( "employee") || str.equals(
"manager" )))
+ throw new RuntimeException( "attrib value not found" );
+ }
+ }
+ else
+ throw new RuntimeException( "unknown" );
+ }
+ }
}
\ No newline at end of file
Added:
federation/trunk/picketlink-fed-core/src/test/resources/parser/saml2/saml2-response-assertion-subject.xml
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/resources/parser/saml2/saml2-response-assertion-subject.xml
(rev 0)
+++
federation/trunk/picketlink-fed-core/src/test/resources/parser/saml2/saml2-response-assertion-subject.xml 2010-11-04
20:40:06 UTC (rev 531)
@@ -0,0 +1,36 @@
+<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
+ xmlns="urn:oasis:names:tc:SAML:2.0:assertion"
ID="ID_45df1ea5-81e4-4147-a39a-43a4ef613f4e"
+ Version="2.0" IssueInstant="2010-11-04T00:19:16.847-05:00"
Destination="http://localhost:8080/employee/"
+ InResponseTo="ID_04ded476-d73c-48af-b3a9-232a52905ffb">
+ <Issuer>http://localhost:8080/idp/</Issuer>
+ <samlp:Status>
+ <samlp:StatusCode
Value="urn:oasis:names:tc:SAML:2.0:status:Success"></samlp:StatusCode>
+ </samlp:Status>
+ <saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
+ xmlns="urn:oasis:names:tc:SAML:2.0:assertion"
ID="ID_8be1534d-9155-4837-9f26-70ea2c15e327"
+ Version="2.0" IssueInstant="2010-11-04T00:19:16.842-05:00">
+ <Issuer>http://localhost:8080/idp/</Issuer>
+ <saml:Subject>
+ <saml:NameID
Format="urn:oasis:names:tc:SAML:2.0:nameid-format:persistent">anil</saml:NameID>
+ <saml:SubjectConfirmation
Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
+ <saml:SubjectConfirmationData
+ InResponseTo="ID_04ded476-d73c-48af-b3a9-232a52905ffb"
NotBefore="2010-11-04T00:19:16.842-05:00"
+ NotOnOrAfter="2010-11-04T00:19:16.842-05:00"
Recipient="http://localhost:8080/employee/"></saml:SubjectConfirmationData>
+ </saml:SubjectConfirmation>
+ <saml:AttributeStatement>
+ <saml:Attribute Name="role" FriendlyName="role"
+ NameFormat="role">
+ <saml:AttributeValue
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:type="xs:string">employee
+ </saml:AttributeValue>
+ </saml:Attribute>
+ <saml:Attribute Name="role" FriendlyName="role"
+ NameFormat="role">
+ <saml:AttributeValue
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:type="xs:string">manager
+ </saml:AttributeValue>
+ </saml:Attribute>
+ </saml:AttributeStatement>
+ </saml:Subject>
+ </saml:Assertion>
+</samlp:Response>
\ No newline at end of file