Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift) eHealth.gov.be RequestSecurityTokenRequest a security token for use with the eHealth.gov.be SOAP web services. For more information, see https://www.ehealth.fgov.be/ehealthplatform/fr/data/file/view/bf25682e28c6ccd0d06768c73a467176f63791dd?name=STS%20-%20WS%20Trust%20-%20Cookbook%20v1.0%20dd%2020012023.pdf
func chilkatTest() { // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. var success: Bool = true // 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 = CkoCert()! success = cert.loadPfxFile("SSIN=12345678.acc.p12", password: "p12_password") if success != true { print("\(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> let xmlToSign = CkoXml()! xmlToSign.tag = "soapenv:Envelope" xmlToSign.addAttribute("xmlns:ns", value: "http://docs.oasis-open.org/ws-sx/ws-trust/200512") xmlToSign.addAttribute("xmlns:soapenv", value: "http://schemas.xmlsoap.org/soap/envelope/") xmlToSign.updateAttr(at: "soapenv:Header|wsse:Security", autoCreate: true, attrName: "xmlns:wsse", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") xmlToSign.updateAttr(at: "soapenv:Header|wsse:Security", autoCreate: true, attrName: "xmlns:wsu", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") xmlToSign.updateAttr(at: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "EncodingType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary") xmlToSign.updateAttr(at: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "ValueType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3") xmlToSign.updateAttr(at: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "wsu:Id", attrValue: "X509-4A13D668E59AAC4F3816750824965588") let bdCert = CkoBinData()! cert.exportDerBd(bdCert) xmlToSign.updateChildContent("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", value: bdCert.getEncoded("base64")) xmlToSign.updateAttr(at: "soapenv:Header|wsse:Security|wsu:Timestamp", autoCreate: true, attrName: "wsu:Id", attrValue: "TS-4A13D668E59AAC4F3816750824965567") let dt = CkoDateTime()! dt.setFromCurrentSystemTime() xmlToSign.updateChildContent("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created", value: dt.getAsTimestamp(false)) dt.addSeconds(300) xmlToSign.updateChildContent("soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires", value: dt.getAsTimestamp(false)) dt.addSeconds(-300) xmlToSign.updateAttr(at: "soapenv:Body", autoCreate: true, attrName: "wsu:Id", attrValue: "id-4A13D668E59AAC4F38167508249655911") xmlToSign.updateAttr(at: "soapenv:Body", autoCreate: true, attrName: "xmlns:wsu", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") xmlToSign.updateAttr(at: "soapenv:Body|wst:RequestSecurityToken", autoCreate: true, attrName: "Context", attrValue: "RC-302613de-a809-46b5-931a-0a55bfca5937") xmlToSign.updateAttr(at: "soapenv:Body|wst:RequestSecurityToken", autoCreate: true, attrName: "xmlns:auth", attrValue: "http://docs.oasis-open.org/wsfed/authorization/200706") xmlToSign.updateAttr(at: "soapenv:Body|wst:RequestSecurityToken", autoCreate: true, attrName: "xmlns:ds", attrValue: "http://www.w3.org/2000/09/xmldsig#") xmlToSign.updateAttr(at: "soapenv:Body|wst:RequestSecurityToken", autoCreate: true, attrName: "xmlns:wsa", attrValue: "http://schemas.xmlsoap.org/ws/2004/08/addressing") xmlToSign.updateAttr(at: "soapenv:Body|wst:RequestSecurityToken", autoCreate: true, attrName: "xmlns:wsp", attrValue: "http://schemas.xmlsoap.org/ws/2004/09/policy") xmlToSign.updateAttr(at: "soapenv:Body|wst:RequestSecurityToken", autoCreate: true, attrName: "xmlns:wsse", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") xmlToSign.updateAttr(at: "soapenv:Body|wst:RequestSecurityToken", autoCreate: true, attrName: "xmlns:wst", attrValue: "http://docs.oasis-open.org/ws-sx/ws-trust/200512") xmlToSign.updateChildContent("soapenv:Body|wst:RequestSecurityToken|wst:TokenType", value: "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1") xmlToSign.updateChildContent("soapenv:Body|wst:RequestSecurityToken|wst:RequestType", value: "http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue") success = xmlToSign.updateAttr(at: "soapenv:Body|wst:RequestSecurityToken|wst:Claims", autoCreate: true, attrName: "Dialect", attrValue: "http://docs.oasis-open.org/wsfed/authorization/200706/authclaims") success = xmlToSign.updateAttr(at: "soapenv:Body|wst:RequestSecurityToken|wst:Claims|auth:ClaimType[1]", autoCreate: true, attrName: "Uri", attrValue: "urn:be:fgov:ehealth:1.0:certificateholder:person:ssin") xmlToSign.updateChildContent("soapenv:Body|wst:RequestSecurityToken|wst:KeyType", value: "http://docs.oasis-open.org/ws-sx/wstrust/200512/PublicKey") let gen = CkoXmlDSigGen()! 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 -------- gen.addSameDocRef("TS-4A13D668E59AAC4F3816750824965567", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "wsse soapenv urn urn1", refType: "") // -------- Reference 2 -------- gen.addSameDocRef("id-4A13D668E59AAC4F38167508249655911", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "urn urn1", refType: "") // -------- Reference 3 -------- gen.addSameDocRef("X509-4A13D668E59AAC4F3816750824965588", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "_EMPTY_", refType: "") gen.setX509Cert(cert, usePrivateKey: true) gen.keyInfoType = "Custom" // Create the custom KeyInfo XML.. let xmlCustomKeyInfo = CkoXml()! xmlCustomKeyInfo.tag = "wsse:SecurityTokenReference" xmlCustomKeyInfo.addAttribute("wsu:Id", value: "STR-4A13D668E59AAC4F38167508249655810") xmlCustomKeyInfo.updateAttr(at: "wsse:Reference", autoCreate: true, attrName: "URI", attrValue: "#X509-4A13D668E59AAC4F3816750824965588") xmlCustomKeyInfo.updateAttr(at: "wsse:Reference", autoCreate: true, attrName: "ValueType", attrValue: "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... let sbXml = CkoStringBuilder()! xmlToSign.getSb(sbXml) gen.behaviors = "IndentedSignature" // Sign the XML... success = gen.createXmlDSigSb(sbXml) if success != true { print("\(gen.lastErrorText!)") return } // The sbXml is sent as the HTTP request body below.. print("\(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.. let http = CkoHttp()! success = http.setSslClientCert(cert) if success == false { print("\(http.lastErrorText!)") return } http.setRequestHeader("Content-Type", value: "text/xml") var url: String? = "https://services-acpt.ehealth.fgov.be/IAM/SecurityTokenService/v1/RequestSecurityToken" var resp: CkoHttpResponse? = http.postXml(url, xmlDoc: sbXml.getAsString(), charset: "utf-8") if http.lastMethodSuccess == false { print("\(http.lastErrorText!)") return } var responseStatus: Int = resp!.statusCode.intValue print("Response Status Code = \(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. print("\(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. let bdSecToken = CkoBinData()! resp!.getBodyBd(bdSecToken) resp = nil // 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("qa_data/tokens/ehealth-fgov-be-sectoken.xml") print("OK") } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.