aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--close_direct_conversations/main.c2
-rw-r--r--restapi/auth.c24
-rw-r--r--restapi/auth.h5
3 files changed, 14 insertions, 17 deletions
diff --git a/close_direct_conversations/main.c b/close_direct_conversations/main.c
index 82bf851..9c723f9 100644
--- a/close_direct_conversations/main.c
+++ b/close_direct_conversations/main.c
@@ -28,7 +28,7 @@ int main(void)
if (password == NULL)
password = common_cli_get_password();
- if (restapi_login(login, password) == 0) {
+ if (restapi_login(login, password) != NULL) {
struct subscription* subscriptions = restapi_subscriptions_get();
printf("Active direct conversations :\n");
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);