Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

Ibanity HTTP Signature for XS2A, Isabel Connect, Ponto Connect

See more Ibanity Examples

Demonstrates how to add a Signature header for Ibanity HTTP requests.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  In order to sign your HTTP requests, you have to add 2 headers to the HTTP request: Digest: the digest of the request payload and Signature: the actual signature of the request. 

    //  POST /xs2a/customer-access-tokens HTTP/1.1
    //  Host: api.ibanity.com
    //  Content-Type: application/json
    //  Digest: SHA-512=z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==
    //  Ibanity-Idempotency-Key: 61f02718-eeee-46e1-b5eb-e8fd6e799c2d
    //  Signature: keyId="62f02718-eeee-46e1-b5eb-e8fd6e799c2e",created=1599659223,algorithm="hs2019",headers="(request-target) host digest (created) ibanity-idempotency-key",signature="SjWJWbWN7i0...zsbM="
    //  
    //  {"data":{"type":"customerAccessToken", "attributes":{"applicationCustomerReference":"15874569"}}}

    //  The payload (body) of the above HTTP request is the JSON.
    //  Build the JSON above.
    //  Use this online tool to generate code from sample JSON: 
    //  Generate Code to Create JSON
    let json = CkoJsonObject()!
    json.updateString(jsonPath: "data.type", value: "customerAccessToken")
    json.updateString(jsonPath: "data.attributes.applicationCustomerReference", value: "15874569")

    var payload: String? = json.emit()
    print("payload = \(payload!)")

    //  Step 1: Build the (created) virtual header

    let dtNow = CkoDateTime()!
    dtNow.setFromCurrentSystemTime()
    var created: String? = dtNow.get(asUnixTimeStr: false)
    print("created = \(created!)")

    //  Step 2: Build the Digest header
    let crypt = CkoCrypt2()!
    crypt.hashAlgorithm = "sha512"
    crypt.encodingMode = "base64"
    crypt.charset = "utf-8"

    let sbDigestHdrValue = CkoStringBuilder()!
    sbDigestHdrValue.append(value: "SHA-512=")
    sbDigestHdrValue.append(value: crypt.hashStringENC(str: json.emit()))

    print("\(sbDigestHdrValue.getAsString()!)")

    //  Step 3: Build the (request target) virtual header

    //  In order to build the signature you will need a virtual header named (request-target) (the parentheses are important). 
    //  The (request-target) is the string concatenation of the HTTP method (in lowercase) with the path and query parameters.
    var request_target: String? = "post /xs2a/customer-access-tokens"

    //  Step 4: Build the signing string

    //  The signing string is the concatenation of the signed header names (in lowercase) and values separated by a LF.

    //  You must always sign the following headers: (request-target), host, (created), digest. 
    //  If used, you must also sign the authorization header and any ibanity-* headers, such as ibanity-idempotency-key. 

    let sbSigningString = CkoStringBuilder()!
    sbSigningString.append(value: "(request-target): ")
    sbSigningString.appendLine(str: request_target, crlf: false)
    sbSigningString.append(value: "host: ")
    sbSigningString.appendLine(str: "api.ibanity.com", crlf: false)
    sbSigningString.append(value: "digest: ")
    sbSigningString.appendLine(str: sbDigestHdrValue.getAsString(), crlf: false)
    sbSigningString.append(value: "(created): ")
    sbSigningString.appendLine(str: created, crlf: false)
    sbSigningString.append(value: "ibanity-idempotency-key: ")
    var idempotencyKey: String? = crypt.generateUuid()
    sbSigningString.append(value: idempotencyKey)

    //  Step 5: Build the signed headers list

    //  To allow Ibanity to check the signed headers, you must provide a list of the header names. They should be lowercase and in the same order used to create the signing string. 
    var signed_headers_list: String? = "(request-target) host digest (created) ibanity-idempotency-key"

    //  Step 6: Build the Signature header

    //  This is where the real signing happens. The signature header is a combination of several sub-headers -
    //  
    //      keyId: the identifier for the application's signature certificate, obtained from the Developer Portal
    //      algorithm: the digital signature algorithm used to generate the signature (must be hs2019)
    //      headers: The list of HTTP headers created in step 5
    //      signature: the Base64-encoded digital signature of the signing string created in step 4.

    let privKey = CkoPrivateKey()!
    success = privKey.loadEncryptedPemFile(path: "my_ibanity_signature_private_key.pem", password: "pem_password")
    if success == false {
        print("\(privKey.lastErrorText!)")
        return
    }

    let rsa = CkoRsa()!
    rsa.pssSaltLen = 32
    rsa.encodingMode = "base64"
    //  Use the RSASSA-PSS signature algorithm
    rsa.pkcsPadding = false

    success = rsa.usePrivateKey(privKey: privKey)
    if success == false {
        print("\(rsa.lastErrorText!)")
        return
    }

    //  Sign the signing string.
    var sigBase64: String? = rsa.signStringENC(str: sbSigningString.getAsString(), hashAlg: "sha-256")
    if rsa.lastMethodSuccess == false {
        print("\(rsa.lastErrorText!)")
        return
    }

    //  Build the signature header value.
    let sbSigHeaderValue = CkoStringBuilder()!
    sbSigHeaderValue.append(value: "keyId=\"")
    //  Use your identifier for the application's signature certificate, obtained from the Developer Portal
    sbSigHeaderValue.append(value: "62f02718-eeee-46e1-b5eb-e8fd6e799c2e")
    sbSigHeaderValue.append(value: "\",created=")
    sbSigHeaderValue.append(value: created)
    sbSigHeaderValue.append(value: ",algorithm=\"hs2019\",headers=\"")
    sbSigHeaderValue.append(value: signed_headers_list)
    sbSigHeaderValue.append(value: "\",signature=\"")
    sbSigHeaderValue.append(value: sigBase64)
    sbSigHeaderValue.append(value: "\"")

    print("\(sbSigHeaderValue.getAsString()!)")

}