From 752058bf662ce4025fd1a445625c012ab83b1edd Mon Sep 17 00:00:00 2001 From: default Date: Sat, 8 Apr 2023 10:29:33 +0200 Subject: [PATCH] More mastoapi work. --- mastoapi.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/mastoapi.c b/mastoapi.c index 7e065fa..80a94af 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -4,6 +4,7 @@ #include "xs.h" #include "xs_encdec.h" #include "xs_json.h" +#include "xs_io.h" #include "xs_time.h" #include "snac.h" @@ -29,6 +30,47 @@ static xs_str *random_str(void) } +int app_add(const char *id, const xs_dict *app) +/* stores an app */ +{ + int status = 201; + xs *fn = xs_fmt("%s/app/", srv_basedir); + FILE *f; + + mkdirx(fn); + fn = xs_str_cat(fn, id); + fn = xs_str_cat(fn, ".json"); + + if ((f = fopen(fn, "w")) != NULL) { + xs *j = xs_json_dumps_pp(app, 4); + fwrite(j, strlen(j), 1, f); + fclose(f); + } + else + status = 500; + + return status; +} + + +xs_dict *app_get(const char *id) +/* gets an app */ +{ + xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id); + xs_dict *app = NULL; + FILE *f; + + if ((f = fopen(fn, "r")) != NULL) { + xs *j = xs_readall(f); + fclose(f); + + app = xs_json_loads(j); + } + + return app; +} + + const char *login_page = "" "\n" "

%s identify

\n" @@ -36,6 +78,7 @@ const char *login_page = "" "

Login:

\n" "

Password:

\n" "\n" +"\n" "

%s

\n" ""; @@ -58,17 +101,20 @@ int oauth_get_handler(const xs_dict *req, const char *q_path, const char *cid = xs_dict_get(msg, "client_id"); const char *ruri = xs_dict_get(msg, "redirect_uri"); const char *rtype = xs_dict_get(msg, "response_type"); - const char *scope = xs_dict_get(msg, "scope"); + + status = 400; if (cid && ruri && rtype && strcmp(rtype, "code") == 0) { - const char *host = xs_dict_get(srv_config, "host"); + xs *app = app_get(cid); - *body = xs_fmt(login_page, host, host, ruri, USER_AGENT); - *ctype = "text/html"; - status = 200; + if (app != NULL) { + const char *host = xs_dict_get(srv_config, "host"); + + *body = xs_fmt(login_page, host, host, ruri, cid, USER_AGENT); + *ctype = "text/html"; + status = 200; + } } - else - status = 400; } return status; @@ -183,6 +229,9 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, *body = xs_json_dumps_pp(app, 4); *ctype = "application/json"; status = 200; + + app = xs_dict_append(app, "code", ""); + app_add(cid, app); } }