private void signAssertion(Assertion assertion, Credential credential) throws SecurityException, MarshallingException, SignatureException { SignatureBuilder signatureBuilder = (SignatureBuilder) builderFactory .getBuilder(Signature.DEFAULT_ELEMENT_NAME); Signature signature = signatureBuilder.buildObject(); signature.setSigningCredential(credential); SecurityHelper.prepareSignatureParams(signature, credential, null, null); assertion.setSignature(signature); Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(assertion); marshaller.marshall(assertion); Signer.signObject(signature); }
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); }
/** * Marhsall the opensaml {@link XMLObject} to a DOM {@link Element} * * @param xmlObject * the XML object * @return marshalled DOM element */ public static Element marshall(XMLObject xmlObject) { MarshallerFactory marshallerFactory = Configuration .getMarshallerFactory(); Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); try { return marshaller.marshall(xmlObject); } catch (MarshallingException e) { throw new RuntimeException("opensaml2 marshalling error: " + e.getMessage(), e); } }
/** * Unmarshall specified DOM {@link Element} to an opensaml {@link XMLObject} * * @param xmlElement * DOM element * @param <X> * opensaml type * @return the opensaml object. */ @SuppressWarnings({ "unchecked" }) public static <X extends XMLObject> X unmarshall(Element xmlElement) { UnmarshallerFactory unmarshallerFactory = Configuration .getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory .getUnmarshaller(xmlElement); try { return (X) unmarshaller.unmarshall(xmlElement); } catch (UnmarshallingException e) { throw new RuntimeException("opensaml2 unmarshalling " + "error: " + e.getMessage(), e); } }
/** * Retrieves the marshaller for the given XMLObject. 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 marshaller registered under it. * * @param xmlObject the XMLObject to retrieve the marshaller for * * @return the marshaller that can be used for the given XMLObject */ public Marshaller getMarshaller(XMLObject xmlObject) { Marshaller marshaller; marshaller = getMarshaller(xmlObject.getSchemaType()); if (marshaller == null) { marshaller = getMarshaller(xmlObject.getElementQName()); } return marshaller; }
/** * 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; }
/** {@inheritDoc} */ public Element marshall(XMLObject xmlObject) throws MarshallingException { try { Document document = Configuration.getParserPool().newDocument(); return marshall(xmlObject, document); } catch (XMLParserException e) { throw new MarshallingException("Unable to create Document to place marshalled elements in", e); } }
/** * Removes the builder, marshaller, and unmarshaller registered to the given key. * * @param key the key of the builder, marshaller, and unmarshaller to be removed */ public static void deregisterObjectProvider(QName key) { Logger log = getLogger(); log.debug("Unregistering builder, marshaller, and unmarshaller for {}", key); configuredObjectProviders.remove(key); builderFactory.deregisterBuilder(key); marshallerFactory.deregisterMarshaller(key); unmarshallerFactory.deregisterUnmarshaller(key); }
/** {@inheritDoc} */ public Element marshall(XMLObject xmlObject, Document document) throws MarshallingException { if (xmlObject instanceof SignableSAMLObject) { SAMLObjectHelper.declareNonVisibleNamespaces((SignableSAMLObject) xmlObject); } return super.marshall(xmlObject, document); }
public static String encodeSAMLRequest(XMLObject authnRequest) throws MarshallingException, IOException { Marshaller marshaller = Configuration.getMarshallerFactory() .getMarshaller(authnRequest); Element authDOM = marshaller.marshall(authnRequest); StringWriter requestWriter = new StringWriter(); XMLHelper.writeNode(authDOM, requestWriter); String requestMessage = requestWriter.toString(); Deflater deflater = new Deflater(Deflater.DEFLATED, true); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater); deflaterOutputStream.write(requestMessage.getBytes(Charset.forName("UTF-8"))); deflaterOutputStream.close(); String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES); encodedRequestMessage = URLEncoder.encode(encodedRequestMessage, HttpUtils.UTF_8).trim(); return encodedRequestMessage; }
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); }
/** {@inheritDoc} */ public Element marshall(XMLObject xmlObject) throws MarshallingException { try { Document document = Configuration.getParserPool().newDocument(); return marshall(xmlObject, document); } catch (XMLParserException e) { throw new MarshallingException("Unable to create Document to place marshalled elements in", e); } }
/** {@inheritDoc} */ public Element marshall(XMLObject xmlObject, Element parentElement) throws MarshallingException { if (xmlObject instanceof SignableSAMLObject) { SAMLObjectHelper.declareNonVisibleNamespaces((SignableSAMLObject) xmlObject); } return super.marshall(xmlObject, parentElement); }
SecurityHelper.prepareSignatureParams(signature, defaultCredential, null, null); assertion.setSignature(signature); Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(assertion); marshaller.marshall(assertion); Signer.signObject(signature); return assertion;
/** * 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); } }
private String writeEntityDescriptor(EntityDescriptor entityDescriptor) throws ParserConfigurationException, MarshallingException, TransformerException { Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(entityDescriptor); Element element = marshaller.marshall(entityDescriptor); return XMLHelper.nodeToString(element); }
/** * 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); }
DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Marshaller out = Configuration.getMarshallerFactory().getMarshaller(spEntityDescriptor); out.marshall(spEntityDescriptor, document);
/** * 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); } }