implement logout command
This commit is contained in:
parent
6dc21a365c
commit
7a15fe6752
@ -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"
|
||||
|
@ -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
71
libdc/src/api-auth.c
Normal 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;
|
||||
}
|
@ -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)
|
||||
{
|
||||
|
@ -19,6 +19,7 @@ SET(SOURCES
|
||||
"src/input.c"
|
||||
"src/keycodes.c"
|
||||
"src/login.c"
|
||||
"src/logout.c"
|
||||
"src/mainwindow.c"
|
||||
"src/msg.c"
|
||||
"src/ncdc.c"
|
||||
|
@ -26,7 +26,8 @@ ncdc_commands_t *ncdc_find_cmd(ncdc_commands_t *cmds, wchar_t const *name);
|
||||
|
||||
bool ncdc_cmd_friends(ncdc_mainwindow_t n, size_t ac, wchar_t **av);
|
||||
bool ncdc_cmd_login(ncdc_mainwindow_t n, size_t ac, wchar_t **av);
|
||||
bool ncdc_cmd_quit(ncdc_mainwindow_t n, size_t ac, wchar_t **av);
|
||||
bool ncdc_cmd_logout(ncdc_mainwindow_t n, size_t ac, wchar_t **av);
|
||||
bool ncdc_cmd_msg(ncdc_mainwindow_t n, size_t ac, wchar_t **av);
|
||||
bool ncdc_cmd_quit(ncdc_mainwindow_t n, size_t ac, wchar_t **av);
|
||||
|
||||
#endif
|
||||
|
@ -4,6 +4,7 @@ ncdc_commands_t cmds[] = {
|
||||
{ L"/friend", ncdc_cmd_friends },
|
||||
{ L"/friends", ncdc_cmd_friends },
|
||||
{ L"/login", ncdc_cmd_login },
|
||||
{ L"/logout", ncdc_cmd_logout },
|
||||
{ L"/msg", ncdc_cmd_msg },
|
||||
{ L"/quit", ncdc_cmd_quit },
|
||||
{ NULL, NULL }
|
||||
|
@ -39,7 +39,7 @@ bool ncdc_cmd_login(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
current_account = acc;
|
||||
current_account = dc_ref(acc);
|
||||
LOG(n, L"login: %ls: authentication successful", av[1]);
|
||||
ret = true;
|
||||
|
||||
|
28
ncdc/src/logout.c
Normal file
28
ncdc/src/logout.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <ncdc/cmds.h>
|
||||
#include <ncdc/ncdc.h>
|
||||
#include <ncdc/config.h>
|
||||
|
||||
bool ncdc_cmd_logout(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
|
||||
{
|
||||
bool ret = false;
|
||||
|
||||
goto_if_true(current_account == NULL ||
|
||||
!dc_account_has_token(current_account), error);
|
||||
|
||||
ret = dc_api_logout(api, current_account);
|
||||
if (!ret) {
|
||||
LOG(n, L"logout: failed to log out the current account");
|
||||
goto error;
|
||||
}
|
||||
|
||||
g_hash_table_remove(accounts, dc_account_fullname(current_account));
|
||||
|
||||
dc_unref(current_account);
|
||||
current_account = NULL;
|
||||
|
||||
LOG(n, L"logout: successfully logged out");
|
||||
|
||||
error:
|
||||
|
||||
return ret;
|
||||
}
|
Loading…
Reference in New Issue
Block a user