Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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:
    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("soap:Envelope");
    xml.addAttribute("xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml.updateChildContent("soap:Header", "");
    xml.updateAttrAt("soap:Body|getHello", true, "xmlns", "http://www.oracle.com") catch {};

    // Get the current date/time in a string with this format: 2004-05-19T08:46:04Z
    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    dt.setFromCurrentSystemTime() catch {};
    const b_local = false;
    const created = try dt.getAsTimestamp(alloc, b_local);

    // Now build each UsernameToken block:
    const wsse1 = try chilkat.Xml.init();
    defer wsse1.deinit();
    wsse1.setTag("wsse:Security");
    wsse1.addAttribute("xmlns:wsse", "http://schemas.xmlsoap.org/ws/2003/06/secext") catch {};
    wsse1.updateAttrAt("wsse:UsernameToken", true, "wsu:Id", "sample") catch {};
    wsse1.updateAttrAt("wsse:UsernameToken", true, "xmlns:wsu", "http://schemas.xmlsoap.org/ws/2003/06/utility") catch {};
    wsse1.updateChildContent("wsse:UsernameToken|wsse:Username", "sample");
    wsse1.updateAttrAt("wsse:UsernameToken|wsse:Password", true, "Type", "wsse:PasswordText") catch {};
    wsse1.updateChildContent("wsse:UsernameToken|wsse:Password", "oracle");
    wsse1.updateChildContent("wsse:UsernameToken|wsu:Created", created);

    const wsse2 = try chilkat.Xml.init();
    defer wsse2.deinit();
    wsse2.setTag("wsse:Security");
    wsse2.addAttribute("soap:actor", "oracle") catch {};
    wsse2.addAttribute("xmlns:wsse", "http://schemas.xmlsoap.org/ws/2003/06/secext") catch {};
    wsse2.updateAttrAt("wsse:UsernameToken", true, "wsu:Id", "oracle") catch {};
    wsse2.updateAttrAt("wsse:UsernameToken", true, "xmlns:wsu", "http://schemas.xmlsoap.org/ws/2003/06/utility") catch {};
    wsse2.updateChildContent("wsse:UsernameToken|wsse:Username", "oracle");
    wsse2.updateAttrAt("wsse:UsernameToken|wsse:Password", true, "Type", "wsse:PasswordText") catch {};
    wsse2.updateChildContent("wsse:UsernameToken|wsse:Password", "oracle");
    wsse2.updateChildContent("wsse:UsernameToken|wsu:Created", created);

    // Insert the UsernameToken blocks:
    const x_header = try xml.getChildWithTag("soap:Header");
    defer x_header.deinit();
    x_header.addChildTree(wsse1) catch {};
    x_header.addChildTree(wsse2) catch {};

    // Show the XML:
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});
}