Unicode C++
Unicode C++
eHealth.gov.be RequestSecurityToken
Request a security token for use with the eHealth.gov.be SOAP web services.Chilkat Unicode C++ Downloads
#include <CkCertW.h>
#include <CkXmlW.h>
#include <CkBinDataW.h>
#include <CkDateTimeW.h>
#include <CkXmlDSigGenW.h>
#include <CkStringBuilderW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
void ChilkatSample(void)
{
bool 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..
CkCertW cert;
success = cert.LoadPfxFile(L"SSIN=12345678.acc.p12",L"p12_password");
if (success == false) {
wprintf(L"%s\n",cert.lastErrorText());
return;
}
// 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>
CkXmlW xmlToSign;
xmlToSign.put_Tag(L"soapenv:Envelope");
xmlToSign.AddAttribute(L"xmlns:ns",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512");
xmlToSign.AddAttribute(L"xmlns:soapenv",L"http://schemas.xmlsoap.org/soap/envelope/");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security",true,L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security",true,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",true,L"EncodingType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",true,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",true,L"wsu:Id",L"X509-4A13D668E59AAC4F3816750824965588");
CkBinDataW bdCert;
cert.ExportCertDerBd(bdCert);
xmlToSign.UpdateChildContent(L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",bdCert.getEncoded(L"base64"));
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security|wsu:Timestamp",true,L"wsu:Id",L"TS-4A13D668E59AAC4F3816750824965567");
CkDateTimeW dt;
dt.SetFromCurrentSystemTime();
xmlToSign.UpdateChildContent(L"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created",dt.getAsTimestamp(false));
dt.AddSeconds(300);
xmlToSign.UpdateChildContent(L"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires",dt.getAsTimestamp(false));
dt.AddSeconds(-300);
xmlToSign.UpdateAttrAt(L"soapenv:Body",true,L"wsu:Id",L"id-4A13D668E59AAC4F38167508249655911");
xmlToSign.UpdateAttrAt(L"soapenv:Body",true,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
xmlToSign.UpdateAttrAt(L"soapenv:Body|wst:RequestSecurityToken",true,L"Context",L"RC-302613de-a809-46b5-931a-0a55bfca5937");
xmlToSign.UpdateAttrAt(L"soapenv:Body|wst:RequestSecurityToken",true,L"xmlns:auth",L"http://docs.oasis-open.org/wsfed/authorization/200706");
xmlToSign.UpdateAttrAt(L"soapenv:Body|wst:RequestSecurityToken",true,L"xmlns:ds",L"http://www.w3.org/2000/09/xmldsig#");
xmlToSign.UpdateAttrAt(L"soapenv:Body|wst:RequestSecurityToken",true,L"xmlns:wsa",L"http://schemas.xmlsoap.org/ws/2004/08/addressing");
xmlToSign.UpdateAttrAt(L"soapenv:Body|wst:RequestSecurityToken",true,L"xmlns:wsp",L"http://schemas.xmlsoap.org/ws/2004/09/policy");
xmlToSign.UpdateAttrAt(L"soapenv:Body|wst:RequestSecurityToken",true,L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
xmlToSign.UpdateAttrAt(L"soapenv:Body|wst:RequestSecurityToken",true,L"xmlns:wst",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512");
xmlToSign.UpdateChildContent(L"soapenv:Body|wst:RequestSecurityToken|wst:TokenType",L"http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1");
xmlToSign.UpdateChildContent(L"soapenv:Body|wst:RequestSecurityToken|wst:RequestType",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");
success = xmlToSign.UpdateAttrAt(L"soapenv:Body|wst:RequestSecurityToken|wst:Claims",true,L"Dialect",L"http://docs.oasis-open.org/wsfed/authorization/200706/authclaims");
success = xmlToSign.UpdateAttrAt(L"soapenv:Body|wst:RequestSecurityToken|wst:Claims|auth:ClaimType[1]",true,L"Uri",L"urn:be:fgov:ehealth:1.0:certificateholder:person:ssin");
xmlToSign.UpdateChildContent(L"soapenv:Body|wst:RequestSecurityToken|wst:KeyType",L"http://docs.oasis-open.org/ws-sx/wstrust/200512/PublicKey");
CkXmlDSigGenW gen;
gen.put_SigLocation(L"soapenv:Envelope|soapenv:Header|wsse:Security|wsse:BinarySecurityToken");
gen.put_SigLocationMod(1);
gen.put_SigId(L"SIG-4A13D668E59AAC4F38167508249656212");
gen.put_SigNamespacePrefix(L"ds");
gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#");
gen.put_SignedInfoPrefixList(L"soapenv urn urn1");
gen.put_IncNamespacePrefix(L"ec");
gen.put_IncNamespaceUri(L"http://www.w3.org/2001/10/xml-exc-c14n#");
gen.put_SignedInfoCanonAlg(L"EXCL_C14N");
gen.put_SignedInfoDigestMethod(L"sha256");
// Set the KeyInfoId before adding references..
gen.put_KeyInfoId(L"KI-4A13D668E59AAC4F3816750824965589");
// -------- Reference 1 --------
gen.AddSameDocRef(L"TS-4A13D668E59AAC4F3816750824965567",L"sha256",L"EXCL_C14N",L"wsse soapenv urn urn1",L"");
// -------- Reference 2 --------
gen.AddSameDocRef(L"id-4A13D668E59AAC4F38167508249655911",L"sha256",L"EXCL_C14N",L"urn urn1",L"");
// -------- Reference 3 --------
gen.AddSameDocRef(L"X509-4A13D668E59AAC4F3816750824965588",L"sha256",L"EXCL_C14N",L"_EMPTY_",L"");
gen.SetX509Cert(cert,true);
gen.put_KeyInfoType(L"Custom");
// Create the custom KeyInfo XML..
CkXmlW xmlCustomKeyInfo;
xmlCustomKeyInfo.put_Tag(L"wsse:SecurityTokenReference");
xmlCustomKeyInfo.AddAttribute(L"wsu:Id",L"STR-4A13D668E59AAC4F38167508249655810");
xmlCustomKeyInfo.UpdateAttrAt(L"wsse:Reference",true,L"URI",L"#X509-4A13D668E59AAC4F3816750824965588");
xmlCustomKeyInfo.UpdateAttrAt(L"wsse:Reference",true,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
xmlCustomKeyInfo.put_EmitXmlDecl(false);
gen.put_CustomKeyInfoXml(xmlCustomKeyInfo.getXml());
// Load XML to be signed...
CkStringBuilderW sbXml;
xmlToSign.GetXmlSb(sbXml);
gen.put_Behaviors(L"IndentedSignature");
// Sign the XML...
success = gen.CreateXmlDSigSb(sbXml);
if (success == false) {
wprintf(L"%s\n",gen.lastErrorText());
return;
}
// The sbXml is sent as the HTTP request body below..
wprintf(L"%s\n",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..
CkHttpW http;
success = http.SetSslClientCert(cert);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
http.SetRequestHeader(L"Content-Type",L"text/xml");
const wchar_t *url = L"https://services-acpt.ehealth.fgov.be/IAM/SecurityTokenService/v1/RequestSecurityToken";
CkHttpResponseW resp;
success = http.HttpSb(L"POST",url,sbXml,L"utf-8",L"application/xml",resp);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
int responseStatus = resp.get_StatusCode();
wprintf(L"Response Status Code = %d\n",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.
wprintf(L"%s\n",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.
CkBinDataW bdSecToken;
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
bdSecToken.WriteFile(L"qa_data/tokens/ehealth-fgov-be-sectoken.xml");
wprintf(L"OK\n");
}