implement logout command

This commit is contained in:
2019-07-08 13:04:53 +02:00
parent 6dc21a365c
commit 7a15fe6752
9 changed files with 113 additions and 45 deletions

View File

@@ -18,6 +18,7 @@ SET(SOURCES
"include/dc/util.h"
"src/account.c"
"src/api.c"
"src/api-auth.c"
"src/api-channel.c"
"src/api-friends.c"
"src/apisync.c"

View File

@@ -27,6 +27,7 @@ void dc_api_signal(dc_api_t api, CURL *easy, int code);
/* internal curl stuff
*/
bool dc_api_error(json_t *j, int *code, char const **message);
dc_api_sync_t dc_api_call(dc_api_t api, char const *token,
char const *verb, char const *method,
json_t *j);
@@ -43,6 +44,12 @@ json_t *dc_api_call_sync(dc_api_t api, char const *token,
*/
bool dc_api_authenticate(dc_api_t api, dc_account_t account);
/**
* Inverse of dc_api_authenticate(). Logs the given user out, destroying the
* login token in the process.
*/
bool dc_api_logout(dc_api_t api, dc_account_t account);
/**
* Retrieve basic user information for the given account. The first
* parameter is the user account holding login info, while the second

71
libdc/src/api-auth.c Normal file
View File

@@ -0,0 +1,71 @@
#include <dc/api.h>
#include "internal.h"
bool dc_api_logout(dc_api_t api, dc_account_t account)
{
json_t *reply = NULL;
json_t *data = NULL;
data = json_object();
return_if_true(data == NULL, false);
json_object_set_new(data, "provider", json_null());
json_object_set_new(data, "voip_provider", json_null());
reply = dc_api_call_sync(api, "POST", dc_account_token(account),
"auth/logout", data
);
json_decref(data);
if (reply != NULL) {
/* TODO: parse error
*/
json_decref(reply);
return false;
}
dc_account_set_token(account, NULL);
return true;
}
bool dc_api_authenticate(dc_api_t api, dc_account_t account)
{
json_t *j = json_object(), *reply = NULL, *token = NULL;
bool ret = false;
json_object_set_new(j, "email",
json_string(dc_account_email(account))
);
json_object_set_new(j, "password",
json_string(dc_account_password(account))
);
reply = dc_api_call_sync(api, "POST", NULL, "auth/login", j);
goto_if_true(reply == NULL, cleanup);
if (dc_api_error(j, NULL, NULL)) {
return false;
}
token = json_object_get(reply, "token");
if (token == NULL || !json_is_string(token)) {
goto cleanup;
}
dc_account_set_token(account, json_string_value(token));
ret = true;
cleanup:
if (j != NULL) {
json_decref(j);
j = NULL;
}
if (reply != NULL) {
json_decref(reply);
reply = NULL;
}
return ret;
}

View File

@@ -253,7 +253,7 @@ cleanup:
return reply;
}
static bool dc_api_error(json_t *j, int *code, char const **message)
bool dc_api_error(json_t *j, int *code, char const **message)
{
return_if_true(j == NULL, false);
@@ -279,48 +279,6 @@ static bool dc_api_error(json_t *j, int *code, char const **message)
return error;
}
bool dc_api_authenticate(dc_api_t api, dc_account_t account)
{
json_t *j = json_object(), *reply = NULL, *token = NULL;
bool ret = false;
json_object_set_new(j, "email",
json_string(dc_account_email(account))
);
json_object_set_new(j, "password",
json_string(dc_account_password(account))
);
reply = dc_api_call_sync(api, "POST", NULL, "auth/login", j);
goto_if_true(reply == NULL, cleanup);
if (dc_api_error(j, NULL, NULL)) {
return false;
}
token = json_object_get(reply, "token");
if (token == NULL || !json_is_string(token)) {
goto cleanup;
}
dc_account_set_token(account, json_string_value(token));
ret = true;
cleanup:
if (j != NULL) {
json_decref(j);
j = NULL;
}
if (reply != NULL) {
json_decref(reply);
reply = NULL;
}
return ret;
}
bool dc_api_get_userinfo(dc_api_t api, dc_account_t login,
dc_account_t user)
{