/*******************************************************************************
* Copyright 2014 Federal Chancellery Austria
* MOA-ID has been developed in a cooperation between BRZ, the Federal
* Chancellery Austria - ICT staff unit, and Graz University of Technology.
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
* http://www.osor.eu/eupl/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* This product combines work with different licenses. See the "NOTICE" text
* file for details on the various modules and licenses.
* The "NOTICE" text file is part of the distribution. Any derivative works
* that you distribute must include a readable copy of the "NOTICE" text file.
******************************************************************************/
package at.gv.egovernment.moa.id.auth.builder;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
import javax.xml.transform.TransformerException;
import org.w3c.dom.Element;
import at.gv.egovernment.moa.id.auth.data.ExtendedSAMLAttribute;
import at.gv.egovernment.moa.id.auth.exception.ParseException;
import at.gv.egovernment.moa.logging.Logger;
import at.gv.egovernment.moa.util.DOMUtils;
import at.gv.egovernment.moa.util.StringUtils;
/**
* Base class for building authentication the AUTHBlock and final OA data SAML assertions.
* Encapsulates methods used by the two specific builders
* {@link at.gv.egovernment.moa.id.auth.builder.AuthenticationBlockAssertionBuilder AuthenticationBlockAssertionBuilder}
* and
* {@link at.gv.egovernment.moa.id.auth.builder.AuthenticationDataAssertionBuilder AuthenticationDataAssertionBuilder}
*
* @author Harald Bratko
*/
public class AuthenticationAssertionBuilder {
/** the NewLine representation in Java*/
protected static String NL = "\n";
protected static String SAML_ATTRIBUTE =
" " + NL +
" {2}" + NL +
" "+ NL;
protected static String SAML_ATTRIBUTE_NO_NAMESPACE =
" " + NL +
" {1}" + NL +
" "+ NL;
/**
* Empty constructor
*/
public AuthenticationAssertionBuilder() {
}
/**
* Builds the SAML attributes to be appended to the AUTHBlock or to the SAML assertion
* delivered to the online application.
* The method traverses through the list of given SAML attribute objects and builds an
* XML structure (String representation) for each of the attributes.
*
* @param extendedSAMLAttributes The SAML attributes to be appended to the AUTHBlock or
* to the SAML assertion delivered to the online application.
* @return A string representation including the XML structures of
* the SAML attributes.
*
* @throws ParseException If an error occurs on serializing an SAML attribute.
*/
protected String buildExtendedSAMLAttributes(List extendedSAMLAttributes) throws ParseException
{
StringBuffer sb = new StringBuffer();
if (extendedSAMLAttributes!=null) {
Iterator it = extendedSAMLAttributes.iterator();
while (it.hasNext()) {
ExtendedSAMLAttribute extendedSAMLAttribute = (ExtendedSAMLAttribute)it.next();
Object value = extendedSAMLAttribute.getValue();
String name = extendedSAMLAttribute.getName();
String namespace = extendedSAMLAttribute.getNameSpace();
if (value instanceof String) {
sb.append(MessageFormat.format( SAML_ATTRIBUTE, new Object[] {name, namespace, value}));
} else if (value instanceof List>) {
if (!((List>)value).isEmpty()) {
Object firstEl = ((List>)value).get(0);
if (firstEl instanceof String) {
sb.append(MessageFormat.format( SAML_ATTRIBUTE, new Object[] {name, namespace, ((String)firstEl)}));
}
}
} else if (value instanceof Element) {
try {
String serializedValue = DOMUtils.serializeNode((Element)(value));
serializedValue = StringUtils.removeXMLDeclaration(serializedValue);
sb.append(MessageFormat.format( SAML_ATTRIBUTE, new Object[] {name, namespace, serializedValue}));
} catch (TransformerException e) {
Logger.error("Error on serializing SAML attribute \"" + name +
" (namespace: \"" + namespace + "\".");
throw new ParseException("parser.05", new Object[] { name, namespace});
} catch (IOException e) {
Logger.error("Error on serializing SAML attribute \"" + name +
" (namespace: \"" + namespace + "\".");
throw new ParseException("parser.05", new Object[] { name, namespace});
}
}
}
}
return sb.toString();
}
}