Unicode C
Unicode C
REST Basic Auth with Secure Strings
Demonstrates how to do REST Basic authentication using secure strings.This example requires Chilkat v9.5.0.71 or greater.
Chilkat Unicode C Downloads
#include <C_CkJsonObjectW.h>
#include <C_CkCrypt2W.h>
#include <C_CkSecureStringW.h>
#include <C_CkRestW.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObjectW json;
HCkCrypt2W crypt;
HCkSecureStringW ssLogin;
HCkSecureStringW ssPassword;
HCkRestW rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
const wchar_t *responseJson;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Imagine we've previously saved our encrypted login and password within a JSON config file
// that contains this:
// {
// "http_login": "mCrOmA7mBA7Au9RuJGb9hw==",
// "http_password": "jJtiI9TgErTTpqBz9JtHBw=="
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_LoadFile(json,L"qa_data/passwords/http.json");
crypt = CkCrypt2W_Create();
// These are the encryption settings we previously used to encrypt the credentials within the JSON config file.
CkCrypt2W_putCryptAlgorithm(crypt,L"aes");
CkCrypt2W_putCipherMode(crypt,L"cbc");
CkCrypt2W_putKeyLength(crypt,128);
CkCrypt2W_SetEncodedKey(crypt,L"000102030405060708090A0B0C0D0E0F",L"hex");
CkCrypt2W_SetEncodedIV(crypt,L"000102030405060708090A0B0C0D0E0F",L"hex");
CkCrypt2W_putEncodingMode(crypt,L"base64");
ssLogin = CkSecureStringW_Create();
ssPassword = CkSecureStringW_Create();
// Decrypt to the secure string. (the strings will still held in memory encrypted, but are now encrypted using
// a randomly generated session key.)
CkCrypt2W_DecryptSecureENC(crypt,CkJsonObjectW_stringOf(json,L"http_login"),ssLogin);
CkCrypt2W_DecryptSecureENC(crypt,CkJsonObjectW_stringOf(json,L"http_password"),ssPassword);
rest = CkRestW_Create();
// Connect to a REST server.
bTls = TRUE;
port = 443;
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"chilkatsoft.com",port,bTls,bAutoReconnect);
// Cause the "Authorization: Basic ..." header to be added to HTTP requests
CkRestW_SetAuthBasicSecure(rest,ssLogin,ssPassword);
responseJson = CkRestW_fullRequestNoBody(rest,L"GET",L"/helloWorld.html");
if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkJsonObjectW_Dispose(json);
CkCrypt2W_Dispose(crypt);
CkSecureStringW_Dispose(ssLogin);
CkSecureStringW_Dispose(ssPassword);
CkRestW_Dispose(rest);
return;
}
// Show the LastRequestHeader that was sent.
wprintf(L"%s\n",CkRestW_lastRequestHeader(rest));
// The LastRequestHeader looks like this:
// Host: chilkatsoft.com
// Authorization: Basic bXlIdHRwTG9naW46bXlIdHRwUGFzc3dvcmQ=
CkJsonObjectW_Dispose(json);
CkCrypt2W_Dispose(crypt);
CkSecureStringW_Dispose(ssLogin);
CkSecureStringW_Dispose(ssPassword);
CkRestW_Dispose(rest);
}