commit 4964a56

default  ·  2023-04-08 07:09:43 +0000 UTC
parent eba6987
More mastoapi work.
3 files changed,  +88, -18
M snac.h
+3, -0
 1@@ -176,6 +176,9 @@ void httpd_connection(FILE *f)
 2         if (status == 0)
 3             status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
 4 
 5+        if (status == 0)
 6+            status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
 7+
 8         if (status == 0)
 9             status = html_get_handler(req, q_path, &body, &b_size, &ctype);
10     }
+79, -16
  1@@ -8,14 +8,40 @@
  2 
  3 #include "snac.h"
  4 
  5-int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
  6+static xs_str *random_str(void)
  7+/* just what is says in the tin */
  8+{
  9+    unsigned int data[4] = {0};
 10+    FILE *f;
 11+
 12+    if ((f = fopen("/dev/random", "r")) != NULL) {
 13+        fread(data, sizeof(data), 1, f);
 14+        fclose(f);
 15+    }
 16+    else {
 17+        data[0] = random() % 0xffffffff;
 18+        data[1] = random() % 0xffffffff;
 19+        data[2] = random() % 0xffffffff;
 20+        data[3] = random() % 0xffffffff;
 21+    }
 22+
 23+    return xs_hex_enc((char *)data, sizeof(data));
 24+}
 25+
 26+
 27+int oauth_get_handler(const xs_dict *req, const char *q_path,
 28                       char **body, int *b_size, char **ctype)
 29 {
 30     if (!xs_startswith(q_path, "/oauth/"))
 31         return 0;
 32 
 33+    {
 34+        xs *j = xs_json_dumps_pp(req, 4);
 35+        printf("oauth:\n%s\n", j);
 36+    }
 37+
 38     int status   = 404;
 39-    xs_dict *msg = xs_dict_get(req, "p_vars");
 40+    xs_dict *msg = xs_dict_get(req, "q_vars");
 41     xs *cmd      = xs_replace(q_path, "/oauth", "");
 42 
 43     if (strcmp(cmd, "/authorize") == 0) {
 44@@ -25,11 +51,32 @@ int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
 45         const char *scope = xs_dict_get(msg, "scope");
 46 
 47         if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
 48+            /* redirect to an identification page */
 49+            status = 303;
 50+//            *body  = xs_fmt("%s/test1/admin?redir=%s", srv_baseurl, ruri);
 51+            *body  = xs_fmt("%s/test1/admin", srv_baseurl);
 52         }
 53         else
 54             status = 400;
 55     }
 56-    else
 57+
 58+    return status;
 59+}
 60+
 61+
 62+int oauth_post_handler(const xs_dict *req, const char *q_path,
 63+                      const char *payload, int p_size,
 64+                      char **body, int *b_size, char **ctype)
 65+{
 66+    if (!xs_startswith(q_path, "/oauth/"))
 67+        return 0;
 68+
 69+    int status   = 404;
 70+    xs_dict *msg = xs_dict_get(req, "p_vars");
 71+    xs *cmd      = xs_replace(q_path, "/oauth", "");
 72+
 73+    printf("oauth: %s\n", q_path);
 74+
 75     if (strcmp(cmd, "/token") == 0) {
 76         const char *gtype = xs_dict_get(msg, "grant_type");
 77         const char *code  = xs_dict_get(msg, "code");
 78@@ -39,10 +86,11 @@ int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
 79         const char *scope = xs_dict_get(msg, "scope");
 80 
 81         if (gtype && code && cid && csec && ruri) {
 82-            xs *rsp = xs_dict_new();
 83-            xs *cat = xs_number_new(time(NULL));
 84+            xs *rsp   = xs_dict_new();
 85+            xs *cat   = xs_number_new(time(NULL));
 86+            xs *token = random_str();
 87 
 88-            rsp = xs_dict_append(rsp, "access_token", "abcde");
 89+            rsp = xs_dict_append(rsp, "access_token", token);
 90             rsp = xs_dict_append(rsp, "token_type",   "Bearer");
 91             rsp = xs_dict_append(rsp, "scope",        scope);
 92             rsp = xs_dict_append(rsp, "created_at",   cat);
 93@@ -56,13 +104,25 @@ int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
 94     }
 95     else
 96     if (strcmp(cmd, "/revoke") == 0) {
 97+        const char *cid   = xs_dict_get(msg, "client_id");
 98+        const char *csec  = xs_dict_get(msg, "client_secret");
 99+        const char *token = xs_dict_get(msg, "token");
100+
101+        if (cid && csec && token) {
102+            *body  = xs_str_new("{}");
103+            *ctype = "application/json";
104+            status = 200;
105+        }
106+        else
107+            status = 400;
108     }
109 
110     return status;
111 }
112 
113 
114-int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
115+int mastoapi_post_handler(const xs_dict *req, const char *q_path,
116+                      const char *payload, int p_size,
117                       char **body, int *b_size, char **ctype)
118 {
119     if (!xs_startswith(q_path, "/api/v1/"))
120@@ -96,15 +156,18 @@ int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
121         const char *ruri = xs_dict_get(msg, "redirect_uris");
122 
123         if (name && ruri) {
124-            xs *app = xs_dict_new();
125-            xs *id  = xs_replace_i(tid(0), ".", "");
126-
127-            app = xs_dict_append(app, "name", name);
128-            app = xs_dict_append(app, "redirect_uri", ruri);
129-            app = xs_dict_append(app, "client_id", "abcde");
130-            app = xs_dict_append(app, "client_secret", "abcde");
131-            app = xs_dict_append(app, "vapid_key", "abcde");
132-            app = xs_dict_append(app, "id", id);
133+            xs *app  = xs_dict_new();
134+            xs *id   = xs_replace_i(tid(0), ".", "");
135+            xs *cid  = random_str();
136+            xs *csec = random_str();
137+            xs *vkey = random_str();
138+
139+            app = xs_dict_append(app, "name",          name);
140+            app = xs_dict_append(app, "redirect_uri",  ruri);
141+            app = xs_dict_append(app, "client_id",     cid);
142+            app = xs_dict_append(app, "client_secret", csec);
143+            app = xs_dict_append(app, "vapid_key",     vkey);
144+            app = xs_dict_append(app, "id",            id);
145 
146             *body  = xs_json_dumps_pp(app, 4);
147             *ctype = "application/json";
M snac.h
+6, -2
 1@@ -224,7 +224,11 @@ int job_fifo_ready(void);
 2 void job_post(const xs_val *job, int urgent);
 3 void job_wait(xs_val **job);
 4 
 5-int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
 6+int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 7+                      const char *payload, int p_size,
 8                       char **body, int *b_size, char **ctype);
 9-int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
10+int oauth_get_handler(const xs_dict *req, const char *q_path,
11+                      char **body, int *b_size, char **ctype);
12+int oauth_post_handler(const xs_dict *req, const char *q_path,
13+                      const char *payload, int p_size,
14                       char **body, int *b_size, char **ctype);