Zig Requires Chilkat v11.0.0+
Zig
eHealth.gov.be RequestSecurityToken
Request a security token for use with the eHealth.gov.be SOAP web services.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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..
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadPfxFile("SSIN=12345678.acc.p12", "p12_password") catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
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>
const xml_to_sign = try chilkat.Xml.init();
defer xml_to_sign.deinit();
xml_to_sign.setTag("soapenv:Envelope");
xml_to_sign.addAttribute("xmlns:ns", "http://docs.oasis-open.org/ws-sx/ws-trust/200512") catch {};
xml_to_sign.addAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security", true, "xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") catch {};
xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") catch {};
xml_to_sign.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") catch {};
xml_to_sign.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") catch {};
xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "wsu:Id", "X509-4A13D668E59AAC4F3816750824965588") catch {};
const bd_cert = try chilkat.BinData.init();
defer bd_cert.deinit();
cert.exportCertDerBd(bd_cert) catch {};
xml_to_sign.updateChildContent("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", try bd_cert.getEncoded(alloc, "base64"));
xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security|wsu:Timestamp", true, "wsu:Id", "TS-4A13D668E59AAC4F3816750824965567") catch {};
const dt = try chilkat.DateTime.init();
defer dt.deinit();
dt.setFromCurrentSystemTime() catch {};
xml_to_sign.updateChildContent("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created", try dt.getAsTimestamp(alloc, false));
dt.addSeconds(300) catch {};
xml_to_sign.updateChildContent("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires", try dt.getAsTimestamp(alloc, false));
dt.addSeconds(-300) catch {};
xml_to_sign.updateAttrAt("soapenv:Body", true, "wsu:Id", "id-4A13D668E59AAC4F38167508249655911") catch {};
xml_to_sign.updateAttrAt("soapenv:Body", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") catch {};
xml_to_sign.updateAttrAt("soapenv:Body|wst:RequestSecurityToken", true, "Context", "RC-302613de-a809-46b5-931a-0a55bfca5937") catch {};
xml_to_sign.updateAttrAt("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:auth", "http://docs.oasis-open.org/wsfed/authorization/200706") catch {};
xml_to_sign.updateAttrAt("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:ds", "http://www.w3.org/2000/09/xmldsig#") catch {};
xml_to_sign.updateAttrAt("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing") catch {};
xml_to_sign.updateAttrAt("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:wsp", "http://schemas.xmlsoap.org/ws/2004/09/policy") catch {};
xml_to_sign.updateAttrAt("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") catch {};
xml_to_sign.updateAttrAt("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:wst", "http://docs.oasis-open.org/ws-sx/ws-trust/200512") catch {};
xml_to_sign.updateChildContent("soapenv:Body|wst:RequestSecurityToken|wst:TokenType", "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1");
xml_to_sign.updateChildContent("soapenv:Body|wst:RequestSecurityToken|wst:RequestType", "http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");
try xml_to_sign.updateAttrAt("soapenv:Body|wst:RequestSecurityToken|wst:Claims", true, "Dialect", "http://docs.oasis-open.org/wsfed/authorization/200706/authclaims");
try xml_to_sign.updateAttrAt("soapenv:Body|wst:RequestSecurityToken|wst:Claims|auth:ClaimType[1]", true, "Uri", "urn:be:fgov:ehealth:1.0:certificateholder:person:ssin");
xml_to_sign.updateChildContent("soapenv:Body|wst:RequestSecurityToken|wst:KeyType", "http://docs.oasis-open.org/ws-sx/wstrust/200512/PublicKey");
const gen = try chilkat.XmlDSigGen.init();
defer gen.deinit();
gen.setSigLocation("soapenv:Envelope|soapenv:Header|wsse:Security|wsse:BinarySecurityToken");
gen.setSigLocationMod(1);
gen.setSigId("SIG-4A13D668E59AAC4F38167508249656212");
gen.setSigNamespacePrefix("ds");
gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
gen.setSignedInfoPrefixList("soapenv urn urn1");
gen.setIncNamespacePrefix("ec");
gen.setIncNamespaceUri("http://www.w3.org/2001/10/xml-exc-c14n#");
gen.setSignedInfoCanonAlg("EXCL_C14N");
gen.setSignedInfoDigestMethod("sha256");
// Set the KeyInfoId before adding references..
gen.setKeyInfoId("KI-4A13D668E59AAC4F3816750824965589");
// -------- Reference 1 --------
gen.addSameDocRef("TS-4A13D668E59AAC4F3816750824965567", "sha256", "EXCL_C14N", "wsse soapenv urn urn1", "") catch {};
// -------- Reference 2 --------
gen.addSameDocRef("id-4A13D668E59AAC4F38167508249655911", "sha256", "EXCL_C14N", "urn urn1", "") catch {};
// -------- Reference 3 --------
gen.addSameDocRef("X509-4A13D668E59AAC4F3816750824965588", "sha256", "EXCL_C14N", "_EMPTY_", "") catch {};
gen.setX509Cert(cert, true) catch {};
gen.setKeyInfoType("Custom");
// Create the custom KeyInfo XML..
const xml_custom_key_info = try chilkat.Xml.init();
defer xml_custom_key_info.deinit();
xml_custom_key_info.setTag("wsse:SecurityTokenReference");
xml_custom_key_info.addAttribute("wsu:Id", "STR-4A13D668E59AAC4F38167508249655810") catch {};
xml_custom_key_info.updateAttrAt("wsse:Reference", true, "URI", "#X509-4A13D668E59AAC4F3816750824965588") catch {};
xml_custom_key_info.updateAttrAt("wsse:Reference", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3") catch {};
xml_custom_key_info.setEmitXmlDecl(false);
gen.setCustomKeyInfoXml(try xml_custom_key_info.getXml(alloc));
// Load XML to be signed...
const sb_xml = try chilkat.StringBuilder.init();
defer sb_xml.deinit();
xml_to_sign.getXmlSb(sb_xml) catch {};
gen.setBehaviors("IndentedSignature");
// Sign the XML...
gen.createXmlDSigSb(sb_xml) catch {
std.debug.print("{s}\n", .{try gen.getLastErrorText(alloc)});
return;
};
// The sbXml is sent as the HTTP request body below..
std.debug.print("{s}\n", .{try sb_xml.getAsString(alloc)});
// -----------------------------------------------------------------------------------------------------------
// Send the SOAP requet to ask the server to issue a security token, which can then be used to access other SOAP services..
const http = try chilkat.Http.init();
defer http.deinit();
http.setSslClientCert(cert) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
http.setRequestHeader("Content-Type", "text/xml");
const url = "https://services-acpt.ehealth.fgov.be/IAM/SecurityTokenService/v1/RequestSecurityToken";
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpSb("POST", url, sb_xml, "utf-8", "application/xml", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const response_status = resp.getStatusCode();
std.debug.print("Response Status Code = {d}\n", .{response_status});
// 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.
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
// 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.
const bd_sec_token = try chilkat.BinData.init();
defer bd_sec_token.deinit();
resp.getBodyBd(bd_sec_token) catch {};
// 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
bd_sec_token.writeFile("qa_data/tokens/ehealth-fgov-be-sectoken.xml") catch {};
std.debug.print("OK\n", .{});
}