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 all 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 @@ -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