Sample code for 30+ languages & platforms
Swift

Belgium eHealth Platform - checkAccessControl

See more Belgian eHealth Platform Examples

Demonstrates the checkAccessControl operation of PlatformIntegrationConsumerTest, which requires an X.509 certificate and signature. This tests the validity of your certificate and signature.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes 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 = CkoCert()!
    success = cert.loadPfxFile(path: "SSIN=12345678.acc.p12", password: "p12_password")
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    // Create the XML to be signed...
    let xmlToSign = CkoXml()!
    xmlToSign.tag = "soapenv:Envelope"
    xmlToSign.addAttribute(name: "xmlns:soapenv", value: "http://schemas.xmlsoap.org/soap/envelope/")
    xmlToSign.addAttribute(name: "xmlns:urn", value: "urn:be:fgov:ehealth:platformintegrationconsumertest:v1")
    xmlToSign.addAttribute(name: "xmlns:urn1", value: "urn:be:fgov:ehealth:platformintegrationconsumertest:types:v1")
    xmlToSign.updateAttrAt(tagPath: "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.updateAttrAt(tagPath: "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.updateAttrAt(tagPath: "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.updateAttrAt(tagPath: "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.updateAttrAt(tagPath: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "wsu:Id", attrValue: "X509-FC77E2C72083DA8E0F16711753508182856")

    // ---------------------------------------------------------------------------------------------------------------
    // A note about the Id's, such as X509-FC77E2C72083DA8E0F16711753508182856, TS-FC77E2C72083DA8E0F16711753508042855, etc.
    // These Id's simply need to be unique within the XML document.  You don't need to generate new Id's every time.
    // You can use the same Id's in each XML document that is submitted. The purpose of each Id is to
    // match the XMLDsig Reference to the element in XML being referenced. 
    // In other words, you could use the Id's "mickey_mouse", "donald_duck", and "goofy", and it would work perfectly OK,
    // as long as no other XML elements also use the Id's "mickey_mouse", "donald_duck", or "goofy"
    // ---------------------------------------------------------------------------------------------------------------

    let bdCert = CkoBinData()!
    cert.exportDerBd(cerData: bdCert)

    xmlToSign.updateChildContent(tagPath: "soapenv:Header|wsse:Security|wsse:BinarySecurityToken", value: bdCert.getEncoded(encoding: "base64"))
    xmlToSign.updateAttrAt(tagPath: "soapenv:Header|wsse:Security|wsu:Timestamp", autoCreate: true, attrName: "wsu:Id", attrValue: "TS-FC77E2C72083DA8E0F16711753508042855")

    let dt = CkoDateTime()!
    dt.setFromCurrentSystemTime()
    xmlToSign.updateChildContent(tagPath: "soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created", value: dt.get(asTimestamp: false))
    dt.addSeconds(numSeconds: 3600)
    xmlToSign.updateChildContent(tagPath: "soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires", value: dt.get(asTimestamp: false))
    dt.addSeconds(numSeconds: -3600)

    xmlToSign.updateAttrAt(tagPath: "soapenv:Body", autoCreate: true, attrName: "wsu:Id", attrValue: "id-FC77E2C72083DA8E0F16711753508182859")
    xmlToSign.updateAttrAt(tagPath: "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.updateChildContent(tagPath: "soapenv:Body|urn:CheckAccessControlRequest|urn1:Message", value: "Hello World")

    // Create a timestamp with the current date/time in the following format: 2014-12-30T15:29:03.157+01:00

    xmlToSign.updateChildContent(tagPath: "soapenv:Body|urn:CheckAccessControlRequest|urn1:Timestamp", value: dt.get(asTimestamp: true))

    let gen = CkoXmlDSigGen()!

    gen.sigLocation = "soapenv:Envelope|soapenv:Header|wsse:Security|wsse:BinarySecurityToken"
    gen.sigLocationMod = 1
    gen.sigId = "SIG-FC77E2C72083DA8E0F16711753508252860"
    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-FC77E2C72083DA8E0F16711753508182857"

    // -------- Reference 1 --------
    gen.addSameDocRef(id: "TS-FC77E2C72083DA8E0F16711753508042855", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "wsse soapenv urn urn1", refType: "")

    // -------- Reference 2 --------
    gen.addSameDocRef(id: "id-FC77E2C72083DA8E0F16711753508182859", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "urn urn1", refType: "")

    // -------- Reference 3 --------
    gen.addSameDocRef(id: "X509-FC77E2C72083DA8E0F16711753508182856", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "_EMPTY_", refType: "")

    gen.setX509Cert(cert: cert, usePrivateKey: true)

    gen.keyInfoType = "Custom"

    // Create the custom KeyInfo XML..
    let xmlCustomKeyInfo = CkoXml()!
    xmlCustomKeyInfo.tag = "wsse:SecurityTokenReference"
    xmlCustomKeyInfo.addAttribute(name: "wsu:Id", value: "STR-FC77E2C72083DA8E0F16711753508182858")
    xmlCustomKeyInfo.updateAttrAt(tagPath: "wsse:Reference", autoCreate: true, attrName: "URI", attrValue: "#X509-FC77E2C72083DA8E0F16711753508182856")
    xmlCustomKeyInfo.updateAttrAt(tagPath: "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(sb: sbXml)

    gen.behaviors = "IndentedSignature"

    // Sign the XML...
    success = gen.createXmlDSigSb(sbXml: sbXml)
    if success == false {
        print("\(gen.lastErrorText!)")
        return
    }

    // -----------------------------------------------
    // Send the signed XML...
    let http = CkoHttp()!

    success = http.setSslClientCert(cert: cert)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    http.setRequestHeader(name: "Content-Type", value: "text/xml")

    // Change to services.ehealth.fgov.be for the production environment.
    let resp = CkoHttpResponse()!
    success = http.httpSb(verb: "POST", url: "https://services-acpt.ehealth.fgov.be/PlatformIntegrationConsumerTest/v1", sb: sbXml, charset: "utf-8", contentType: "application/xml", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("\(resp.bodyStr!)")
    print("response status code = \(resp.statusCode.intValue)")

    // A successful response is a 200 status code, with this sample response:

    // <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    //    <soapenv:Header xmlns:v1="urn:be:fgov:ehealth:platformintegrationconsumertest:v1" xmlns:v11="urn:be:fgov:ehealth:platformintegrationconsumertest:types:v1"/>
    //    <soapenv:Body xmlns:ic="urn:be:fgov:ehealth:platformintegrationconsumertest:v1" xmlns:type="urn:be:fgov:ehealth:platformintegrationconsumertest:types:v1">
    //       <ic:CheckAccessControlResponse>
    //          <type:Message>Hello World</type:Message>
    //          <type:Timestamp>2023-09-28T22:17:26.643+02:00</type:Timestamp>
    //          <type:AuthenticatedConsumer>CN="SSIN=aaaaaa", OU=eHealth-platform Belgium, OU=bbbb, OU="SSIN=aaaaaaa", O=Federal Government, C=BE</type:AuthenticatedConsumer>
    //       </ic:CheckAccessControlResponse>
    //    </soapenv:Body>
    // </soapenv:Envelope>

}