How to connect to and authenticate an external web service from BC cloud

garth68garth68 Member Posts: 5
I am trying to connect to the ABN Amro sandbox. The API example is as follows:

curl -X POST "https://auth-mtls-sandbox.abnamro.com/as/token.oauth2" \
-v \
--cert CertificateCommercial.crt \
--key PrivateKeyCommercial.key \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&client_id=test_client&scope=account:details:read account:balance:read account:transaction:read'

The headers, body, certificate and connection are fairly easy, but I have no idea how to use the key file:

Url := 'https://auth-mtls-sandbox.abnamro.com/';
BodyContent := 'grant_type=client_credentials&client_id=test_client&scope=account:details:read account:balance:read account:transaction:read';
Headers.Remove('Content-Type');
Headers.Add('Content-Type', 'application/x-www-form-urlencoded');
Headers.Add('Cache-Control', 'no-cache');
Body.WriteFrom(ContentText);
Body.GetHeaders(Headers);

// cert = get from cert table convert to base64?
Client.AddCertificate(Cert);

if not Client.Get(Url, Response) then
Error(WebServiceError);

Any help is appreciated!!

Best Answers

  • ftorneroftornero Member Posts: 524
    Answer ✓
    Hello @garth68,

    You need to convert the .crt and the .key to a .pfx and this new certificate is the one that you use with Client.AddCertificate(Cert).

    For this conversion you can use openssl.
    openssl pkcs12 -export -out NewCertificate.pfx -inkey PrivateKeyCommercial.key -in CertificateCommercial.crt
    

    Regards.
  • ftorneroftornero Member Posts: 524
    Answer ✓
    Hello @garth68,

    You could save the pfx certificate in a BLOB field and the password in a text field, for the test, later you could elaborate a little more for security purposes.

    And use Client.AddCertificate(Cert, Password)

    The Cert in the Client.AddCertificate must be in Base64 format, so you can save in the BLOB field the certificate in Base64 o convert to Base64 if is in pfx format.

    Regards.

Answers

  • ftorneroftornero Member Posts: 524
    Hello @garth68,

    What kind of certificate do you store in the cert table ?

    Regards.
  • garth68garth68 Member Posts: 5
    Hi @ftornero,

    Thanks for the reply! I'm not sure what type of certificate it is. I'm not even sure if I'm supposed to store it in the certificate table (I can't find any help on this topic).
    The certificate and key (which are freely available for this sandbox testing) are as follows:
    .crt file:
    BEGIN CERTIFICATE
    MIIF6zCCA9OgAwIBAgIUGQpUrbFYC6Ld1+n9PIgYqHQxzNwwDQYJKoZIhvcNAQEL
    BQAwgYQxCzAJBgNVBAYTAk5MMRYwFAYDVQQIDA1Ob29yZCBIb2xsYW5kMRIwEAYD
    VQQHDAlBbXN0ZXJkYW0xEDAOBgNVBAoMB1NhbmRib3gxDDAKBgNVBAsMA0NBVDEp
    MCcGA1UEAwwgU2FuZGJveENlcnRpZmljYXRlIFdJVEhPVVQgVFJVU1QwHhcNMTkw
    OTA0MDkzNzQ5WhcNMjkwOTAxMDkzNzQ5WjCBhDELMAkGA1UEBhMCTkwxFjAUBgNV
    BAgMDU5vb3JkIEhvbGxhbmQxEjAQBgNVBAcMCUFtc3RlcmRhbTEQMA4GA1UECgwH
    U2FuZGJveDEMMAoGA1UECwwDQ0FUMSkwJwYDVQQDDCBTYW5kYm94Q2VydGlmaWNh
    dGUgV0lUSE9VVCBUUlVTVDCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIB
    AMRWQ+VekoSxHEhJ93rSgApLW2pvFinb6N1hipHIBIwJPdlAsnEAwCkA009zojs
    MxscvspsUJvsdsUn2RCjw4idP41Ta0ZFsVla63NIoTI83ZpToE20lpgJrM0ay1Zv
    rraLJzoXCj3PDP6JyqlhXtl/ASRXtOlOwf+KL2XBfkrwtRXsMwSbXh/A8V4AjzTN
    M1QyUdfiHnImzw04HfS2nPaUbJ4TEcvrw5s9qMGddsmeMsRPH54zCO9JVdt3emXg
    l5sBdbzDxp1nwkcR6sKoYiEy7k/o3+CDWYkTf1FUREiylVl46J85nHl3Msw1ptwA
    keMw1aL6U+JG07EOq/RhRctIhdWqR40JkUspeVd70t4hgqUaQL/Uopv6dVfK9zM3
    6s29SMtlWNvvRNHpS+z0USsJ/MKO60gtHo+O5jnmYJtL8WgdKt7LlmIxzgX2kc0j
    hkI+g42dy4y0+aPsw01Nw+htYfIp24sYv7GiouYRMel1zdHMQt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key file:
    BEGIN PRIVATE KEY
    MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQDEVkPlXpKEsRxI
    Sfd60oLwKS1tqbxYp2+jdYYqRyASMCT3ZQLJxAMApANNPc6I7DMbHL7KbFCb7HbF
    J9kQo8OInT+NU2tGRbFZWutzSKEyPN2aU6BNtJaYCazNGstWb662iyc6Fwo9zwz+
    icqpYV7ZfwEkV7TpTsH/ii9lwX5K8LUV7DMEm14fwPFeAI80zTNUMlHX4h5yJs8N
    OB30tpz2lGyeExHL68ObPajBnXbJnjLETx+eMwjvSVXbd3pl4JebAXW8w8adZ8JH
    EerCqGIhMu5P6N/gg1mJE39RVERIspVZeOifOZx5dzLMNabcAJHjMNWi+lPiRtOx
    Dqv0YUXLSIXVqkeNCZFLKXlXe9LeIYKlGkC/1KKb+nVXyvczN+rNvUjLZVjb70TR
    6Uvs9FErCfzCjutILR6PjuY55mCbS/FoHSrey5ZiMc4F9pHNI4ZCPoONncuMtPmj
    7MNNTcPobWHyKduLGL+xoqLmETHpdc3RzELTJwN6eknFP2rDCVHxjFsaPLNawaFD
    QOatEdJ1yHvRKg5oN3khFMDhhGoQ1cv98OrY+CYSiq4Pgp+Dz5zxopfHLQNvtx8
    UUU2LEIX4PEvb+eKDeVcZU/EfrQMwOiPL0Tb2Rk/NDX0kYNlGB2kYRvCPiGbCZSA
    3oiq9y8tpn9GwRXJ40nnGi7mKdSofQIDAQABAoICAQC20FhSF7g4A64BBZaBb8hq
    FP1u0hBFQEqsGj892JJdPST+kriA5JmjHBTS9Ez+o2iGw0JHu4lnVkvVEmCEpOfp
    qfnF/zmw6i3TtNvtsWAfJht1mo52LEPY95FGPb++ESnOYU26Qj/QA53m2KizBK5L
    uH14uI4Ee8gZyDKP/hY5c146Nn/et0hyKrbUP5MYqEU2DdC1Qp/0zHwYo/sIpI0M
    8HEe00TL6xBs2YHsHlHANCSzM7WOGZklLdTbEKoQeIrR0w8tH+ItKLL2eBFvBVix
    7v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s0Qe6u60Tf693IAZYwe7IVobasYT0R2ThbawGrS
    gj12OIBan8VryRt0eIuQKPp06ARzEzs=
    END PRIVATE KEY

    In Postman I add these 2 files to the certificate setup and I can connect. But in BC cloud I have no idea how to use them. I assume that I stream the certificate to the client using Client.AddCertificate(Cert)...

    The response to this Client.Get should be a OAuth2 token that allows me to get the data needed
  • ftorneroftornero Member Posts: 524
    Answer ✓
    Hello @garth68,

    You need to convert the .crt and the .key to a .pfx and this new certificate is the one that you use with Client.AddCertificate(Cert).

    For this conversion you can use openssl.
    openssl pkcs12 -export -out NewCertificate.pfx -inkey PrivateKeyCommercial.key -in CertificateCommercial.crt
    

    Regards.
  • garth68garth68 Member Posts: 5
    Thanks @ftornero! I appreciate the help!!
  • garth68garth68 Member Posts: 5
    Hi @ftornero,

    Thanks, created without a problem. How do I actually use the certificate? The functions in the Certificate Management codeunit are marked as OnPrem, and when I use the isolatedstorage.Get I get an error:

    Certificate.Get('CERT0000000002');
    //CertificateMgt.GetCertAsBase64String(Certificate); //marked as OnPrem
    ISOLATEDSTORAGE.Get(Certificate.Code, DATASCOPE::CompanyAndUser, Cert); //error here
    ISOLATEDSTORAGE.Get(Certificate.Code + 'Password', DATASCOPE::CompanyAndUser, StoredPassword);

    The Isolated Storage does not exist. Identification fields and values: App Id='{56368753-DB13-4133-8BF0-6F24NNNA7A4A}',Scope='CompanyAndUser',Company Name='TestDB',User Id='{026716F0-DBEB-4417-A3E0-9FA440277933}',Key='CERT0000000002'

    I'm really not sure how this functionality works because the code on the Certificate page to save the file in isolated storage calls the following function in cert management:

    [Scope('OnPrem')]
    procedure SaveCertToIsolatedStorage(IsolatedCertificate: Record "Isolated Certificate")

    Am I missing something? I don't get an error, but I don't see how this can work....
  • ftorneroftornero Member Posts: 524
    Answer ✓
    Hello @garth68,

    You could save the pfx certificate in a BLOB field and the password in a text field, for the test, later you could elaborate a little more for security purposes.

    And use Client.AddCertificate(Cert, Password)

    The Cert in the Client.AddCertificate must be in Base64 format, so you can save in the BLOB field the certificate in Base64 o convert to Base64 if is in pfx format.

    Regards.
  • garth68garth68 Member Posts: 5
    Thanks @ftornero . That makes sense!
Sign In or Register to comment.