Unicode C
Unicode C
PRODA Get OAuth2 Access Token using JWT
See more PRODA Examples
Demonstrates how to get an OAuth2 access token for the PRODA Australian Government Online Services using a JWT.Chilkat Unicode C Downloads
#include <C_CkPrivateKeyW.h>
#include <C_CkJwtW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPrivateKeyW privKey;
HCkJwtW jwt;
HCkJsonObjectW jose;
HCkJsonObjectW claims;
int curDateTime;
const wchar_t *jwtToken;
HCkHttpW http;
HCkHttpRequestW req;
HCkHttpResponseW resp;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First create a JWT to be sent in the POST to https://vnd.proda.humanservices.gov.au/mga/sps/oauth/oauth20/token
privKey = CkPrivateKeyW_Create();
// Load an RSA private key from a PEM file.
// Chilkat provides alternative methods to load from other formats, or to load from a string or binary data.
success = CkPrivateKeyW_LoadEncryptedPemFile(privKey,L"qa_data/pem/rsa_passwd.pem",L"passwd");
if (success == FALSE) {
wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
CkPrivateKeyW_Dispose(privKey);
return;
}
jwt = CkJwtW_Create();
// Build the JOSE header
jose = CkJsonObjectW_Create();
// Use RS256. Pass the string "RS384" or "RS512" to use RSA with SHA-384 or SHA-512.
success = CkJsonObjectW_AppendString(jose,L"alg",L"RS256");
success = CkJsonObjectW_AppendString(jose,L"typ",L"JWT");
success = CkJsonObjectW_AppendString(jose,L"kid",L"test-device");
// Now build the JWT claims (also known as the payload)
claims = CkJsonObjectW_Create();
success = CkJsonObjectW_AppendString(claims,L"iss",L"9646844092");
success = CkJsonObjectW_AppendString(claims,L"sub",L"test-device");
success = CkJsonObjectW_AppendString(claims,L"aud",L"https://proda.humanservices.gov.au");
// Set the timestamp of when the JWT was created to now.
curDateTime = CkJwtW_GenNumericDate(jwt,0);
success = CkJsonObjectW_AddIntAt(claims,-1,L"iat",curDateTime);
// Set the timestamp defining an expiration time (end time) for the token
// to be now + 1 hour (3600 seconds)
success = CkJsonObjectW_AddIntAt(claims,-1,L"exp",curDateTime + 3600);
// Produce the smallest possible JWT:
CkJwtW_putAutoCompact(jwt,TRUE);
// Create the JWT token. This is where the RSA signature is created.
jwtToken = CkJwtW_createJwtPk(jwt,CkJsonObjectW_emit(jose),CkJsonObjectW_emit(claims),privKey);
// ---------------------------------------------------------------------
// Build and send the POST, which should look something like this:
// POST https://vnd.proda.humanservices.gov.au/mga/sps/oauth/oauth20/token HTTP/1.1
// Content-Type: application/x-www-form-urlencoded
// Content-Length: 666
// Host: vnd.proda.humanservices.gov.au
//
// grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=<jwt>&client_id=VendorClient03
http = CkHttpW_Create();
req = CkHttpRequestW_Create();
CkHttpRequestW_putHttpVerb(req,L"POST");
CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded");
// Add the request params.
CkHttpRequestW_AddParam(req,L"grant_type",L"urn:ietf:params:oauth:grant-type:jwt-bearer");
CkHttpRequestW_AddParam(req,L"assertion",jwtToken);
CkHttpRequestW_AddParam(req,L"client_id",L"VendorClient03");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpReq(http,L"https://vnd.proda.humanservices.gov.au/mga/sps/oauth/oauth20/token",req,resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkPrivateKeyW_Dispose(privKey);
CkJwtW_Dispose(jwt);
CkJsonObjectW_Dispose(jose);
CkJsonObjectW_Dispose(claims);
CkHttpW_Dispose(http);
CkHttpRequestW_Dispose(req);
CkHttpResponseW_Dispose(resp);
return;
}
wprintf(L"Response status code = %d\n",CkHttpResponseW_getStatusCode(resp));
wprintf(L"Response body:\n");
wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
CkPrivateKeyW_Dispose(privKey);
CkJwtW_Dispose(jwt);
CkJsonObjectW_Dispose(jose);
CkJsonObjectW_Dispose(claims);
CkHttpW_Dispose(http);
CkHttpRequestW_Dispose(req);
CkHttpResponseW_Dispose(resp);
}