Chilkat Online Tools

IssueCertificate Swift3 Example

AWS Private CA

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

    let rest = CkoRest()!
    var success: Bool

    let authAws = CkoAuthAws()!
    authAws.accessKey = "AWS_ACCESS_KEY"
    authAws.secretKey = "AWS_SECRET_KEY"

    // Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    authAws.region = "us-west-2"
    authAws.serviceName = "acm-pca"
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.setAuthAws(authAws)

    // URL: https://acm-pca.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.connect("acm-pca.us-west-2.amazonaws.com", port: 443, tls: true, autoReconnect: true)
    if success != true {
        print("ConnectFailReason: \(rest.connectFailReason.intValue)")
        print("\(rest.lastErrorText!)")
        return
    }

    // The following code creates the JSON request body.
    // The JSON created by this code is shown below.

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    let json = CkoJsonObject()!
    json.update("ApiPassthrough.Extensions.CertificatePolicies[0].CertPolicyId", value: "string")
    json.update("ApiPassthrough.Extensions.CertificatePolicies[0].PolicyQualifiers[0].PolicyQualifierId", value: "string")
    json.update("ApiPassthrough.Extensions.CertificatePolicies[0].PolicyQualifiers[0].Qualifier.CpsUri", value: "string")
    json.update("ApiPassthrough.Extensions.ExtendedKeyUsage[0].ExtendedKeyUsageObjectIdentifier", value: "string")
    json.update("ApiPassthrough.Extensions.ExtendedKeyUsage[0].ExtendedKeyUsageType", value: "string")
    json.updateInt("ApiPassthrough.Extensions.KeyUsage.CRLSign", value: 123)
    json.updateInt("ApiPassthrough.Extensions.KeyUsage.DataEncipherment", value: 123)
    json.updateInt("ApiPassthrough.Extensions.KeyUsage.DecipherOnly", value: 123)
    json.updateInt("ApiPassthrough.Extensions.KeyUsage.DigitalSignature", value: 123)
    json.updateInt("ApiPassthrough.Extensions.KeyUsage.EncipherOnly", value: 123)
    json.updateInt("ApiPassthrough.Extensions.KeyUsage.KeyAgreement", value: 123)
    json.updateInt("ApiPassthrough.Extensions.KeyUsage.KeyCertSign", value: 123)
    json.updateInt("ApiPassthrough.Extensions.KeyUsage.KeyEncipherment", value: 123)
    json.updateInt("ApiPassthrough.Extensions.KeyUsage.NonRepudiation", value: 123)
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.CommonName", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Country", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.DistinguishedNameQualifier", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.GenerationQualifier", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.GivenName", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Initials", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Locality", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Organization", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.OrganizationalUnit", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Pseudonym", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.SerialNumber", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.State", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Surname", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Title", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].DnsName", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].EdiPartyName.NameAssigner", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].EdiPartyName.PartyName", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].IpAddress", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].OtherName.TypeId", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].OtherName.Value", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].RegisteredId", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].Rfc822Name", value: "string")
    json.update("ApiPassthrough.Extensions.SubjectAlternativeNames[0].UniformResourceIdentifier", value: "string")
    json.update("ApiPassthrough.Subject.CommonName", value: "string")
    json.update("ApiPassthrough.Subject.Country", value: "string")
    json.update("ApiPassthrough.Subject.DistinguishedNameQualifier", value: "string")
    json.update("ApiPassthrough.Subject.GenerationQualifier", value: "string")
    json.update("ApiPassthrough.Subject.GivenName", value: "string")
    json.update("ApiPassthrough.Subject.Initials", value: "string")
    json.update("ApiPassthrough.Subject.Locality", value: "string")
    json.update("ApiPassthrough.Subject.Organization", value: "string")
    json.update("ApiPassthrough.Subject.OrganizationalUnit", value: "string")
    json.update("ApiPassthrough.Subject.Pseudonym", value: "string")
    json.update("ApiPassthrough.Subject.SerialNumber", value: "string")
    json.update("ApiPassthrough.Subject.State", value: "string")
    json.update("ApiPassthrough.Subject.Surname", value: "string")
    json.update("ApiPassthrough.Subject.Title", value: "string")
    json.update("CertificateAuthorityArn", value: "string")
    json.updateInt("Csr", value: 123)
    json.update("IdempotencyToken", value: "string")
    json.update("SigningAlgorithm", value: "string")
    json.update("TemplateArn", value: "string")
    json.update("Validity.Type", value: "string")
    json.updateInt("Validity.Value", value: 123)
    json.update("ValidityNotBefore.Type", value: "string")
    json.updateInt("ValidityNotBefore.Value", value: 123)

    // The JSON request body created by the above code:

    // {
    //   "ApiPassthrough": {
    //     "Extensions": {
    //       "CertificatePolicies": [
    //         {
    //           "CertPolicyId": "string",
    //           "PolicyQualifiers": [
    //             {
    //               "PolicyQualifierId": "string",
    //               "Qualifier": {
    //                 "CpsUri": "string"
    //               }
    //             }
    //           ]
    //         }
    //       ],
    //       "ExtendedKeyUsage": [
    //         {
    //           "ExtendedKeyUsageObjectIdentifier": "string",
    //           "ExtendedKeyUsageType": "string"
    //         }
    //       ],
    //       "KeyUsage": {
    //         "CRLSign": boolean,
    //         "DataEncipherment": boolean,
    //         "DecipherOnly": boolean,
    //         "DigitalSignature": boolean,
    //         "EncipherOnly": boolean,
    //         "KeyAgreement": boolean,
    //         "KeyCertSign": boolean,
    //         "KeyEncipherment": boolean,
    //         "NonRepudiation": boolean
    //       },
    //       "SubjectAlternativeNames": [
    //         {
    //           "DirectoryName": {
    //             "CommonName": "string",
    //             "Country": "string",
    //             "DistinguishedNameQualifier": "string",
    //             "GenerationQualifier": "string",
    //             "GivenName": "string",
    //             "Initials": "string",
    //             "Locality": "string",
    //             "Organization": "string",
    //             "OrganizationalUnit": "string",
    //             "Pseudonym": "string",
    //             "SerialNumber": "string",
    //             "State": "string",
    //             "Surname": "string",
    //             "Title": "string"
    //           },
    //           "DnsName": "string",
    //           "EdiPartyName": {
    //             "NameAssigner": "string",
    //             "PartyName": "string"
    //           },
    //           "IpAddress": "string",
    //           "OtherName": {
    //             "TypeId": "string",
    //             "Value": "string"
    //           },
    //           "RegisteredId": "string",
    //           "Rfc822Name": "string",
    //           "UniformResourceIdentifier": "string"
    //         }
    //       ]
    //     },
    //     "Subject": {
    //       "CommonName": "string",
    //       "Country": "string",
    //       "DistinguishedNameQualifier": "string",
    //       "GenerationQualifier": "string",
    //       "GivenName": "string",
    //       "Initials": "string",
    //       "Locality": "string",
    //       "Organization": "string",
    //       "OrganizationalUnit": "string",
    //       "Pseudonym": "string",
    //       "SerialNumber": "string",
    //       "State": "string",
    //       "Surname": "string",
    //       "Title": "string"
    //     }
    //   },
    //   "CertificateAuthorityArn": "string",
    //   "Csr": blob,
    //   "IdempotencyToken": "string",
    //   "SigningAlgorithm": "string",
    //   "TemplateArn": "string",
    //   "Validity": {
    //     "Type": "string",
    //     "Value": number
    //   },
    //   "ValidityNotBefore": {
    //     "Type": "string",
    //     "Value": number
    //   }
    // }

    rest.addHeader("Content-Type", value: "application/x-amz-json-1.1")
    rest.addHeader("X-Amz-Target", value: "ACMPrivateCA.IssueCertificate")

    let sbRequestBody = CkoStringBuilder()!
    json.emitSb(sbRequestBody)
    let sbResponseBody = CkoStringBuilder()!
    success = rest.fullRequestSb("POST", uriPath: "/", requestBody: sbRequestBody, responseBody: sbResponseBody)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    var respStatusCode: Int = rest.responseStatusCode.intValue
    print("response status code = \(respStatusCode)")
    if respStatusCode != 200 {
        print("Response Header:")
        print("\(rest.responseHeader!)")
        print("Response Body:")
        print("\(sbResponseBody.getAsString()!)")
        return
    }

    let jResp = CkoJsonObject()!
    jResp.loadSb(sbResponseBody)

    // The following code parses the JSON response.
    // A sample JSON response is shown below the sample code.

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var CertificateArn: String? = jResp.string(of: "CertificateArn")

    // A sample JSON response body parsed by the above code:

    // {
    //   "CertificateArn": "string"
    // }

}