summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2023-04-08 07:04:40 +0200
committerGravatar default2023-04-08 07:04:40 +0200
commiteba6987fd55c2cc94393c61e6bbd86514e14993b (patch)
tree0d1b2112e57bae091bc2bee8d3c77fcbd9d2319a
parentNew file mastoapi.c. (diff)
downloadsnac2-eba6987fd55c2cc94393c61e6bbd86514e14993b.tar.gz
snac2-eba6987fd55c2cc94393c61e6bbd86514e14993b.tar.xz
snac2-eba6987fd55c2cc94393c61e6bbd86514e14993b.zip
Added some OAuth scaffold code.
-rw-r--r--httpd.c4
-rw-r--r--mastoapi.c102
-rw-r--r--snac.h2
3 files changed, 103 insertions, 5 deletions
diff --git a/httpd.c b/httpd.c
index 183e194..e220d29 100644
--- a/httpd.c
+++ b/httpd.c
@@ -186,6 +186,10 @@ void httpd_connection(FILE *f)
186 payload, p_size, &body, &b_size, &ctype); 186 payload, p_size, &body, &b_size, &ctype);
187 187
188 if (status == 0) 188 if (status == 0)
189 status = oauth_post_handler(req, q_path,
190 payload, p_size, &body, &b_size, &ctype);
191
192 if (status == 0)
189 status = mastoapi_post_handler(req, q_path, 193 status = mastoapi_post_handler(req, q_path,
190 payload, p_size, &body, &b_size, &ctype); 194 payload, p_size, &body, &b_size, &ctype);
191 195
diff --git a/mastoapi.c b/mastoapi.c
index 1e4e309..9f9de13 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -8,17 +8,109 @@
8 8
9#include "snac.h" 9#include "snac.h"
10 10
11int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size, 11int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
12 char **body, int *b_size, char **ctype) 12 char **body, int *b_size, char **ctype)
13{ 13{
14 int status = 404; 14 if (!xs_startswith(q_path, "/oauth/"))
15 return 0;
16
17 int status = 404;
18 xs_dict *msg = xs_dict_get(req, "p_vars");
19 xs *cmd = xs_replace(q_path, "/oauth", "");
20
21 if (strcmp(cmd, "/authorize") == 0) {
22 const char *cid = xs_dict_get(msg, "client_id");
23 const char *ruri = xs_dict_get(msg, "redirect_uri");
24 const char *rtype = xs_dict_get(msg, "response_type");
25 const char *scope = xs_dict_get(msg, "scope");
26
27 if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
28 }
29 else
30 status = 400;
31 }
32 else
33 if (strcmp(cmd, "/token") == 0) {
34 const char *gtype = xs_dict_get(msg, "grant_type");
35 const char *code = xs_dict_get(msg, "code");
36 const char *cid = xs_dict_get(msg, "client_id");
37 const char *csec = xs_dict_get(msg, "client_secret");
38 const char *ruri = xs_dict_get(msg, "redirect_uri");
39 const char *scope = xs_dict_get(msg, "scope");
40
41 if (gtype && code && cid && csec && ruri) {
42 xs *rsp = xs_dict_new();
43 xs *cat = xs_number_new(time(NULL));
44
45 rsp = xs_dict_append(rsp, "access_token", "abcde");
46 rsp = xs_dict_append(rsp, "token_type", "Bearer");
47 rsp = xs_dict_append(rsp, "scope", scope);
48 rsp = xs_dict_append(rsp, "created_at", cat);
49
50 *body = xs_json_dumps_pp(rsp, 4);
51 *ctype = "application/json";
52 status = 200;
53 }
54 else
55 status = 400;
56 }
57 else
58 if (strcmp(cmd, "/revoke") == 0) {
59 }
60
61 return status;
62}
63
15 64
65int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
66 char **body, int *b_size, char **ctype)
67{
16 if (!xs_startswith(q_path, "/api/v1/")) 68 if (!xs_startswith(q_path, "/api/v1/"))
17 return 0; 69 return 0;
18 70
19 xs *j = xs_json_dumps_pp(req, 4); 71 int status = 404;
20 printf("%s\n", j); 72 xs *msg = NULL;
21 printf("%s\n", payload); 73 char *i_ctype = xs_dict_get(req, "content-type");
74
75 if (xs_startswith(i_ctype, "application/json"))
76 msg = xs_json_loads(payload);
77 else
78 msg = xs_dup(xs_dict_get(req, "p_vars"));
79
80 if (msg == NULL)
81 return 400;
82
83 {
84 xs *j = xs_json_dumps_pp(req, 4);
85 printf("%s\n", j);
86 }
87 {
88 xs *j = xs_json_dumps_pp(msg, 4);
89 printf("%s\n", j);
90 }
91
92 xs *cmd = xs_replace(q_path, "/api/v1", "");
93
94 if (strcmp(cmd, "/apps") == 0) {
95 const char *name = xs_dict_get(msg, "client_name");
96 const char *ruri = xs_dict_get(msg, "redirect_uris");
97
98 if (name && ruri) {
99 xs *app = xs_dict_new();
100 xs *id = xs_replace_i(tid(0), ".", "");
101
102 app = xs_dict_append(app, "name", name);
103 app = xs_dict_append(app, "redirect_uri", ruri);
104 app = xs_dict_append(app, "client_id", "abcde");
105 app = xs_dict_append(app, "client_secret", "abcde");
106 app = xs_dict_append(app, "vapid_key", "abcde");
107 app = xs_dict_append(app, "id", id);
108
109 *body = xs_json_dumps_pp(app, 4);
110 *ctype = "application/json";
111 status = 200;
112 }
113 }
22 114
23 return status; 115 return status;
24} 116}
diff --git a/snac.h b/snac.h
index b0ff33c..438768a 100644
--- a/snac.h
+++ b/snac.h
@@ -226,3 +226,5 @@ void job_wait(xs_val **job);
226 226
227int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size, 227int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
228 char **body, int *b_size, char **ctype); 228 char **body, int *b_size, char **ctype);
229int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
230 char **body, int *b_size, char **ctype);