From 55821a92a240b5a3fdfcc45a07535a8e143dae6c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 6 Aug 2019 19:55:04 +0200 Subject: Have the login function return the authToken --- restapi/auth.c | 24 ++++++++++++------------ restapi/auth.h | 5 +---- 2 files changed, 13 insertions(+), 16 deletions(-) (limited to 'restapi') diff --git a/restapi/auth.c b/restapi/auth.c index dbe77d7..eda92a9 100644 --- a/restapi/auth.c +++ b/restapi/auth.c @@ -10,10 +10,11 @@ #define USER_STR "user=" #define PASS_STR "&password=" -char // returns 0 if ok, greater than 0 otherwise +char * authToken = NULL; + +const char * // returns authToken if ok, NULL otherwise restapi_login(const char* username, const char* password) { - char ret = 0; http_init(); size_t user_str_len = strlen(USER_STR); size_t user_len = strlen(username); @@ -28,7 +29,7 @@ restapi_login(const char* username, const char* password) free(login_args); if (buffer == NULL) { fprintf(stderr, "Error while authenticating, http post didn't return any data.\n"); - return 1; + return NULL; } cJSON* json = cJSON_Parse(buffer); @@ -37,7 +38,6 @@ restapi_login(const char* username, const char* password) if (error_ptr != NULL) fprintf(stderr, "Json parsing error before: %s\n", error_ptr); fprintf(stderr, "Error while authenticating, couldn't parse json output :\n%s\n", buffer); - ret = 2; goto login_json_cleanup; } @@ -46,37 +46,37 @@ restapi_login(const char* username, const char* password) cJSON* data = cJSON_GetObjectItemCaseSensitive(json, "data"); if (!cJSON_IsObject(data)) { fprintf(stderr, "Error while authenticating, couldn't parse json output :\n%s\n", buffer); - ret = 4; goto login_json_cleanup; } cJSON* userId = cJSON_GetObjectItemCaseSensitive(data, "userId"); - cJSON* authToken = cJSON_GetObjectItemCaseSensitive(data, "authToken"); - if (!cJSON_IsString(userId) || userId->valuestring == NULL || !cJSON_IsString(authToken) || authToken->valuestring == NULL) { + cJSON* jauthToken = cJSON_GetObjectItemCaseSensitive(data, "authToken"); + if (!cJSON_IsString(userId) || userId->valuestring == NULL || !cJSON_IsString(jauthToken) || jauthToken->valuestring == NULL) { fprintf(stderr, "Error while authenticating, couldn't parse json output :\n%s\n", buffer); - ret = 5; goto login_json_cleanup; } - printf("userid: %s\nauthtoken: %s\n", userId->valuestring, authToken->valuestring); + printf("userid: %s\nauthtoken: %s\n", userId->valuestring, jauthToken->valuestring); http_add_header("X-User-Id", userId->valuestring); - http_add_header("X-Auth-Token", authToken->valuestring); + http_add_header("X-Auth-Token", jauthToken->valuestring); http_add_header("Content-type", "application/json"); http_add_header("Expect", ""); + authToken = malloc(strlen(jauthToken->valuestring) + 1); + strcpy(authToken, jauthToken->valuestring); } else { const cJSON* msg = cJSON_GetObjectItemCaseSensitive(json, "message"); if (cJSON_IsString(msg) && msg->valuestring != NULL) fprintf(stderr, "Error while authenticating: %s\n", msg->valuestring); else fprintf(stderr, "Error while authenticating.\n%s\n", buffer); - ret = 3; } login_json_cleanup: cJSON_Delete(json); - return ret; + return authToken; } void restapi_logout(void) { (void) http_post("/api/v1/logout", NULL); http_clean(); + free(authToken); } diff --git a/restapi/auth.h b/restapi/auth.h index ce5ea10..0378095 100644 --- a/restapi/auth.h +++ b/restapi/auth.h @@ -1,10 +1,7 @@ #ifndef RESTAPI_AUTH_H_ #define RESTAPI_AUTH_H_ -extern char * userId; -extern char * authToken; - -char // returns 0 if ok, greater than 0 otherwise +const char * // returns authToken if ok, NULL otherwise restapi_login(const char* username, const char* password); void restapi_logout(void); -- cgit v1.2.3