/** * Parse a document from the string * * @param docString * @return * @throws IOException * @throws SAXException * @throws ParserConfigurationException */ public static Document getDocument(String docString) throws ConfigurationException, ParsingException, ProcessingException { return getDocument(new StringReader(docString)); }
/** * Get the document as a string while ignoring any exceptions * * @param doc * @return */ public static String asString(Document doc) { String str = null; try { str = getDocumentAsString(doc); } catch (Exception ignore) { } return str; }
public Source invoke(Source request) { try { Document doc = (Document) DocumentUtil.getNodeFromSource(request); if (log.isDebugEnabled()) { log.debug("Received Message::" + DocumentUtil.asString(doc)); } XACMLAuthzDecisionQueryType xacmlQuery = SOAPSAMLXACMLUtil.getXACMLQueryType(doc); ResponseType samlResponseType = SOAPSAMLXACMLUtil.handleXACMLQuery(pdp, issuer, xacmlQuery); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter xmlStreamWriter = StaxUtil.getXMLStreamWriter(baos); SAMLResponseWriter samlResponseWriter = new SAMLResponseWriter(xmlStreamWriter); samlResponseWriter.write(samlResponseType); Document responseDocument = DocumentUtil.getDocument(new ByteArrayInputStream(baos.toByteArray())); return new DOMSource(responseDocument.getDocumentElement()); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Signs a SAML assertion using the given security {@link KeyPair}. * @param assertion * @param keypair */ public static String signSAMLAssertion(String assertion, KeyPair keypair) { try { Document samlDocument = DocumentUtil.getDocument(assertion); SAML2Signature sig = new SAML2Signature(); sig.signSAMLDocument(samlDocument, keypair); return DocumentUtil.getDocumentAsString(samlDocument); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Get an encrypted assertion from the stream * @param is * @return * @throws ParsingException * @throws ProcessingException * @throws ConfigurationException */ public EncryptedAssertionType getEncryptedAssertion(InputStream is) throws ParsingException, ConfigurationException, ProcessingException { if (is == null) throw new IllegalArgumentException(ErrorCodes.NULL_ARGUMENT + "inputstream"); Document samlDocument = DocumentUtil.getDocument(is); SAMLParser samlParser = new SAMLParser(); JAXPValidationUtil.checkSchemaValidation(samlDocument); return (EncryptedAssertionType) samlParser.parse(DocumentUtil.getNodeAsStream(samlDocument)); }
/** * Read a {@code SAML2Object} from an input stream * @param is * @return * @throws ParsingException * @throws ConfigurationException * @throws ProcessingException */ public SAML2Object getSAML2ObjectFromStream(InputStream is) throws ParsingException, ConfigurationException, ProcessingException { if (is == null) throw new IllegalArgumentException(ErrorCodes.NULL_ARGUMENT + "inputstream"); Document samlResponseDocument = DocumentUtil.getDocument(is); if (trace) log.trace("RESPONSE=" + DocumentUtil.asString(samlResponseDocument)); SAMLParser samlParser = new SAMLParser(); JAXPValidationUtil.checkSchemaValidation(samlResponseDocument); InputStream responseStream = DocumentUtil.getNodeAsStream(samlResponseDocument); SAML2Object responseType = (SAML2Object) samlParser.parse(responseStream); samlDocumentHolder = new SAMLDocumentHolder(responseType, samlResponseDocument); return responseType; }
/** * <p> * Utility method that marshals the specified {@code AssertionType} object into an {@code Element} instance. * </p> * * @param assertion * an {@code AssertionType} object representing the SAML assertion to be marshaled. * @return a reference to the {@code Element} that contains the marshaled SAML assertion. * @throws Exception * if an error occurs while marshaling the assertion. */ public static Element toElement(AssertionType assertion) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SAMLAssertionWriter writer = new SAMLAssertionWriter(StaxUtil.getXMLStreamWriter(baos)); writer.write(assertion); ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); Document document = DocumentUtil.getDocument(bis); if (trace) { log.trace("Written Assertion=" + DocumentUtil.asString(document)); } return document.getDocumentElement(); }
byte[] responseBytes = DocumentUtil.getDocumentAsString(responseDoc).getBytes("UTF-8"); } else { if (logger.isTraceEnabled()) { logger.trace("SAML Response Document: " + DocumentUtil.asString(responseDoc)); byte[] responseBytes = DocumentUtil.getDocumentAsString(responseDoc).getBytes("UTF-8");
if (trace) log.trace("Document to be signed=" + DocumentUtil.asString(doc)); Document newDoc = DocumentUtil.createDocument(); && WSTrustConstants.SAML2_ASSERTION_NS.equals(nodeToBeSigned.getNamespaceURI())) Node signatureNode = DocumentUtil.getElement(newDoc, new QName(WSTrustConstants.DSIG_NS, "Signature")); Node subjectNode = DocumentUtil.getElement(newDoc, new QName(WSTrustConstants.SAML2_ASSERTION_NS, "Subject")); if (signatureNode != null && subjectNode != null)
private ResponseType decryptAssertion(ResponseType responseType, PrivateKey privateKey) throws ProcessingException { if (privateKey == null) throw new IllegalArgumentException(ErrorCodes.NULL_ARGUMENT + "privateKey"); SAML2Response saml2Response = new SAML2Response(); try { Document doc = saml2Response.convert(responseType); Element enc = DocumentUtil.getElement(doc, new QName(JBossSAMLConstants.ENCRYPTED_ASSERTION.get())); if (enc == null) throw new ProcessingException(ErrorCodes.NULL_VALUE + "Null encrypted assertion element"); String oldID = enc.getAttribute(JBossSAMLConstants.ID.get()); Document newDoc = DocumentUtil.createDocument(); Node importedNode = newDoc.importNode(enc, true); newDoc.appendChild(importedNode); Element decryptedDocumentElement = XMLEncryptionUtil.decryptElementInDocument(newDoc, privateKey); SAMLParser parser = new SAMLParser(); JAXPValidationUtil.checkSchemaValidation(decryptedDocumentElement); AssertionType assertion = (AssertionType) parser.parse(StaxParserUtil.getXMLEventReader(DocumentUtil .getNodeAsStream(decryptedDocumentElement))); responseType.replaceAssertion(oldID, new RTChoiceType(assertion)); return responseType; } catch (Exception e) { throw new ProcessingException(e); } }
/** * Convert an EncryptedElement into a Document * @param encryptedElementType * @return * @throws ConfigurationException */ public Document convert(EncryptedElementType encryptedElementType) throws ConfigurationException { if (encryptedElementType == null) throw new IllegalArgumentException(ErrorCodes.NULL_ARGUMENT + "encryptedElementType"); Document doc = DocumentUtil.createDocument(); Node importedNode = doc.importNode(encryptedElementType.getEncryptedElement(), true); doc.appendChild(importedNode); return doc; }
/** * Sign the root element * * @param doc * @param signingKey * @param publicKey * @param digestMethod * @param signatureMethod * @param referenceURI * @return * @throws GeneralSecurityException * @throws XMLSignatureException * @throws MarshalException * @since 2.5.0 */ public static Document sign(Document doc, KeyPair keyPair, String digestMethod, String signatureMethod, String referenceURI, X509Certificate x509Certificate) throws GeneralSecurityException, MarshalException, XMLSignatureException { logger.trace("Document to be signed=" + DocumentUtil.asString(doc)); PrivateKey signingKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); DOMSignContext dsc = new DOMSignContext(signingKey, doc.getDocumentElement()); signImpl(dsc, digestMethod, signatureMethod, referenceURI, publicKey, x509Certificate); return doc; }
public SAML11AssertionType fromElement(Element element) throws ConfigurationException, ProcessingException, ParsingException { XMLEventReader xmlEventReader = StaxParserUtil.getXMLEventReader(DocumentUtil.getNodeAsStream(element)); return (SAML11AssertionType) parse(xmlEventReader); }
throw new IllegalArgumentException(ErrorCodes.WRONG_TYPE + "Wrapping element prefix invalid"); Element documentElement = DocumentUtil.getElement(document, elementQName);
if (StringUtil.isNotNull(containsBaseNamespace)) el = DocumentUtil.createDocumentWithBaseNamespace(containsBaseNamespace, localPart).getDocumentElement(); el = (Element) doc.importNode(el, true);
/** * <p> * Utility method that marshals the specified {@code AssertionType} object into an {@code Element} instance. * </p> * * @param assertion * an {@code AssertionType} object representing the SAML assertion to be marshaled. * @return a reference to the {@code Element} that contains the marshaled SAML assertion. * @throws Exception * if an error occurs while marshaling the assertion. */ public static Element toElement(AssertionType assertion) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SAMLAssertionWriter writer = new SAMLAssertionWriter(StaxUtil.getXMLStreamWriter(baos)); writer.write(assertion); ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); Document document = DocumentUtil.getDocument(bis); if (trace) { log.trace("Written Assertion=" + DocumentUtil.asString(document)); } return document.getDocumentElement(); }
/** * Read an assertion from an input stream * @param is * @return * @throws ParsingException * @throws ProcessingException * @throws ConfigurationException */ public AssertionType getAssertionType(InputStream is) throws ParsingException, ConfigurationException, ProcessingException { if (is == null) throw new IllegalArgumentException(ErrorCodes.NULL_ARGUMENT + "inputstream"); Document samlDocument = DocumentUtil.getDocument(is); SAMLParser samlParser = new SAMLParser(); JAXPValidationUtil.checkSchemaValidation(samlDocument); return (AssertionType) samlParser.parse(DocumentUtil.getNodeAsStream(samlDocument)); }
byte[] responseBytes = DocumentUtil.getDocumentAsString(responseDoc).getBytes("UTF-8"); } else { if (logger.isTraceEnabled()) { logger.trace("SAML Response Document: " + DocumentUtil.asString(responseDoc)); byte[] responseBytes = DocumentUtil.getDocumentAsString(responseDoc).getBytes("UTF-8");
/** * Read a {@code SAML2Object} from an input stream * * @param is * @return * @throws ParsingException * @throws ConfigurationException * @throws ProcessingException */ public SAML2Object getSAML2ObjectFromStream(InputStream is) throws ParsingException, ConfigurationException, ProcessingException { if (is == null) throw logger.nullArgumentError("InputStream"); Document samlResponseDocument = DocumentUtil.getDocument(is); if (logger.isTraceEnabled()) { logger.trace("SAML Response Document: " + DocumentUtil.asString(samlResponseDocument)); } SAMLParser samlParser = new SAMLParser(); JAXPValidationUtil.checkSchemaValidation(samlResponseDocument); InputStream responseStream = DocumentUtil.getNodeAsStream(samlResponseDocument); SAML2Object responseType = (SAML2Object) samlParser.parse(responseStream); samlDocumentHolder = new SAMLDocumentHolder(responseType, samlResponseDocument); return responseType; }
if (trace) log.trace("Document to be signed=" + DocumentUtil.asString(doc)); Document newDoc = DocumentUtil.createDocument(); && WSTrustConstants.SAML2_ASSERTION_NS.equals(nodeToBeSigned.getNamespaceURI())) Node signatureNode = DocumentUtil.getElement(newDoc, new QName(WSTrustConstants.DSIG_NS, "Signature")); Node subjectNode = DocumentUtil.getElement(newDoc, new QName(WSTrustConstants.SAML2_ASSERTION_NS, "Subject")); if (signatureNode != null && subjectNode != null)