Java Code Examples for org.opensaml.xml.util.XMLHelper#writeNode()

The following examples show how to use org.opensaml.xml.util.XMLHelper#writeNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpSOAPClient.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates the request entity that makes up the POST message body.
 * 
 * @param message message to be sent
 * @param charset character set used for the message
 * 
 * @return request entity that makes up the POST message body
 * 
 * @throws SOAPClientException thrown if the message could not be marshalled
 */
protected RequestEntity createRequestEntity(Envelope message, Charset charset) throws SOAPClientException {
    try {
        Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(message);
        ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(arrayOut, charset);

        if (log.isDebugEnabled()) {
            log.debug("Outbound SOAP message is:\n" + XMLHelper.prettyPrintXML(marshaller.marshall(message)));
        }
        XMLHelper.writeNode(marshaller.marshall(message), writer);
        return new ByteArrayRequestEntity(arrayOut.toByteArray(), "text/xml");
    } catch (MarshallingException e) {
        throw new SOAPClientException("Unable to marshall SOAP envelope", e);
    }
}
 
Example 2
Source File: SAMLUtils.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: Main.java    From saml-generator with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	try {
		HashMap<String, List<String>> attributes = new HashMap<String, List<String>>();
		String issuer = null;
		String subject = null;
		String privateKey = null;
		String publicKey = null;
		Integer samlAssertionExpirationDays = null;
		
		Options options = new Options();
		options.addOption("issuer", true, "Issuer for saml assertion");
		options.addOption("subject", true, "Subject of saml assertion");
           options.addOption("email", true, "Email associated with the subject");
           options.addOption("domain", true, "Domain attribute");
		options.addOption("roles", true, "Comma separated list of roles");
		options.addOption("publicKey", true, "Location of public key to decrypt assertion");
		options.addOption("privateKey", true, "Location or private key use to sign assertion");
		options.addOption("samlAssertionExpirationDays", true, "How long before assertion is no longer valid. Can be negative.");
		
		CommandLineParser parser = new GnuParser();
		CommandLine cmd = parser.parse(options, args);

		if (args.length == 0) {
			HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp( "saml-util-1.0", options, true);
			System.exit(1);
		}
	
		issuer = cmd.getOptionValue("issuer");
		subject = cmd.getOptionValue("subject");
		privateKey = cmd.getOptionValue("privateKey");
		publicKey = cmd.getOptionValue("publicKey");

		samlAssertionExpirationDays = cmd.getOptionValue("samlAssertionExpirationDays") != null ? Integer.valueOf(cmd.getOptionValue("samlAssertionExpirationDays")) : null;
		
		if (cmd.getOptionValue("domain") != null)
			attributes.put("domain", Arrays.asList(cmd.getOptionValue("domain")));
		
		if (cmd.getOptionValue("roles") != null)
			attributes.put("roles", Arrays.asList(cmd.getOptionValue("roles").split(",")));

           if (cmd.getOptionValue("email") != null)
               attributes.put("email", Arrays.asList(cmd.getOptionValue("email")));

		SamlAssertionProducer producer = new SamlAssertionProducer();
		producer.setPrivateKeyLocation(privateKey);
		producer.setPublicKeyLocation(publicKey);
		
		Response responseInitial = producer.createSAMLResponse(subject, new DateTime(), "password", attributes, issuer, samlAssertionExpirationDays);
		
		ResponseMarshaller marshaller = new ResponseMarshaller();
		Element element = marshaller.marshall(responseInitial);
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		XMLHelper.writeNode(element, baos);
		String responseStr = new String(baos.toByteArray());
		
		System.out.println(responseStr);
		
	} catch (Throwable t) {
		t.printStackTrace();
	}
}
 
Example 4
Source File: SamlAssertionProducer.java    From saml-generator with Apache License 2.0 4 votes vote down vote up
public Response createSAMLResponse(final String subjectId, final DateTime authenticationTime,
		                           final String credentialType, final HashMap<String, List<String>> attributes, String issuer, Integer samlAssertionDays) {
	
	try {
		DefaultBootstrap.bootstrap();
		
		Signature signature = createSignature();
		Status status = createStatus();
		Issuer responseIssuer = null;
		Issuer assertionIssuer = null;
		Subject subject = null;
		AttributeStatement attributeStatement = null;
		
		if (issuer != null) {
			responseIssuer = createIssuer(issuer);
			assertionIssuer = createIssuer(issuer);
		}
		
		if (subjectId != null) {
			subject = createSubject(subjectId, samlAssertionDays);
		}
		
		if (attributes != null && attributes.size() != 0) {
			attributeStatement = createAttributeStatement(attributes);
		}
		
		AuthnStatement authnStatement = createAuthnStatement(authenticationTime);
		
		Assertion assertion = createAssertion(new DateTime(), subject, assertionIssuer, authnStatement, attributeStatement);
		
		Response response = createResponse(new DateTime(), responseIssuer, status, assertion);
		response.setSignature(signature);
		
		ResponseMarshaller marshaller = new ResponseMarshaller();
		Element element = marshaller.marshall(response);
		
		if (signature != null) {
			Signer.signObject(signature);
		}
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		XMLHelper.writeNode(element, baos);
	
		return response;
		
	} catch (Throwable t) {
		t.printStackTrace();
		return null;
	}
}