Search code examples
javasamlopensaml

Converting assertion to string in OpenSAML 4


I am trying to follow this example: How to create a valid SAML 2.0 Assertion with OpenSAML library in Java and trying to create a SAML response. The code shown is for an older version of OpenSAML. I am using OpenSAML 4.1.1. Apparently XMLHelper is no longer in v.4. I have searched high and low and not found a solution, at least one that makes sense where I can convert the assertion into a string.

Below is the code I have right out of the sample:

        SAMLInputContainer input = new SAMLInputContainer();
        input.strIssuer = "http://synesty.com";
        input.strNameID = "UserJohnSmith";
        input.strNameQualifier = "My Website";
        input.sessionId = "abcdedf1234567";

        Map<String,String> customAttributes = new HashMap<String, String>();
        customAttributes.put("FirstName", "John");
        customAttributes.put("LastName", "Smith");
        customAttributes.put("Email", "[email protected]");
        customAttributes.put("PhoneNumber", "76373898998");
        customAttributes.put("Locality", "USA");
        customAttributes.put("Username", "John.Smith");

        input.attributes = customAttributes;

        Assertion assertion = GenSAMLAssertion.buildDefaultAssertion(input);
        AssertionMarshaller marshaller = new AssertionMarshaller();
        assert assertion != null;
        Element plaintextElement = marshaller.marshall(assertion);
        String originalAssertionString = XMLHelper.nodeToString(plaintextElement);

        System.out.println("Assertion String: " + originalAssertionString);

Solution

  • The SerializeSupport class is now used to print XML in OpenSAML.

    String originalAssertionString = SerializeSupport.prettyPrintXML(plaintextElement)
    

    As for more current sample code for OpenSAML 4 I have a ongoing series on my blog. As well as accompanying source samples on my Github

    A few years ago I also wrote a book on OpenSAML 3. It has not been updated but there are very few changes between 3 and 4.