Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C] Support for authentication methods #1628

Merged
merged 7 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@ 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);
}
free(apiClient);
//free(apiClient);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhemant maybe simply remove this line instead of commenting it out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified the code in new commits, now in API I free the dataReceived and leave apiClient object as it is for the user to free. So If someone doesn't want to reuse apiClient, instead of calling free function for multiple parameters, only 1 function will do it.

curl_global_cleanup();
}

Expand Down Expand Up @@ -254,11 +262,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 +293,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 +310,10 @@ void apiClient_invoke(apiClient_t *apiClient,
free(headerValueToWrite);
}
}
#endif // API_KEY
}
{{/isApiKey}}
{{/authMethods}}
{{/hasAuthMethods}}

char *targetUrl =
assembleTargetUrl(apiClient->basePath,
Expand All @@ -316,19 +329,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 +356,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 +386,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