Rust Requires Chilkat v11.0.0+
Rust
eHealth.gov.be RequestSecurityToken
Request a security token for use with the eHealth.gov.be SOAP web services.Chilkat Rust Downloads
// 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..
let cert = chilkat::Cert::new();
if cert.load_pfx_file("SSIN=12345678.acc.p12", "p12_password").is_err() {
println!("{}", cert.last_error_text());
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>
let xml_to_sign = chilkat::Xml::new();
xml_to_sign.set_tag("soapenv:Envelope");
let _ = xml_to_sign.add_attribute("xmlns:ns", "http://docs.oasis-open.org/ws-sx/ws-trust/200512");
let _ = xml_to_sign.add_attribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security", true, "xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
let _ = xml_to_sign.update_attr_at("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");
let _ = xml_to_sign.update_attr_at("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");
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "wsu:Id", "X509-4A13D668E59AAC4F3816750824965588");
let bd_cert = chilkat::BinData::new();
let _ = cert.export_cert_der_bd(&bd_cert);
xml_to_sign.update_child_content("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", &bd_cert.get_encoded("base64").unwrap_or_default());
let _ = xml_to_sign.update_attr_at("soapenv:Header|wsse:Security|wsu:Timestamp", true, "wsu:Id", "TS-4A13D668E59AAC4F3816750824965567");
let dt = chilkat::DateTime::new();
let _ = dt.set_from_current_system_time();
xml_to_sign.update_child_content("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created", &dt.get_as_timestamp(false).unwrap_or_default());
let _ = dt.add_seconds(300);
xml_to_sign.update_child_content("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires", &dt.get_as_timestamp(false).unwrap_or_default());
let _ = dt.add_seconds(-300);
let _ = xml_to_sign.update_attr_at("soapenv:Body", true, "wsu:Id", "id-4A13D668E59AAC4F38167508249655911");
let _ = xml_to_sign.update_attr_at("soapenv:Body", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
let _ = xml_to_sign.update_attr_at("soapenv:Body|wst:RequestSecurityToken", true, "Context", "RC-302613de-a809-46b5-931a-0a55bfca5937");
let _ = xml_to_sign.update_attr_at("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:auth", "http://docs.oasis-open.org/wsfed/authorization/200706");
let _ = xml_to_sign.update_attr_at("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");
let _ = xml_to_sign.update_attr_at("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
let _ = xml_to_sign.update_attr_at("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:wsp", "http://schemas.xmlsoap.org/ws/2004/09/policy");
let _ = xml_to_sign.update_attr_at("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
let _ = xml_to_sign.update_attr_at("soapenv:Body|wst:RequestSecurityToken", true, "xmlns:wst", "http://docs.oasis-open.org/ws-sx/ws-trust/200512");
xml_to_sign.update_child_content("soapenv:Body|wst:RequestSecurityToken|wst:TokenType", "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1");
xml_to_sign.update_child_content("soapenv:Body|wst:RequestSecurityToken|wst:RequestType", "http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");
let _ = xml_to_sign.update_attr_at("soapenv:Body|wst:RequestSecurityToken|wst:Claims", true, "Dialect", "http://docs.oasis-open.org/wsfed/authorization/200706/authclaims").is_ok();
let _ = xml_to_sign.update_attr_at("soapenv:Body|wst:RequestSecurityToken|wst:Claims|auth:ClaimType[1]", true, "Uri", "urn:be:fgov:ehealth:1.0:certificateholder:person:ssin").is_ok();
xml_to_sign.update_child_content("soapenv:Body|wst:RequestSecurityToken|wst:KeyType", "http://docs.oasis-open.org/ws-sx/wstrust/200512/PublicKey");
let gen_ = chilkat::XmlDSigGen::new();
gen_.set_sig_location("soapenv:Envelope|soapenv:Header|wsse:Security|wsse:BinarySecurityToken");
gen_.set_sig_location_mod(1);
gen_.set_sig_id("SIG-4A13D668E59AAC4F38167508249656212");
gen_.set_sig_namespace_prefix("ds");
gen_.set_sig_namespace_uri("http://www.w3.org/2000/09/xmldsig#");
gen_.set_signed_info_prefix_list("soapenv urn urn1");
gen_.set_inc_namespace_prefix("ec");
gen_.set_inc_namespace_uri("http://www.w3.org/2001/10/xml-exc-c14n#");
gen_.set_signed_info_canon_alg("EXCL_C14N");
gen_.set_signed_info_digest_method("sha256");
// Set the KeyInfoId before adding references..
gen_.set_key_info_id("KI-4A13D668E59AAC4F3816750824965589");
// -------- Reference 1 --------
let _ = gen_.add_same_doc_ref("TS-4A13D668E59AAC4F3816750824965567", "sha256", "EXCL_C14N", "wsse soapenv urn urn1", "");
// -------- Reference 2 --------
let _ = gen_.add_same_doc_ref("id-4A13D668E59AAC4F38167508249655911", "sha256", "EXCL_C14N", "urn urn1", "");
// -------- Reference 3 --------
let _ = gen_.add_same_doc_ref("X509-4A13D668E59AAC4F3816750824965588", "sha256", "EXCL_C14N", "_EMPTY_", "");
let _ = gen_.set_x509_cert(&cert, true);
gen_.set_key_info_type("Custom");
// Create the custom KeyInfo XML..
let xml_custom_key_info = chilkat::Xml::new();
xml_custom_key_info.set_tag("wsse:SecurityTokenReference");
let _ = xml_custom_key_info.add_attribute("wsu:Id", "STR-4A13D668E59AAC4F38167508249655810");
let _ = xml_custom_key_info.update_attr_at("wsse:Reference", true, "URI", "#X509-4A13D668E59AAC4F3816750824965588");
let _ = xml_custom_key_info.update_attr_at("wsse:Reference", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
xml_custom_key_info.set_emit_xml_decl(false);
gen_.set_custom_key_info_xml(&xml_custom_key_info.get_xml().unwrap_or_default());
// Load XML to be signed...
let sb_xml = chilkat::StringBuilder::new();
let _ = xml_to_sign.get_xml_sb(&sb_xml);
gen_.set_behaviors("IndentedSignature");
// Sign the XML...
if gen_.create_xml_d_sig_sb(&sb_xml).is_err() {
println!("{}", gen_.last_error_text());
return;
}
// The sbXml is sent as the HTTP request body below..
println!("{}", sb_xml.get_as_string().unwrap_or_default());
// -----------------------------------------------------------------------------------------------------------
// Send the SOAP requet to ask the server to issue a security token, which can then be used to access other SOAP services..
let http = chilkat::Http::new();
if http.set_ssl_client_cert(&cert).is_err() {
println!("{}", http.last_error_text());
return;
}
http.set_request_header("Content-Type", "text/xml");
let url = "https://services-acpt.ehealth.fgov.be/IAM/SecurityTokenService/v1/RequestSecurityToken".to_string();
let resp = chilkat::HttpResponse::new();
if http.http_sb("POST", &url, &sb_xml, "utf-8", "application/xml", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let response_status = resp.status_code();
println!("Response Status Code = {}", 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.
println!("{}", resp.body_str());
// 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.
let bd_sec_token = chilkat::BinData::new();
let _ = resp.get_body_bd(&bd_sec_token);
// 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
let _ = bd_sec_token.write_file("qa_data/tokens/ehealth-fgov-be-sectoken.xml");
println!("OK");