Sample code for 30+ languages & platforms
Rust

SOAP WS-Security Username Authentication

See more XML Examples

Demonstrates creating SOAP XML for WS-Security Username Authentication. The client user name and password are encapsulated in a WS-Security <wsse:UsernameToken>.

Chilkat Rust Downloads

Rust
// Generate this XML with the code that follows:

// 	<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
// 	 <soap:Header>	     
// 	  <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2003/06/secext">
// 	   <wsse:UsernameToken wsu:Id="sample" 
// 	       xmlns:wsu="http://schemas.xmlsoap.org/ws/2003/06/utility">
// 	    <wsse:Username>sample</wsse:Username>
// 	    <wsse:Password Type="wsse:PasswordText">oracle</wsse:Password>
// 	    <wsu:Created>2004-05-19T08:44:51Z</wsu:Created>
// 	   </wsse:UsernameToken>
// 	  </wsse:Security>
// 	  <wsse:Security soap:actor="oracle" 
// 	      xmlns:wsse="http://schemas.xmlsoap.org/ws/2003/06/secext">
// 	   <wsse:UsernameToken wsu:Id="oracle" 
// 	       xmlns:wsu="http://schemas.xmlsoap.org/ws/2003/06/utility">
// 	    <wsse:Username>myUsername</wsse:Username>
// 	    <wsse:Password Type="wsse:PasswordText">myPassword</wsse:Password>
// 	    <wsu:Created>2004-05-19T08:46:04Z</wsu:Created>
// 	   </wsse:UsernameToken>
// 	  </wsse:Security>
// 	 </soap:Header>
// 	  <soap:Body>
// 	   <getHello xmlns="http://www.oracle.com"/>
// 	  </soap:Body>
// 	</soap:Envelope>
// 

// First build the outer housing:
let xml = chilkat::Xml::new();
xml.set_tag("soap:Envelope");
let _ = xml.add_attribute("xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");
xml.update_child_content("soap:Header", "");
let _ = xml.update_attr_at("soap:Body|getHello", true, "xmlns", "http://www.oracle.com");

// Get the current date/time in a string with this format: 2004-05-19T08:46:04Z
let dt = chilkat::DateTime::new();
let _ = dt.set_from_current_system_time();
let b_local = false;
let created = dt.get_as_timestamp(b_local).unwrap_or_default();

// Now build each UsernameToken block:
let wsse1 = chilkat::Xml::new();
wsse1.set_tag("wsse:Security");
let _ = wsse1.add_attribute("xmlns:wsse", "http://schemas.xmlsoap.org/ws/2003/06/secext");
let _ = wsse1.update_attr_at("wsse:UsernameToken", true, "wsu:Id", "sample");
let _ = wsse1.update_attr_at("wsse:UsernameToken", true, "xmlns:wsu", "http://schemas.xmlsoap.org/ws/2003/06/utility");
wsse1.update_child_content("wsse:UsernameToken|wsse:Username", "sample");
let _ = wsse1.update_attr_at("wsse:UsernameToken|wsse:Password", true, "Type", "wsse:PasswordText");
wsse1.update_child_content("wsse:UsernameToken|wsse:Password", "oracle");
wsse1.update_child_content("wsse:UsernameToken|wsu:Created", &created);

let wsse2 = chilkat::Xml::new();
wsse2.set_tag("wsse:Security");
let _ = wsse2.add_attribute("soap:actor", "oracle");
let _ = wsse2.add_attribute("xmlns:wsse", "http://schemas.xmlsoap.org/ws/2003/06/secext");
let _ = wsse2.update_attr_at("wsse:UsernameToken", true, "wsu:Id", "oracle");
let _ = wsse2.update_attr_at("wsse:UsernameToken", true, "xmlns:wsu", "http://schemas.xmlsoap.org/ws/2003/06/utility");
wsse2.update_child_content("wsse:UsernameToken|wsse:Username", "oracle");
let _ = wsse2.update_attr_at("wsse:UsernameToken|wsse:Password", true, "Type", "wsse:PasswordText");
wsse2.update_child_content("wsse:UsernameToken|wsse:Password", "oracle");
wsse2.update_child_content("wsse:UsernameToken|wsu:Created", &created);

// Insert the UsernameToken blocks:
let x_header = xml.get_child_with_tag("soap:Header").unwrap();
let _ = x_header.add_child_tree(&wsse1);
let _ = x_header.add_child_tree(&wsse2);

// Show the XML:
println!("{}", xml.get_xml().unwrap_or_default());