diff options
Diffstat (limited to 'clients/client.c')
| -rw-r--r-- | clients/client.c | 49 |
1 files changed, 41 insertions, 8 deletions
diff --git a/clients/client.c b/clients/client.c index 8fb604b..fd119f1 100644 --- a/clients/client.c +++ b/clients/client.c @@ -361,18 +361,12 @@ char *client_rpc(struct client_conn *c, const char *cmd, const char *session, return client_read_line(c); } -int client_login(struct client_conn *c, const char *user, const char *password, - char **session_out, char **err_out) +static int session_open(struct client_conn *c, const char *args, + char **session_out, char **err_out) { *session_out = NULL; *err_out = NULL; - char *args = client_make_login_args(user, password); - if (!args) { - *err_out = xstrdup("could not build login request"); - return -1; - } char *resp = client_rpc(c, "session.open", NULL, 0, args); - free(args); if (!resp) { *err_out = xstrdup(client_last_error()); return -1; @@ -396,6 +390,45 @@ int client_login(struct client_conn *c, const char *user, const char *password, return 0; } +int client_login(struct client_conn *c, const char *user, const char *password, + char **session_out, char **err_out) +{ + char *args = client_make_login_args(user, password); + if (!args) { + *session_out = NULL; + *err_out = xstrdup("could not build login request"); + return -1; + } + int rc = session_open(c, args, session_out, err_out); + free(args); + return rc; +} + +int client_token_login(struct client_conn *c, const char *token, + char **session_out, char **err_out) +{ + yyjson_mut_doc *d = yyjson_mut_doc_new(NULL); + if (!d) { + *session_out = NULL; + *err_out = xstrdup("out of memory"); + return -1; + } + yyjson_mut_val *o = yyjson_mut_obj(d); + yyjson_mut_doc_set_root(d, o); + yyjson_mut_obj_add_strcpy(d, o, "method", "token"); + yyjson_mut_obj_add_strcpy(d, o, "token", token); + char *args = yyjson_mut_write(d, 0, NULL); + yyjson_mut_doc_free(d); + if (!args) { + *session_out = NULL; + *err_out = xstrdup("could not build token request"); + return -1; + } + int rc = session_open(c, args, session_out, err_out); + free(args); + return rc; +} + int client_ok(const char *response) { if (!response) |
