commit 752058b
default
·
2023-04-08 08:29:33 +0000 UTC
parent bbbce7a
More mastoapi work.
1 files changed,
+56,
-7
+56,
-7
1@@ -4,6 +4,7 @@
2 #include "xs.h"
3 #include "xs_encdec.h"
4 #include "xs_json.h"
5+#include "xs_io.h"
6 #include "xs_time.h"
7
8 #include "snac.h"
9@@ -29,6 +30,47 @@ static xs_str *random_str(void)
10 }
11
12
13+int app_add(const char *id, const xs_dict *app)
14+/* stores an app */
15+{
16+ int status = 201;
17+ xs *fn = xs_fmt("%s/app/", srv_basedir);
18+ FILE *f;
19+
20+ mkdirx(fn);
21+ fn = xs_str_cat(fn, id);
22+ fn = xs_str_cat(fn, ".json");
23+
24+ if ((f = fopen(fn, "w")) != NULL) {
25+ xs *j = xs_json_dumps_pp(app, 4);
26+ fwrite(j, strlen(j), 1, f);
27+ fclose(f);
28+ }
29+ else
30+ status = 500;
31+
32+ return status;
33+}
34+
35+
36+xs_dict *app_get(const char *id)
37+/* gets an app */
38+{
39+ xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
40+ xs_dict *app = NULL;
41+ FILE *f;
42+
43+ if ((f = fopen(fn, "r")) != NULL) {
44+ xs *j = xs_readall(f);
45+ fclose(f);
46+
47+ app = xs_json_loads(j);
48+ }
49+
50+ return app;
51+}
52+
53+
54 const char *login_page = ""
55 "<!DOCTYPE html>\n"
56 "<body><h1>%s identify</h1>\n"
57@@ -36,6 +78,7 @@ const char *login_page = ""
58 "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
59 "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
60 "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
61+"<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
62 "</form><p>%s</p></body>\n"
63 "";
64
65@@ -58,17 +101,20 @@ int oauth_get_handler(const xs_dict *req, const char *q_path,
66 const char *cid = xs_dict_get(msg, "client_id");
67 const char *ruri = xs_dict_get(msg, "redirect_uri");
68 const char *rtype = xs_dict_get(msg, "response_type");
69- const char *scope = xs_dict_get(msg, "scope");
70+
71+ status = 400;
72
73 if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
74- const char *host = xs_dict_get(srv_config, "host");
75+ xs *app = app_get(cid);
76
77- *body = xs_fmt(login_page, host, host, ruri, USER_AGENT);
78- *ctype = "text/html";
79- status = 200;
80+ if (app != NULL) {
81+ const char *host = xs_dict_get(srv_config, "host");
82+
83+ *body = xs_fmt(login_page, host, host, ruri, cid, USER_AGENT);
84+ *ctype = "text/html";
85+ status = 200;
86+ }
87 }
88- else
89- status = 400;
90 }
91
92 return status;
93@@ -183,6 +229,9 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
94 *body = xs_json_dumps_pp(app, 4);
95 *ctype = "application/json";
96 status = 200;
97+
98+ app = xs_dict_append(app, "code", "");
99+ app_add(cid, app);
100 }
101 }
102