Skip to content

Commit e916f8c

Browse files
zhemantwing328
authored andcommitted
[C] Support for authentication methods (OpenAPITools#1628)
* 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
1 parent ff4df8b commit e916f8c

File tree

3 files changed

+89
-37
lines changed

3 files changed

+89
-37
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@
310310
{{/returnContainer}}
311311
//return type
312312
{{/returnTypeIsPrimitive}}
313-
apiClient_free(apiClient);
313+
if (apiClient->dataReceived) {
314+
free(apiClient->dataReceived);
315+
}
314316
{{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}}
315317
{{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}}
316318
{{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}}
@@ -380,7 +382,10 @@ end:
380382
{{/returnType}}
381383
{{^returnType}}
382384
//No return type
383-
end: apiClient_free(apiClient);
385+
end:
386+
if (apiClient->dataReceived) {
387+
free(apiClient->dataReceived);
388+
}
384389
{{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}}
385390
{{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}}
386391
{{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}}

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,48 @@ apiClient_t *apiClient_create() {
1212
apiClient->basePath = "{{{basePath}}}";
1313
apiClient->dataReceived = NULL;
1414
apiClient->response_code = 0;
15-
#ifdef BASIC_AUTH
15+
{{#hasAuthMethods}}
16+
{{#authMethods}}
17+
{{#isBasic}}
1618
apiClient->username = NULL;
1719
apiClient->password = NULL;
18-
#endif // BASIC_AUTH
19-
#ifdef OAUTH2
20+
{{/isBasic}}
21+
{{#isOAuth}}
2022
apiClient->accessToken = NULL;
21-
#endif // OAUTH2
23+
{{/isOAuth}}
24+
{{#isApiKey}}
25+
apiClient->apiKeys = NULL;
26+
{{/isApiKey}}
27+
{{/authMethods}}
28+
{{/hasAuthMethods}}
29+
2230
return apiClient;
2331
}
2432

2533
void apiClient_free(apiClient_t *apiClient) {
26-
if(apiClient->dataReceived) {
27-
free(apiClient->dataReceived);
34+
if(apiClient->basePath) {
35+
free(apiClient->basePath);
36+
}
37+
{{#hasAuthMethods}}
38+
{{#authMethods}}
39+
{{#isBasic}}
40+
if(apiClient->username) {
41+
free(apiClient->username);
42+
}
43+
if(apiClient->password) {
44+
free(apiClient->password);
2845
}
46+
{{/isBasic}}
47+
{{#isOAuth}}
48+
if(apiClient->accessToken) {
49+
free(apiClient->accessToken);
50+
}
51+
{{/isOAuth}}
52+
{{#isApiKey}}
53+
list_free(apiClient->apiKeys);
54+
{{/isApiKey}}
55+
{{/authMethods}}
56+
{{/hasAuthMethods}}
2957
free(apiClient);
3058
curl_global_cleanup();
3159
}
@@ -254,11 +282,9 @@ void apiClient_invoke(apiClient_t *apiClient,
254282
if(strcmp(keyValuePair->key,
255283
"file") == 0)
256284
{
257-
printf("Size of fileVar - %p\n",fileVar);
258285
memcpy(&fileVar,
259286
keyValuePair->value,
260287
sizeof(fileVar));
261-
printf("Size of fileVar1 - %p\n",fileVar);
262288
curl_mime_data(part,
263289
fileVar->fileData,
264290
fileVar->fileSize);
@@ -287,8 +313,12 @@ void apiClient_invoke(apiClient_t *apiClient,
287313
free(headerValueToWrite);
288314
}
289315
}
316+
{{#hasAuthMethods}}
317+
{{#authMethods}}
318+
{{#isApiKey}}
290319
// this would only be generated for apiKey authentication
291-
#ifdef API_KEY
320+
if (apiClient->apiKeys != NULL)
321+
{
292322
list_ForEach(listEntry, apiClient->apiKeys) {
293323
keyValuePair_t *apiKey = listEntry->data;
294324
if((apiKey->key != NULL) &&
@@ -300,7 +330,10 @@ void apiClient_invoke(apiClient_t *apiClient,
300330
free(headerValueToWrite);
301331
}
302332
}
303-
#endif // API_KEY
333+
}
334+
{{/isApiKey}}
335+
{{/authMethods}}
336+
{{/hasAuthMethods}}
304337

305338
char *targetUrl =
306339
assembleTargetUrl(apiClient->basePath,
@@ -316,19 +349,11 @@ void apiClient_invoke(apiClient_t *apiClient,
316349
&apiClient->dataReceived);
317350
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
318351
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
319-
// this would only be generated for OAuth2 authentication
320-
#ifdef OAUTH2
321-
if(apiClient->accessToken != NULL) {
322-
// curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
323-
curl_easy_setopt(handle,
324-
CURLOPT_XOAUTH2_BEARER,
325-
apiClient->accessToken);
326-
}
327-
#endif
328-
329352

353+
{{#hasAuthMethods}}
354+
{{#authMethods}}
355+
{{#isBasic}}
330356
// this would only be generated for basic authentication:
331-
#ifdef BASIC_AUTH
332357
char *authenticationToken;
333358

334359
if((apiClient->username != NULL) &&
@@ -351,8 +376,18 @@ void apiClient_invoke(apiClient_t *apiClient,
351376
CURLOPT_USERPWD,
352377
authenticationToken);
353378
}
354-
355-
#endif // BASIC_AUTH
379+
{{/isBasic}}
380+
{{#isOAuth}}
381+
// this would only be generated for OAuth2 authentication
382+
if(apiClient->accessToken != NULL) {
383+
// curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
384+
curl_easy_setopt(handle,
385+
CURLOPT_XOAUTH2_BEARER,
386+
apiClient->accessToken);
387+
}
388+
{{/isOAuth}}
389+
{{/authMethods}}
390+
{{/hasAuthMethods}}
356391

357392
if(bodyParameters != NULL) {
358393
postData(handle, bodyParameters);
@@ -371,16 +406,27 @@ void apiClient_invoke(apiClient_t *apiClient,
371406
if(res == CURLE_OK) {
372407
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &apiClient->response_code);
373408
} else {
374-
fprintf(stderr, "curl_easy_perform() failed: %s\n",
409+
char *url,*ip,*scheme;
410+
long port;
411+
curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url);
412+
curl_easy_getinfo(handle, CURLINFO_PRIMARY_IP, &ip);
413+
curl_easy_getinfo(handle, CURLINFO_PRIMARY_PORT, &port);
414+
curl_easy_getinfo(handle, CURLINFO_SCHEME, &scheme);
415+
fprintf(stderr, "curl_easy_perform() failed\n\nURL: %s\nIP: %s\nPORT: %li\nSCHEME: %s\nStrERROR: %s\n",url,ip,port,scheme,
375416
curl_easy_strerror(res));
376417
}
377-
#ifdef BASIC_AUTH
418+
{{#hasAuthMethods}}
419+
{{#authMethods}}
420+
{{#isBasic}}
378421
if((apiClient->username != NULL) &&
379422
(apiClient->password != NULL) )
380423
{
381424
free(authenticationToken);
382425
}
383-
#endif // BASIC_AUTH
426+
{{/isBasic}}
427+
{{/authMethods}}
428+
{{/hasAuthMethods}}
429+
384430
curl_easy_cleanup(handle);
385431
if(formParameters != NULL) {
386432
free(formString);

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ typedef struct apiClient_t {
1212
char *basePath;
1313
void *dataReceived;
1414
long response_code;
15-
// this would only be generated for basic authentication
16-
#ifdef BASIC_AUTH
15+
{{#hasAuthMethods}}
16+
{{#authMethods}}
17+
{{#isBasic}}
1718
char *username;
1819
char *password;
19-
#endif // BASIC_AUTH
20-
// this would only be generated for OAUTH2 authentication
21-
#ifdef OAUTH2
20+
{{/isBasic}}
21+
{{#isOAuth}}
2222
char *accessToken;
23-
#endif // OAUTH2
24-
#ifdef API_KEY
25-
//this would only be generated for apiKey authentication
23+
{{/isOAuth}}
24+
{{#isApiKey}}
2625
list_t *apiKeys;
27-
#endif // API_KEY
26+
{{/isApiKey}}
27+
{{/authMethods}}
28+
{{/hasAuthMethods}}
2829
} apiClient_t;
2930

3031
typedef struct FileStruct

0 commit comments

Comments
 (0)