aboutsummaryrefslogtreecommitdiff
path: root/restapi/auth.c
diff options
context:
space:
mode:
authorJulien Dessaux2019-08-06 19:55:04 +0200
committerJulien Dessaux2019-08-06 19:55:04 +0200
commit55821a92a240b5a3fdfcc45a07535a8e143dae6c (patch)
treecb396b01dcdce44b6a30e8259aeaff9e4b092525 /restapi/auth.c
parentMigrated the reading of login and password from cli to the common lib (diff)
downloadrocket-cli-client-55821a92a240b5a3fdfcc45a07535a8e143dae6c.tar.gz
rocket-cli-client-55821a92a240b5a3fdfcc45a07535a8e143dae6c.tar.bz2
rocket-cli-client-55821a92a240b5a3fdfcc45a07535a8e143dae6c.zip
Have the login function return the authToken
Diffstat (limited to '')
-rw-r--r--restapi/auth.c24
1 files changed, 12 insertions, 12 deletions
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);
}