Skip to content

Commit

Permalink
[C] Support for authentication methods (#1628)
Browse files Browse the repository at this point in the history
* added auth support in apiClient

* added httperror response in detail

* added apikey to apiClient create

* remove unnecessary print statements

* remove freeing of apiclient object from apiclient_free function

* added auth params to apiClient_free function

* free only received data and apiClient object to be freed by user
  • Loading branch information
zhemant authored and wing328 committed Jan 7, 2019
1 parent b7971e2 commit 233ce09
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@
{{/returnContainer}}
//return type
{{/returnTypeIsPrimitive}}
apiClient_free(apiClient);
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
}
{{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}}
{{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}}
{{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}}
Expand Down Expand Up @@ -380,7 +382,10 @@ end:
{{/returnType}}
{{^returnType}}
//No return type
end: apiClient_free(apiClient);
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
}
{{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}}
{{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}}
{{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,48 @@ apiClient_t *apiClient_create() {
apiClient->basePath = "{{{basePath}}}";
apiClient->dataReceived = NULL;
apiClient->response_code = 0;
#ifdef BASIC_AUTH
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
apiClient->username = NULL;
apiClient->password = NULL;
#endif // BASIC_AUTH
#ifdef OAUTH2
{{/isBasic}}
{{#isOAuth}}
apiClient->accessToken = NULL;
#endif // OAUTH2
{{/isOAuth}}
{{#isApiKey}}
apiClient->apiKeys = NULL;
{{/isApiKey}}
{{/authMethods}}
{{/hasAuthMethods}}

return apiClient;
}

void apiClient_free(apiClient_t *apiClient) {
if(apiClient->dataReceived) {
free(apiClient->dataReceived);
if(apiClient->basePath) {
free(apiClient->basePath);
}
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
if(apiClient->username) {
free(apiClient->username);
}
if(apiClient->password) {
free(apiClient->password);
}
{{/isBasic}}
{{#isOAuth}}
if(apiClient->accessToken) {
free(apiClient->accessToken);
}
{{/isOAuth}}
{{#isApiKey}}
list_free(apiClient->apiKeys);
{{/isApiKey}}
{{/authMethods}}
{{/hasAuthMethods}}
free(apiClient);
curl_global_cleanup();
}
Expand Down Expand Up @@ -254,11 +282,9 @@ void apiClient_invoke(apiClient_t *apiClient,
if(strcmp(keyValuePair->key,
"file") == 0)
{
printf("Size of fileVar - %p\n",fileVar);
memcpy(&fileVar,
keyValuePair->value,
sizeof(fileVar));
printf("Size of fileVar1 - %p\n",fileVar);
curl_mime_data(part,
fileVar->fileData,
fileVar->fileSize);
Expand Down Expand Up @@ -287,8 +313,12 @@ void apiClient_invoke(apiClient_t *apiClient,
free(headerValueToWrite);
}
}
{{#hasAuthMethods}}
{{#authMethods}}
{{#isApiKey}}
// this would only be generated for apiKey authentication
#ifdef API_KEY
if (apiClient->apiKeys != NULL)
{
list_ForEach(listEntry, apiClient->apiKeys) {
keyValuePair_t *apiKey = listEntry->data;
if((apiKey->key != NULL) &&
Expand All @@ -300,7 +330,10 @@ void apiClient_invoke(apiClient_t *apiClient,
free(headerValueToWrite);
}
}
#endif // API_KEY
}
{{/isApiKey}}
{{/authMethods}}
{{/hasAuthMethods}}

char *targetUrl =
assembleTargetUrl(apiClient->basePath,
Expand All @@ -316,19 +349,11 @@ void apiClient_invoke(apiClient_t *apiClient,
&apiClient->dataReceived);
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
// this would only be generated for OAuth2 authentication
#ifdef OAUTH2
if(apiClient->accessToken != NULL) {
// curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_easy_setopt(handle,
CURLOPT_XOAUTH2_BEARER,
apiClient->accessToken);
}
#endif


{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
// this would only be generated for basic authentication:
#ifdef BASIC_AUTH
char *authenticationToken;

if((apiClient->username != NULL) &&
Expand All @@ -351,8 +376,18 @@ void apiClient_invoke(apiClient_t *apiClient,
CURLOPT_USERPWD,
authenticationToken);
}

#endif // BASIC_AUTH
{{/isBasic}}
{{#isOAuth}}
// this would only be generated for OAuth2 authentication
if(apiClient->accessToken != NULL) {
// curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_easy_setopt(handle,
CURLOPT_XOAUTH2_BEARER,
apiClient->accessToken);
}
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}

if(bodyParameters != NULL) {
postData(handle, bodyParameters);
Expand All @@ -371,16 +406,27 @@ void apiClient_invoke(apiClient_t *apiClient,
if(res == CURLE_OK) {
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &apiClient->response_code);
} else {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
char *url,*ip,*scheme;
long port;
curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url);
curl_easy_getinfo(handle, CURLINFO_PRIMARY_IP, &ip);
curl_easy_getinfo(handle, CURLINFO_PRIMARY_PORT, &port);
curl_easy_getinfo(handle, CURLINFO_SCHEME, &scheme);
fprintf(stderr, "curl_easy_perform() failed\n\nURL: %s\nIP: %s\nPORT: %li\nSCHEME: %s\nStrERROR: %s\n",url,ip,port,scheme,
curl_easy_strerror(res));
}
#ifdef BASIC_AUTH
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
if((apiClient->username != NULL) &&
(apiClient->password != NULL) )
{
free(authenticationToken);
}
#endif // BASIC_AUTH
{{/isBasic}}
{{/authMethods}}
{{/hasAuthMethods}}

curl_easy_cleanup(handle);
if(formParameters != NULL) {
free(formString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ typedef struct apiClient_t {
char *basePath;
void *dataReceived;
long response_code;
// this would only be generated for basic authentication
#ifdef BASIC_AUTH
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
char *username;
char *password;
#endif // BASIC_AUTH
// this would only be generated for OAUTH2 authentication
#ifdef OAUTH2
{{/isBasic}}
{{#isOAuth}}
char *accessToken;
#endif // OAUTH2
#ifdef API_KEY
//this would only be generated for apiKey authentication
{{/isOAuth}}
{{#isApiKey}}
list_t *apiKeys;
#endif // API_KEY
{{/isApiKey}}
{{/authMethods}}
{{/hasAuthMethods}}
} apiClient_t;

typedef struct FileStruct
Expand Down

0 comments on commit 233ce09

Please sign in to comment.