user1432059
user1432059

Reputation:

Convert SAML2 AuthnRequest to Java classes

I'm using OpenSAML 2.6.5 as SAML2 library. I've not found documentation about how to marshall the string (XML document) representing the AuthnRequest that comes from the Service Provider. Can anyone help me?

Upvotes: 0

Views: 1378

Answers (1)

Ola
Ola

Reputation: 11

To read request from SP, you need to encode and unmarshall the incoming String, like this:

    DefaultBootstrap.bootstrap(); //crucial in SAML2
    byte[] decodedSamlAsBytes = Base64.decode(incomingEncodedSaml);

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();

    Document document = docBuilder.parse(new ByteArrayInputStream(decodedSamlAsBytes));
    Element element = document.getDocumentElement();

    UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
    Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
    XMLObject requestXmlObj = unmarshaller.unmarshall(element);
    AuthnRequest request = (AuthnRequest) requestXmlObj;

Upvotes: 1

Related Questions