summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2023-04-08 09:09:43 +0200
committerGravatar default2023-04-08 09:09:43 +0200
commit4964a564560fb81a505179ae7d9730d2231a743c (patch)
treef8061836eec76123af0b325f3b59e2b215e2a21f
parentAdded some OAuth scaffold code. (diff)
downloadsnac2-4964a564560fb81a505179ae7d9730d2231a743c.tar.gz
snac2-4964a564560fb81a505179ae7d9730d2231a743c.tar.xz
snac2-4964a564560fb81a505179ae7d9730d2231a743c.zip
More mastoapi work.
-rw-r--r--httpd.c3
-rw-r--r--mastoapi.c95
-rw-r--r--snac.h8
3 files changed, 88 insertions, 18 deletions
diff --git a/httpd.c b/httpd.c
index e220d29..6d7b76e 100644
--- a/httpd.c
+++ b/httpd.c
@@ -177,6 +177,9 @@ void httpd_connection(FILE *f)
177 status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype); 177 status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
178 178
179 if (status == 0) 179 if (status == 0)
180 status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
181
182 if (status == 0)
180 status = html_get_handler(req, q_path, &body, &b_size, &ctype); 183 status = html_get_handler(req, q_path, &body, &b_size, &ctype);
181 } 184 }
182 else 185 else
diff --git a/mastoapi.c b/mastoapi.c
index 9f9de13..06a74cd 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -8,14 +8,40 @@
8 8
9#include "snac.h" 9#include "snac.h"
10 10
11int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size, 11static xs_str *random_str(void)
12/* just what is says in the tin */
13{
14 unsigned int data[4] = {0};
15 FILE *f;
16
17 if ((f = fopen("/dev/random", "r")) != NULL) {
18 fread(data, sizeof(data), 1, f);
19 fclose(f);
20 }
21 else {
22 data[0] = random() % 0xffffffff;
23 data[1] = random() % 0xffffffff;
24 data[2] = random() % 0xffffffff;
25 data[3] = random() % 0xffffffff;
26 }
27
28 return xs_hex_enc((char *)data, sizeof(data));
29}
30
31
32int oauth_get_handler(const xs_dict *req, const char *q_path,
12 char **body, int *b_size, char **ctype) 33 char **body, int *b_size, char **ctype)
13{ 34{
14 if (!xs_startswith(q_path, "/oauth/")) 35 if (!xs_startswith(q_path, "/oauth/"))
15 return 0; 36 return 0;
16 37
38 {
39 xs *j = xs_json_dumps_pp(req, 4);
40 printf("oauth:\n%s\n", j);
41 }
42
17 int status = 404; 43 int status = 404;
18 xs_dict *msg = xs_dict_get(req, "p_vars"); 44 xs_dict *msg = xs_dict_get(req, "q_vars");
19 xs *cmd = xs_replace(q_path, "/oauth", ""); 45 xs *cmd = xs_replace(q_path, "/oauth", "");
20 46
21 if (strcmp(cmd, "/authorize") == 0) { 47 if (strcmp(cmd, "/authorize") == 0) {
@@ -25,11 +51,32 @@ int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
25 const char *scope = xs_dict_get(msg, "scope"); 51 const char *scope = xs_dict_get(msg, "scope");
26 52
27 if (cid && ruri && rtype && strcmp(rtype, "code") == 0) { 53 if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
54 /* redirect to an identification page */
55 status = 303;
56// *body = xs_fmt("%s/test1/admin?redir=%s", srv_baseurl, ruri);
57 *body = xs_fmt("%s/test1/admin", srv_baseurl);
28 } 58 }
29 else 59 else
30 status = 400; 60 status = 400;
31 } 61 }
32 else 62
63 return status;
64}
65
66
67int oauth_post_handler(const xs_dict *req, const char *q_path,
68 const char *payload, int p_size,
69 char **body, int *b_size, char **ctype)
70{
71 if (!xs_startswith(q_path, "/oauth/"))
72 return 0;
73
74 int status = 404;
75 xs_dict *msg = xs_dict_get(req, "p_vars");
76 xs *cmd = xs_replace(q_path, "/oauth", "");
77
78 printf("oauth: %s\n", q_path);
79
33 if (strcmp(cmd, "/token") == 0) { 80 if (strcmp(cmd, "/token") == 0) {
34 const char *gtype = xs_dict_get(msg, "grant_type"); 81 const char *gtype = xs_dict_get(msg, "grant_type");
35 const char *code = xs_dict_get(msg, "code"); 82 const char *code = xs_dict_get(msg, "code");
@@ -39,10 +86,11 @@ int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
39 const char *scope = xs_dict_get(msg, "scope"); 86 const char *scope = xs_dict_get(msg, "scope");
40 87
41 if (gtype && code && cid && csec && ruri) { 88 if (gtype && code && cid && csec && ruri) {
42 xs *rsp = xs_dict_new(); 89 xs *rsp = xs_dict_new();
43 xs *cat = xs_number_new(time(NULL)); 90 xs *cat = xs_number_new(time(NULL));
91 xs *token = random_str();
44 92
45 rsp = xs_dict_append(rsp, "access_token", "abcde"); 93 rsp = xs_dict_append(rsp, "access_token", token);
46 rsp = xs_dict_append(rsp, "token_type", "Bearer"); 94 rsp = xs_dict_append(rsp, "token_type", "Bearer");
47 rsp = xs_dict_append(rsp, "scope", scope); 95 rsp = xs_dict_append(rsp, "scope", scope);
48 rsp = xs_dict_append(rsp, "created_at", cat); 96 rsp = xs_dict_append(rsp, "created_at", cat);
@@ -56,13 +104,25 @@ int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
56 } 104 }
57 else 105 else
58 if (strcmp(cmd, "/revoke") == 0) { 106 if (strcmp(cmd, "/revoke") == 0) {
107 const char *cid = xs_dict_get(msg, "client_id");
108 const char *csec = xs_dict_get(msg, "client_secret");
109 const char *token = xs_dict_get(msg, "token");
110
111 if (cid && csec && token) {
112 *body = xs_str_new("{}");
113 *ctype = "application/json";
114 status = 200;
115 }
116 else
117 status = 400;
59 } 118 }
60 119
61 return status; 120 return status;
62} 121}
63 122
64 123
65int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size, 124int mastoapi_post_handler(const xs_dict *req, const char *q_path,
125 const char *payload, int p_size,
66 char **body, int *b_size, char **ctype) 126 char **body, int *b_size, char **ctype)
67{ 127{
68 if (!xs_startswith(q_path, "/api/v1/")) 128 if (!xs_startswith(q_path, "/api/v1/"))
@@ -96,15 +156,18 @@ int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
96 const char *ruri = xs_dict_get(msg, "redirect_uris"); 156 const char *ruri = xs_dict_get(msg, "redirect_uris");
97 157
98 if (name && ruri) { 158 if (name && ruri) {
99 xs *app = xs_dict_new(); 159 xs *app = xs_dict_new();
100 xs *id = xs_replace_i(tid(0), ".", ""); 160 xs *id = xs_replace_i(tid(0), ".", "");
101 161 xs *cid = random_str();
102 app = xs_dict_append(app, "name", name); 162 xs *csec = random_str();
103 app = xs_dict_append(app, "redirect_uri", ruri); 163 xs *vkey = random_str();
104 app = xs_dict_append(app, "client_id", "abcde"); 164
105 app = xs_dict_append(app, "client_secret", "abcde"); 165 app = xs_dict_append(app, "name", name);
106 app = xs_dict_append(app, "vapid_key", "abcde"); 166 app = xs_dict_append(app, "redirect_uri", ruri);
107 app = xs_dict_append(app, "id", id); 167 app = xs_dict_append(app, "client_id", cid);
168 app = xs_dict_append(app, "client_secret", csec);
169 app = xs_dict_append(app, "vapid_key", vkey);
170 app = xs_dict_append(app, "id", id);
108 171
109 *body = xs_json_dumps_pp(app, 4); 172 *body = xs_json_dumps_pp(app, 4);
110 *ctype = "application/json"; 173 *ctype = "application/json";
diff --git a/snac.h b/snac.h
index 438768a..f1960a3 100644
--- a/snac.h
+++ b/snac.h
@@ -224,7 +224,11 @@ int job_fifo_ready(void);
224void job_post(const xs_val *job, int urgent); 224void job_post(const xs_val *job, int urgent);
225void job_wait(xs_val **job); 225void 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(const xs_dict *req, const char *q_path,
228 const char *payload, int p_size,
228 char **body, int *b_size, char **ctype); 229 char **body, int *b_size, char **ctype);
229int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size, 230int oauth_get_handler(const xs_dict *req, const char *q_path,
231 char **body, int *b_size, char **ctype);
232int oauth_post_handler(const xs_dict *req, const char *q_path,
233 const char *payload, int p_size,
230 char **body, int *b_size, char **ctype); 234 char **body, int *b_size, char **ctype);