aboutsummaryrefslogtreecommitdiff
path: root/restapi
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--restapi/CMakeLists.txt5
-rw-r--r--restapi/auth.c80
-rw-r--r--restapi/auth.h11
-rw-r--r--restapi/im.c58
-rw-r--r--restapi/im.h7
5 files changed, 161 insertions, 0 deletions
diff --git a/restapi/CMakeLists.txt b/restapi/CMakeLists.txt
new file mode 100644
index 0000000..45a22c2
--- /dev/null
+++ b/restapi/CMakeLists.txt
@@ -0,0 +1,5 @@
+file(GLOB_RECURSE SOURCES *.c)
+
+ADD_LIBRARY(restapi STATIC ${SOURCES})
+add_dependencies(restapi cjson-build)
+target_link_libraries(restapi common)
diff --git a/restapi/auth.c b/restapi/auth.c
new file mode 100644
index 0000000..e3f1b74
--- /dev/null
+++ b/restapi/auth.c
@@ -0,0 +1,80 @@
+#include <cjson/cJSON.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "auth.h"
+#include "common/config.h"
+#include "common/http.h"
+
+#define USER_STR "user="
+#define PASS_STR "&password="
+
+char // returns 0 if ok, greater than 0 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);
+ size_t pass_str_len = strlen(PASS_STR);
+ size_t pass_len = strlen(password);
+ char* login_args = malloc(user_str_len + user_len + pass_str_len + pass_len + 1);
+ strcpy(login_args, USER_STR);
+ strcpy(login_args + user_str_len, username);
+ strcpy(login_args + user_str_len + user_len, PASS_STR);
+ strcpy(login_args + user_str_len + user_len + pass_str_len, password);
+ const char* buffer = http_post("/api/v1/login", login_args);
+ free(login_args);
+ if (buffer == NULL) {
+ fprintf(stderr, "Error while authenticating, http post didn't return any data.\n");
+ return 1;
+ }
+
+ cJSON* json = cJSON_Parse(buffer);
+ if (json == NULL) {
+ const char *error_ptr = cJSON_GetErrorPtr();
+ 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;
+ }
+
+ const cJSON* status = cJSON_GetObjectItemCaseSensitive(json, "status");
+ if (cJSON_IsString(status) && status->valuestring != NULL && strcmp(status->valuestring, "success") == 0) {
+ 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) {
+ 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);
+ http_add_header("X-User-Id", userId->valuestring);
+ http_add_header("X-Auth-Token", authToken->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;
+}
+
+void restapi_logout(void)
+{
+ (void) http_post("/api/v1/logout", NULL);
+ http_clean();
+}
diff --git a/restapi/auth.h b/restapi/auth.h
new file mode 100644
index 0000000..ce5ea10
--- /dev/null
+++ b/restapi/auth.h
@@ -0,0 +1,11 @@
+#ifndef RESTAPI_AUTH_H_
+#define RESTAPI_AUTH_H_
+
+extern char * userId;
+extern char * authToken;
+
+char // returns 0 if ok, greater than 0 otherwise
+restapi_login(const char* username, const char* password);
+void restapi_logout(void);
+
+#endif
diff --git a/restapi/im.c b/restapi/im.c
new file mode 100644
index 0000000..6b9f75c
--- /dev/null
+++ b/restapi/im.c
@@ -0,0 +1,58 @@
+#include <cjson/cJSON.h>
+#include <curl/curl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "common/http.h"
+#include "im.h"
+
+#define LOGIN_ARG_PRE "{ \"username\": \""
+#define LOGIN_ARG_POST "\" }"
+
+char // returns 0 if ok, greater than 0 otherwise
+restapi_im_close(const char* username)
+{
+ char ret = 0;
+
+ size_t pre_len = strlen(LOGIN_ARG_PRE);
+ size_t user_len = strlen(username);
+ size_t post_len = strlen(LOGIN_ARG_POST);
+ char* login_args = malloc(pre_len + user_len + post_len + 1);
+ strcpy(login_args, LOGIN_ARG_PRE);
+ strcpy(login_args + pre_len, username);
+ strcpy(login_args + pre_len + user_len, LOGIN_ARG_POST);
+ http_add_header("Content-type", "application/json");
+ const char* buffer = http_post("/api/v1/im.close", login_args);
+ free(login_args);
+
+ if (buffer == NULL) {
+ fprintf(stderr, "Error while im_close, http post didn't return any data.\n");
+ return 1;
+ }
+
+ cJSON* json = cJSON_Parse(buffer);
+ if (json == NULL) {
+ const char *error_ptr = cJSON_GetErrorPtr();
+ if (error_ptr != NULL)
+ fprintf(stderr, "Json parsing error before: %s\n", error_ptr);
+ fprintf(stderr, "Error while im_close, couldn't parse json output :\n%s\n", buffer);
+ ret = 2;
+ goto login_json_cleanup;
+ }
+
+ const cJSON* status = cJSON_GetObjectItemCaseSensitive(json, "status");
+ if (cJSON_IsString(status) && status->valuestring != NULL && strcmp(status->valuestring, "success") == 0) {
+ printf("user %s removed\n", username);
+ } else {
+ const cJSON* msg = cJSON_GetObjectItemCaseSensitive(json, "message");
+ if (cJSON_IsString(msg) && msg->valuestring != NULL)
+ fprintf(stderr, "Error while im_close: %s\n", msg->valuestring);
+ else
+ fprintf(stderr, "Error while im_close.\n%s\n", buffer);
+ ret = 3;
+ }
+login_json_cleanup:
+ cJSON_Delete(json);
+ return ret;
+}
diff --git a/restapi/im.h b/restapi/im.h
new file mode 100644
index 0000000..fe85860
--- /dev/null
+++ b/restapi/im.h
@@ -0,0 +1,7 @@
+#ifndef RESTAPI_IM_H_
+#define RESTAPI_IM_H_
+
+char // returns 0 if ok, greater than 0 otherwise
+restapi_im_close(const char* username);
+
+#endif