Sample code for 30+ languages & platforms
Xojo Plugin

eHealth.gov.be RequestSecurityToken

Request a security token for use with the eHealth.gov.be SOAP web services.

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Provide a certificate + private key.
// Note: If your certificate + private key is located on a hardware token or smartcard, you can call a different function to load from smartcard..
Dim cert As New Chilkat.Cert
success = cert.LoadPfxFile("SSIN=12345678.acc.p12","p12_password")
If (success = False) Then
    System.DebugLog(cert.LastErrorText)
    Return
End If

// Create the following XML to be signed..

// <?xml version="1.0" encoding="UTF-8"?>
// <soapenv:Envelope xmlns:ns="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
// 	<soapenv:Header>
// 		<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
// 		xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
// 			<wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" 
// 			    ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" 
// 			    wsu:Id="X509-4A13D668E59AAC4F3816750824965588">{organization certificate}</wsse:BinarySecurityToken>			
// 			<wsu:Timestamp wsu:Id="TS-4A13D668E59AAC4F3816750824965567">
// 				<wsu:Created>2023-02-01T12:42:11.156Z</wsu:Created>
// 				<wsu:Expires>2023-02-01T12:58:51.156Z</wsu:Expires>
// 			</wsu:Timestamp>
// 		</wsse:Security>
// 	</soapenv:Header>
// 	<soapenv:Body wsu:Id="id-4A13D668E59AAC4F38167508249655911" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
// 		<wst:RequestSecurityToken Context="RC-302613de-a809-46b5-931a-0a55bfca5937" 
// 		    xmlns:auth="http://docs.oasis-open.org/wsfed/authorization/200706"
// 		    xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
// 		    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
// 		    xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
// 		    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
// 		    xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
// 			<wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</wst:TokenType>
// 			<wst:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</wst:RequestType>
// 			<wst:Claims Dialect="http://docs.oasis-open.org/wsfed/authorization/200706/authclaims">
// 				<auth:ClaimType Uri="urn:be:fgov:kbo-bce:organization:cbe-number">
// 					<auth:Value>{cbenumber}</auth:Value>
// 				</auth:ClaimType>
// 				<auth:ClaimType Uri="urn:be:fgov:ehealth:1.0:certificateholder:enterprise:cbe-number">
// 					<auth:Value>{cbenumber}</auth:Value>
// 				</auth:ClaimType>
// 			</wst:Claims>
// 			<wst:Lifetime>
// 				<wsu:Created>2023-02-01T08:30:10+02:00</wsu:Created>
// 				<wsu:Expires>2023-02-01T09:30:10+02:00</wsu:Expires>
// 			</wst:Lifetime>
// 			<wst:KeyType>http://docs.oasis-open.org/ws-sx/wstrust/200512/PublicKey</wst:KeyType>
// 		</wst:RequestSecurityToken>
// 	</soapenv:Body>
// </soapenv:Envelope>

Dim xmlToSign As New Chilkat.Xml
xmlToSign.Tag = "soapenv:Envelope"
success = xmlToSign.AddAttribute("xmlns:ns","http://docs.oasis-open.org/ws-sx/ws-trust/200512")
success = xmlToSign.AddAttribute("xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/")
success = xmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",True,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
success = xmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security",True,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
success = xmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken",True,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
success = xmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken",True,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3")
success = xmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken",True,"wsu:Id","X509-4A13D668E59AAC4F3816750824965588")

Dim bdCert As New Chilkat.BinData
success = cert.ExportCertDerBd(bdCert)
xmlToSign.UpdateChildContent "soapenv:Header|wsse:Security|wsse:BinarySecurityToken",bdCert.GetEncoded("base64")

success = xmlToSign.UpdateAttrAt("soapenv:Header|wsse:Security|wsu:Timestamp",True,"wsu:Id","TS-4A13D668E59AAC4F3816750824965567")

Dim dt As New Chilkat.CkDateTime
success = dt.SetFromCurrentSystemTime()
xmlToSign.UpdateChildContent "soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created",dt.GetAsTimestamp(False)
success = dt.AddSeconds(300)
xmlToSign.UpdateChildContent "soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires",dt.GetAsTimestamp(False)
success = dt.AddSeconds(-300)

success = xmlToSign.UpdateAttrAt("soapenv:Body",True,"wsu:Id","id-4A13D668E59AAC4F38167508249655911")
success = xmlToSign.UpdateAttrAt("soapenv:Body",True,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
success = xmlToSign.UpdateAttrAt("soapenv:Body|wst:RequestSecurityToken",True,"Context","RC-302613de-a809-46b5-931a-0a55bfca5937")
success = xmlToSign.UpdateAttrAt("soapenv:Body|wst:RequestSecurityToken",True,"xmlns:auth","http://docs.oasis-open.org/wsfed/authorization/200706")
success = xmlToSign.UpdateAttrAt("soapenv:Body|wst:RequestSecurityToken",True,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#")
success = xmlToSign.UpdateAttrAt("soapenv:Body|wst:RequestSecurityToken",True,"xmlns:wsa","http://schemas.xmlsoap.org/ws/2004/08/addressing")
success = xmlToSign.UpdateAttrAt("soapenv:Body|wst:RequestSecurityToken",True,"xmlns:wsp","http://schemas.xmlsoap.org/ws/2004/09/policy")
success = xmlToSign.UpdateAttrAt("soapenv:Body|wst:RequestSecurityToken",True,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
success = xmlToSign.UpdateAttrAt("soapenv:Body|wst:RequestSecurityToken",True,"xmlns:wst","http://docs.oasis-open.org/ws-sx/ws-trust/200512")
xmlToSign.UpdateChildContent "soapenv:Body|wst:RequestSecurityToken|wst:TokenType","http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1"
xmlToSign.UpdateChildContent "soapenv:Body|wst:RequestSecurityToken|wst:RequestType","http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue"

success = xmlToSign.UpdateAttrAt("soapenv:Body|wst:RequestSecurityToken|wst:Claims",True,"Dialect","http://docs.oasis-open.org/wsfed/authorization/200706/authclaims")
success = xmlToSign.UpdateAttrAt("soapenv:Body|wst:RequestSecurityToken|wst:Claims|auth:ClaimType[1]",True,"Uri","urn:be:fgov:ehealth:1.0:certificateholder:person:ssin")

xmlToSign.UpdateChildContent "soapenv:Body|wst:RequestSecurityToken|wst:KeyType","http://docs.oasis-open.org/ws-sx/wstrust/200512/PublicKey"

Dim gen As New Chilkat.XmlDSigGen

gen.SigLocation = "soapenv:Envelope|soapenv:Header|wsse:Security|wsse:BinarySecurityToken"
gen.SigLocationMod = 1
gen.SigId = "SIG-4A13D668E59AAC4F38167508249656212"
gen.SigNamespacePrefix = "ds"
gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
gen.SignedInfoPrefixList = "soapenv urn urn1"
gen.IncNamespacePrefix = "ec"
gen.IncNamespaceUri = "http://www.w3.org/2001/10/xml-exc-c14n#"
gen.SignedInfoCanonAlg = "EXCL_C14N"
gen.SignedInfoDigestMethod = "sha256"

// Set the KeyInfoId before adding references..
gen.KeyInfoId = "KI-4A13D668E59AAC4F3816750824965589"

// -------- Reference 1 --------
success = gen.AddSameDocRef("TS-4A13D668E59AAC4F3816750824965567","sha256","EXCL_C14N","wsse soapenv urn urn1","")

// -------- Reference 2 --------
success = gen.AddSameDocRef("id-4A13D668E59AAC4F38167508249655911","sha256","EXCL_C14N","urn urn1","")

// -------- Reference 3 --------
success = gen.AddSameDocRef("X509-4A13D668E59AAC4F3816750824965588","sha256","EXCL_C14N","_EMPTY_","")

success = gen.SetX509Cert(cert,True)

gen.KeyInfoType = "Custom"

// Create the custom KeyInfo XML..
Dim xmlCustomKeyInfo As New Chilkat.Xml
xmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference"
success = xmlCustomKeyInfo.AddAttribute("wsu:Id","STR-4A13D668E59AAC4F38167508249655810")
success = xmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",True,"URI","#X509-4A13D668E59AAC4F3816750824965588")
success = xmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",True,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3")

xmlCustomKeyInfo.EmitXmlDecl = False
gen.CustomKeyInfoXml = xmlCustomKeyInfo.GetXml()

// Load XML to be signed...
Dim sbXml As New Chilkat.StringBuilder
success = xmlToSign.GetXmlSb(sbXml)

gen.Behaviors = "IndentedSignature"

// Sign the XML...
success = gen.CreateXmlDSigSb(sbXml)
If (success = False) Then
    System.DebugLog(gen.LastErrorText)
    Return
End If

// The sbXml is sent as the HTTP request body below..
System.DebugLog(sbXml.GetAsString())

// -----------------------------------------------------------------------------------------------------------
// Send the SOAP requet to ask the server to issue a security token, which can then be used to access other SOAP services..
Dim http As New Chilkat.Http

success = http.SetSslClientCert(cert)
If (success = False) Then
    System.DebugLog(http.LastErrorText)
    Return
End If

http.SetRequestHeader "Content-Type","text/xml"

Dim url As String
url = "https://services-acpt.ehealth.fgov.be/IAM/SecurityTokenService/v1/RequestSecurityToken"
Dim resp As New Chilkat.HttpResponse
success = http.HttpSb("POST",url,sbXml,"utf-8","application/xml",resp)
If (success = False) Then
    System.DebugLog(http.LastErrorText)
    Return
End If

Dim responseStatus As Int32
responseStatus = resp.StatusCode
System.DebugLog("Response Status Code = " + Str(responseStatus))

// You'll want to check to see if the response status code = 200.
// If not, then the response body contains error information instead of a security token.
// This example will assume we received 200 status code.

System.DebugLog(resp.BodyStr)

// The response body contains XML like this:

// <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
//     <SOAP-ENV:Header/>
//     <SOAP-ENV:Body>
//         <wst:RequestSecurityTokenResponse xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512" Context="RC-302613de-a809-46b5-931a-0a55bfca5937">
//             <wst:RequestedSecurityToken>
//                 <Assertion xmlns="urn:oasis:names:tc:SAML:1.0:assertion" AssertionID="_3e8ea5c951b2167c274974750ace9b5d" 
// ...
//                 </Assertion>
//             </wst:RequestedSecurityToken>
//         </wst:RequestSecurityTokenResponse>
//     </SOAP-ENV:Body>
// </SOAP-ENV:Envelope>

// The portion of the response from  <Assertion ..> ... </Assertion>  is the SAML security token to be included
// in a subsesquent SOAP request.  It is extremely important to not modify the contents of the security token in any way, including not changing
// whitespace or any formatting.  Therefore, we get the response body exactly as-is, to be used in a SOAP request.

// Copy the response body to a Chilkat BinData object.
Dim bdSecToken As New Chilkat.BinData
success = resp.GetBodyBd(bdSecToken)

// Let's save the bdSecToken to a file, and pick it up in the next example where it is used
// in a SOAP request, such as in this example:  AddressBook Search for Professionals

success = bdSecToken.WriteFile("qa_data/tokens/ehealth-fgov-be-sectoken.xml")

System.DebugLog("OK")