Skip to content

Instantly share code, notes, and snippets.

@infamousjoeg
Created December 6, 2021 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infamousjoeg/b3d9d46555909ed6f595841e17b45d7f to your computer and use it in GitHub Desktop.
Save infamousjoeg/b3d9d46555909ed6f595841e17b45d7f to your computer and use it in GitHub Desktop.
C++ CCP REST Call Example - Thanks @JimmyJamCABD
#include "CentralCredentialObject.h"
#include <iostream>
size_t CentralCredentialObject::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
/**
* Constructor.
*
* @param url address of server where the CCP resides
* @param id appid that will be used to retrieve object
*/
CentralCredentialObject::CentralCredentialObject(std::string address, std::string id)
{
address_url = address;
app_id = id;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &read_buffer);
}
}
/**
* setter for the certificates that will be used to authenticare.
*
* @param path_to_cert path to certificate file
* @param path_to_key path to key file
*/
void CentralCredentialObject::setCertandKey(std::string path_to_cert, std::string path_to_key)
{
if (curl)
{
curl_easy_setopt(curl, CURLOPT_SSLCERT, path_to_cert.c_str());
curl_easy_setopt(curl, CURLOPT_SSLKEY, path_to_key.c_str());
}
}
/**
* setter for the certificate that will be used to authenticare.
*
* @param path_to_cert path to certificate file
*/
void CentralCredentialObject::setCert(std::string path_to_cert)
{
if (curl){curl_easy_setopt(curl, CURLOPT_SSLCERT, path_to_cert.c_str())};
}
/**
* Sets SSL Verification to False.
*
*/
void CentralCredentialObject::ignoreRoot()
{
if (curl){ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); }
}
/**
* Sets SSL Verification to False.
*
* @param path_to_root_ca path to Root certificate file
*/
void CentralCredentialObject::setRootCA(std::string path_to_root_ca)
{
if (curl){
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
curl_easy_setopt(curl, CURLOPT_CAINFO, path_to_root_ca.c_str());
}
}
/**
* setter for the certificates that will be used to authenticare.
*
* @param safe safe where the object is located
* @param object object name of desired credential
* @return response of the CCP Query as a string
*/
std::string CentralCredentialObject::getCCPResponse(std::string safe, std::string object){
CURLcode res;
std::string final_url = address_url
+ "/AIMWebService/api/Accounts?"
+ "AppId=" + app_id
+ "&Safe=" + safe
+ "&Object=" + object;
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, final_url.c_str());
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
std::cout << res << std::endl;
return read_buffer;
}
#ifndef __CENTRALCREDENTIALOBJECT_H_INCLUDED__
#define __CENTRALCREDENTIALOBJECT_H_INCLUDED__
#include <string>
#include <curl/curl.h>
class CentralCredentialObject { // The class
private: // Access specifier
std::string address_url; // Attribute (int variable)
std::string app_id; // Attribute (string variable)
std::string read_buffer;
CURL *curl;
static size_t WriteCallback(void *, size_t, size_t, void *);
public:
CentralCredentialObject(std::string, std::string );
void setCertandKey(std::string, std::string);
void setCert(std::string);
void setRootCA(std::string);
void ignoreRoot();
std::string getCCPResponse(std::string, std::string);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment