Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkDateTime.pb"
IncludeFile "CkXml.pb"

Procedure ChilkatExample()

    ; 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:
    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "soap:Envelope")
    CkXml::ckAddAttribute(xml,"xmlns:soap","http://schemas.xmlsoap.org/soap/envelope/")
    CkXml::ckUpdateChildContent(xml,"soap:Header","")
    CkXml::ckUpdateAttrAt(xml,"soap:Body|getHello",1,"xmlns","http://www.oracle.com")

    ; Get the current date/time in a string with this format: 2004-05-19T08:46:04Z
    dt.i = CkDateTime::ckCreate()
    If dt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDateTime::ckSetFromCurrentSystemTime(dt)
    bLocal.i = 0
    created.s = CkDateTime::ckGetAsTimestamp(dt,bLocal)

    ; Now build each UsernameToken block:
    wsse1.i = CkXml::ckCreate()
    If wsse1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(wsse1, "wsse:Security")
    CkXml::ckAddAttribute(wsse1,"xmlns:wsse","http://schemas.xmlsoap.org/ws/2003/06/secext")
    CkXml::ckUpdateAttrAt(wsse1,"wsse:UsernameToken",1,"wsu:Id","sample")
    CkXml::ckUpdateAttrAt(wsse1,"wsse:UsernameToken",1,"xmlns:wsu","http://schemas.xmlsoap.org/ws/2003/06/utility")
    CkXml::ckUpdateChildContent(wsse1,"wsse:UsernameToken|wsse:Username","sample")
    CkXml::ckUpdateAttrAt(wsse1,"wsse:UsernameToken|wsse:Password",1,"Type","wsse:PasswordText")
    CkXml::ckUpdateChildContent(wsse1,"wsse:UsernameToken|wsse:Password","oracle")
    CkXml::ckUpdateChildContent(wsse1,"wsse:UsernameToken|wsu:Created",created)

    wsse2.i = CkXml::ckCreate()
    If wsse2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(wsse2, "wsse:Security")
    CkXml::ckAddAttribute(wsse2,"soap:actor","oracle")
    CkXml::ckAddAttribute(wsse2,"xmlns:wsse","http://schemas.xmlsoap.org/ws/2003/06/secext")
    CkXml::ckUpdateAttrAt(wsse2,"wsse:UsernameToken",1,"wsu:Id","oracle")
    CkXml::ckUpdateAttrAt(wsse2,"wsse:UsernameToken",1,"xmlns:wsu","http://schemas.xmlsoap.org/ws/2003/06/utility")
    CkXml::ckUpdateChildContent(wsse2,"wsse:UsernameToken|wsse:Username","oracle")
    CkXml::ckUpdateAttrAt(wsse2,"wsse:UsernameToken|wsse:Password",1,"Type","wsse:PasswordText")
    CkXml::ckUpdateChildContent(wsse2,"wsse:UsernameToken|wsse:Password","oracle")
    CkXml::ckUpdateChildContent(wsse2,"wsse:UsernameToken|wsu:Created",created)

    ; Insert the UsernameToken blocks:
    xHeader.i = CkXml::ckGetChildWithTag(xml,"soap:Header")
    CkXml::ckAddChildTree(xHeader,wsse1)
    CkXml::ckAddChildTree(xHeader,wsse2)
    CkXml::ckDispose(xHeader)

    ; Show the XML:
    Debug CkXml::ckGetXml(xml)


    CkXml::ckDispose(xml)
    CkDateTime::ckDispose(dt)
    CkXml::ckDispose(wsse1)
    CkXml::ckDispose(wsse2)


    ProcedureReturn
EndProcedure