Swift
Swift
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 Swift Downloads
func chilkatTest() {
// 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 = CkoXml()!
xml.tag = "soap:Envelope"
xml.addAttribute(name: "xmlns:soap", value: "http://schemas.xmlsoap.org/soap/envelope/")
xml.updateChildContent(tagPath: "soap:Header", value: "")
xml.updateAttrAt(tagPath: "soap:Body|getHello", autoCreate: true, attrName: "xmlns", attrValue: "http://www.oracle.com")
// Get the current date/time in a string with this format: 2004-05-19T08:46:04Z
let dt = CkoDateTime()!
dt.setFromCurrentSystemTime()
var bLocal: Bool = false
var created: String? = dt.get(asTimestamp: bLocal)
// Now build each UsernameToken block:
let wsse1 = CkoXml()!
wsse1.tag = "wsse:Security"
wsse1.addAttribute(name: "xmlns:wsse", value: "http://schemas.xmlsoap.org/ws/2003/06/secext")
wsse1.updateAttrAt(tagPath: "wsse:UsernameToken", autoCreate: true, attrName: "wsu:Id", attrValue: "sample")
wsse1.updateAttrAt(tagPath: "wsse:UsernameToken", autoCreate: true, attrName: "xmlns:wsu", attrValue: "http://schemas.xmlsoap.org/ws/2003/06/utility")
wsse1.updateChildContent(tagPath: "wsse:UsernameToken|wsse:Username", value: "sample")
wsse1.updateAttrAt(tagPath: "wsse:UsernameToken|wsse:Password", autoCreate: true, attrName: "Type", attrValue: "wsse:PasswordText")
wsse1.updateChildContent(tagPath: "wsse:UsernameToken|wsse:Password", value: "oracle")
wsse1.updateChildContent(tagPath: "wsse:UsernameToken|wsu:Created", value: created)
let wsse2 = CkoXml()!
wsse2.tag = "wsse:Security"
wsse2.addAttribute(name: "soap:actor", value: "oracle")
wsse2.addAttribute(name: "xmlns:wsse", value: "http://schemas.xmlsoap.org/ws/2003/06/secext")
wsse2.updateAttrAt(tagPath: "wsse:UsernameToken", autoCreate: true, attrName: "wsu:Id", attrValue: "oracle")
wsse2.updateAttrAt(tagPath: "wsse:UsernameToken", autoCreate: true, attrName: "xmlns:wsu", attrValue: "http://schemas.xmlsoap.org/ws/2003/06/utility")
wsse2.updateChildContent(tagPath: "wsse:UsernameToken|wsse:Username", value: "oracle")
wsse2.updateAttrAt(tagPath: "wsse:UsernameToken|wsse:Password", autoCreate: true, attrName: "Type", attrValue: "wsse:PasswordText")
wsse2.updateChildContent(tagPath: "wsse:UsernameToken|wsse:Password", value: "oracle")
wsse2.updateChildContent(tagPath: "wsse:UsernameToken|wsu:Created", value: created)
// Insert the UsernameToken blocks:
var xHeader: CkoXml? = xml.getChild(withTag: "soap:Header")
xHeader!.addChildTree(tree: wsse1)
xHeader!.addChildTree(tree: wsse2)
xHeader = nil
// Show the XML:
print("\(xml.getXml()!)")
}