private XMLObject unmarshallObject(String xmlString) throws UnmarshallingException, XMLParserException, UnsupportedEncodingException { BasicParserPool parser = new BasicParserPool(); parser.setNamespaceAware(true); /* Base64URL encoded */ byte bytes[] = xmlString.getBytes("utf-8"); if (bytes == null || bytes.length == 0) throw new InsufficientAuthenticationException("Invalid assertion encoding"); Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes)); Document doc = parser.parse(reader); Element samlElement = doc.getDocumentElement(); UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(samlElement); if (unmarshaller == null) { throw new InsufficientAuthenticationException("Unsuccessful to unmarshal assertion string"); } return unmarshaller.unmarshall(samlElement); }
public static Response decodeSAMLResponse(String responseMessage) throws ConfigurationException, ParserConfigurationException, SAXException, IOException, UnmarshallingException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder(); byte[] base64DecodedResponse = Base64.decode(responseMessage); Document document = docBuilder.parse(new ByteArrayInputStream(base64DecodedResponse)); Element element = document.getDocumentElement(); UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element); return (Response) unmarshaller.unmarshall(element); }
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;
/** * Retrieves the unmarshaller for the given element. The schema type, if present, is tried first as the key with the * element QName used if no schema type is present or does not have a unmarshaller registered under it. * * @param domElement the element to retrieve the unmarshaller for * * @return the unmarshaller for the XMLObject the given element can be unmarshalled into */ public Unmarshaller getUnmarshaller(Element domElement) { Unmarshaller unmarshaller; unmarshaller = getUnmarshaller(XMLHelper.getXSIType(domElement)); if (unmarshaller == null) { unmarshaller = getUnmarshaller(XMLHelper.getNodeQName(domElement)); } return unmarshaller; }
/** * Retrieves the unmarshaller for the given element. The schema type, if present, is tried first as the key with the * element QName used if no schema type is present or does not have a unmarshaller registered under it. * * @param domElement the element to retrieve the unmarshaller for * * @return the unmarshaller for the XMLObject the given element can be unmarshalled into */ public Unmarshaller getUnmarshaller(Element domElement) { Unmarshaller unmarshaller; unmarshaller = getUnmarshaller(XMLHelper.getXSIType(domElement)); if (unmarshaller == null) { unmarshaller = getUnmarshaller(XMLHelper.getNodeQName(domElement)); } return unmarshaller; }
/** * Convert a SAML Assertion from a DOM Element to an XMLObject * * @param root of type Element * @return XMLObject * @throws UnmarshallingException */ public static XMLObject fromDom(Element root) throws Exception { Unmarshaller unmarshaller = OpenSamlBootStrap.unmarshallerFactory.getUnmarshaller(root); try { return unmarshaller.unmarshall(root); } catch (UnmarshallingException ex) { throw new Exception("Error unmarshalling a SAML assertion", ex); } }
/** * Constructor. * */ public Encrypter() { UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); encryptedDataUnmarshaller = unmarshallerFactory.getUnmarshaller(EncryptedData.DEFAULT_ELEMENT_NAME); encryptedKeyUnmarshaller = unmarshallerFactory.getUnmarshaller(EncryptedKey.DEFAULT_ELEMENT_NAME); XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory(); keyInfoBuilder = (XMLSignatureBuilder<KeyInfo>) builderFactory.getBuilder(KeyInfo.DEFAULT_ELEMENT_NAME); jcaProviderName = null; }
/** * Constructor. * */ public Encrypter() { UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); encryptedDataUnmarshaller = unmarshallerFactory.getUnmarshaller(EncryptedData.DEFAULT_ELEMENT_NAME); encryptedKeyUnmarshaller = unmarshallerFactory.getUnmarshaller(EncryptedKey.DEFAULT_ELEMENT_NAME); XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory(); keyInfoBuilder = (XMLSignatureBuilder<KeyInfo>) builderFactory.getBuilder(KeyInfo.DEFAULT_ELEMENT_NAME); jcaProviderName = null; }
private Response parseResponse(String authnResponse) throws SAMLException { try { final Document doc = parsers.getBuilder() .parse(new InputSource(new StringReader(authnResponse))); final Element root = doc.getDocumentElement(); return (Response) Configuration.getUnmarshallerFactory() .getUnmarshaller(root) .unmarshall(root); } catch (org.opensaml.xml.parse.XMLParserException | org.opensaml.xml.io.UnmarshallingException | org.xml.sax.SAXException | java.io.IOException e) { throw new SAMLException(e); } }
/** * Creates the SAML object from the element This method must be called first * * @param elem * @throws UnmarshallingException If the token creation fails */ @Override public void createToken(Element elem) throws UnmarshallingException { UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(elem); assertion = (Assertion) unmarshaller.unmarshall(elem); }
/** * Creates the SAML object from the element This method must be called first * * @param elem * @throws UnmarshallingException If the token creation fails */ @Override public void createToken(Element elem) throws UnmarshallingException { UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(elem); assertion = (Assertion) unmarshaller.unmarshall(elem); }
/** * Convert a SAML Assertion from a DOM Element to an XMLObject * * @param root of type Element * @return XMLObject * @throws UnmarshallingException */ public static XMLObject fromDom(Element root) throws Exception { Unmarshaller unmarshaller = OpenSamlBootStrap.getUnmarshallerFactory().getUnmarshaller(root); try { return unmarshaller.unmarshall(root); } catch (UnmarshallingException ex) { throw new Exception("Error unmarshalling a SAML assertion", ex); } }
String inCommonMDFile = "/data/org/opensaml/saml2/metadata/InCommon-metadata.xml"; // Initialize the library DefaultBootstrap.bootstrap(); // Get parser pool manager BasicParserPool ppMgr = new BasicParserPool(); ppMgr.setNamespaceAware(true); // Parse metadata file InputStream in = MetadataTest.class.getResourceAsStream(inCommonMDFile); Document inCommonMDDoc = ppMgr.parse(in); Element metadataRoot = inCommonMDDoc.getDocumentElement(); // Get apropriate unmarshaller UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot); // Unmarshall using the document root element, an EntitiesDescriptor in this case EntitiesDescriptor inCommonMD = (EntitiesDescriptor) unmarshaller.unmarshall(metadataRoot);
/** * Get a KeyInfo unmarshaller. * * @return a KeyInfo unmarshaller * @throws SecurityException thrown if there is an error obtaining the unmarshaller from the configuration */ private Unmarshaller getUnmarshaller() throws SecurityException { if (keyInfoUnmarshaller != null) { return keyInfoUnmarshaller; } keyInfoUnmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(KeyInfo.DEFAULT_ELEMENT_NAME); if (keyInfoUnmarshaller == null) { throw new SecurityException("Could not obtain KeyInfo unmarshaller from the configuration"); } return keyInfoUnmarshaller; }
/** * Constructing the SAML or XACML Objects from a String * * @param xmlString Decoded SAML or XACML String * @return SAML or XACML Object * @throws org.wso2.carbon.identity.base.IdentityException */ public static XMLObject unmarshall(String xmlString) throws IdentityException { try { DocumentBuilderFactory documentBuilderFactory = getSecuredDocumentBuilderFactory(); DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new ByteArrayInputStream(xmlString.trim().getBytes(Charsets.UTF_8))); Element element = document.getDocumentElement(); UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element); return unmarshaller.unmarshall(element); } catch (ParserConfigurationException | UnmarshallingException | SAXException | IOException e) { String message = "Error in constructing XML Object from the encoded String"; throw IdentityException.error(message, e); } }
/** * Get a KeyInfo unmarshaller. * * @return a KeyInfo unmarshaller * @throws SecurityException thrown if there is an error obtaining the unmarshaller from the configuration */ private Unmarshaller getUnmarshaller() throws SecurityException { if (keyInfoUnmarshaller != null) { return keyInfoUnmarshaller; } keyInfoUnmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(KeyInfo.DEFAULT_ELEMENT_NAME); if (keyInfoUnmarshaller == null) { throw new SecurityException("Could not obtain KeyInfo unmarshaller from the configuration"); } return keyInfoUnmarshaller; }
/** * Constructing the SAML or XACML Objects from a String * * @param xmlString Decoded SAML or XACML String * @return SAML or XACML Object * @throws org.wso2.carbon.identity.entitlement.EntitlementException */ public XMLObject unmarshall(String xmlString) throws EntitlementException { try { doBootstrap(); DocumentBuilderFactory documentBuilderFactory = IdentityUtil.getSecuredDocumentBuilderFactory(); DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new ByteArrayInputStream(xmlString.trim().getBytes())); Element element = document.getDocumentElement(); UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element); return unmarshaller.unmarshall(element); } catch (Exception e) { log.error("Error in constructing XML(SAML or XACML) Object from the encoded String", e); throw new EntitlementException("Error in constructing XML(SAML or XACML) from the encoded String ", e); } }
private static Assertion unmarshellElement(OMElement omAssertion) throws Exception{ if(omAssertion.getLocalName().equals("Assertion")){ try{ UnmarshallerFactory factory = Configuration.getUnmarshallerFactory (); javax.xml.namespace.QName qname = omAssertion.getQName(); Unmarshaller unmarsh = factory.getUnmarshaller(qname); if(unmarsh == null){ throw new Exception("Unmarshell Exception"); } Assertion assertion; assertion = (Assertion)unmarsh.unmarshall(XMLUtils.toDOM(omAssertion)); return assertion; } catch(Exception e){ throw e ; } } return null ; }
/** * Convert a SAML Assertion from a DOM Element to an XMLObject * * @param root of type Element * @return XMLObject * @throws UnmarshallingException */ public static XMLObject fromDom(Element root) throws WSSecurityException { if (root == null) { LOG.debug("Attempting to unmarshal a null element!"); throw new WSSecurityException("Error unmarshalling a SAML assertion"); } Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(root); if (unmarshaller == null) { LOG.debug("Unable to find an unmarshaller for element: " + root.getLocalName()); throw new WSSecurityException("Error unmarshalling a SAML assertion"); } try { return unmarshaller.unmarshall(root); } catch (UnmarshallingException ex) { throw new WSSecurityException("Error unmarshalling a SAML assertion", ex); } }
/** {@inheritDoc} */ protected void doInitialization() throws MetadataProviderException { try { Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataElement); XMLObject metadataTemp = unmarshaller.unmarshall(metadataElement); filterMetadata(metadataTemp); releaseMetadataDOM(metadataTemp); metadata = metadataTemp; emitChangeEvent(); } catch (UnmarshallingException e) { String errorMsg = "Unable to unmarshall metadata element"; log.error(errorMsg, e); throw new MetadataProviderException(errorMsg, e); } catch (FilterException e) { String errorMsg = "Unable to filter metadata"; log.error(errorMsg, e); throw new MetadataProviderException(errorMsg, e); } } }