commit eba6987

default  ·  2023-04-08 05:04:40 +0000 UTC
parent 353e393
Added some OAuth scaffold code.
3 files changed,  +103, -5
M snac.h
+4, -0
 1@@ -185,6 +185,10 @@ void httpd_connection(FILE *f)
 2             status = activitypub_post_handler(req, q_path,
 3                         payload, p_size, &body, &b_size, &ctype);
 4 
 5+        if (status == 0)
 6+            status = oauth_post_handler(req, q_path,
 7+                        payload, p_size, &body, &b_size, &ctype);
 8+
 9         if (status == 0)
10             status = mastoapi_post_handler(req, q_path,
11                         payload, p_size, &body, &b_size, &ctype);
+97, -5
  1@@ -8,17 +8,109 @@
  2 
  3 #include "snac.h"
  4 
  5-int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
  6+int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
  7                       char **body, int *b_size, char **ctype)
  8 {
  9-    int status = 404;
 10+    if (!xs_startswith(q_path, "/oauth/"))
 11+        return 0;
 12+
 13+    int status   = 404;
 14+    xs_dict *msg = xs_dict_get(req, "p_vars");
 15+    xs *cmd      = xs_replace(q_path, "/oauth", "");
 16+
 17+    if (strcmp(cmd, "/authorize") == 0) {
 18+        const char *cid   = xs_dict_get(msg, "client_id");
 19+        const char *ruri  = xs_dict_get(msg, "redirect_uri");
 20+        const char *rtype = xs_dict_get(msg, "response_type");
 21+        const char *scope = xs_dict_get(msg, "scope");
 22+
 23+        if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
 24+        }
 25+        else
 26+            status = 400;
 27+    }
 28+    else
 29+    if (strcmp(cmd, "/token") == 0) {
 30+        const char *gtype = xs_dict_get(msg, "grant_type");
 31+        const char *code  = xs_dict_get(msg, "code");
 32+        const char *cid   = xs_dict_get(msg, "client_id");
 33+        const char *csec  = xs_dict_get(msg, "client_secret");
 34+        const char *ruri  = xs_dict_get(msg, "redirect_uri");
 35+        const char *scope = xs_dict_get(msg, "scope");
 36+
 37+        if (gtype && code && cid && csec && ruri) {
 38+            xs *rsp = xs_dict_new();
 39+            xs *cat = xs_number_new(time(NULL));
 40+
 41+            rsp = xs_dict_append(rsp, "access_token", "abcde");
 42+            rsp = xs_dict_append(rsp, "token_type",   "Bearer");
 43+            rsp = xs_dict_append(rsp, "scope",        scope);
 44+            rsp = xs_dict_append(rsp, "created_at",   cat);
 45+
 46+            *body  = xs_json_dumps_pp(rsp, 4);
 47+            *ctype = "application/json";
 48+            status = 200;
 49+        }
 50+        else
 51+            status = 400;
 52+    }
 53+    else
 54+    if (strcmp(cmd, "/revoke") == 0) {
 55+    }
 56+
 57+    return status;
 58+}
 59+
 60 
 61+int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
 62+                      char **body, int *b_size, char **ctype)
 63+{
 64     if (!xs_startswith(q_path, "/api/v1/"))
 65         return 0;
 66 
 67-    xs *j = xs_json_dumps_pp(req, 4);
 68-    printf("%s\n", j);
 69-    printf("%s\n", payload);
 70+    int status    = 404;
 71+    xs *msg       = NULL;
 72+    char *i_ctype = xs_dict_get(req, "content-type");
 73+
 74+    if (xs_startswith(i_ctype, "application/json"))
 75+        msg = xs_json_loads(payload);
 76+    else
 77+        msg = xs_dup(xs_dict_get(req, "p_vars"));
 78+
 79+    if (msg == NULL)
 80+        return 400;
 81+
 82+    {
 83+        xs *j = xs_json_dumps_pp(req, 4);
 84+        printf("%s\n", j);
 85+    }
 86+    {
 87+        xs *j = xs_json_dumps_pp(msg, 4);
 88+        printf("%s\n", j);
 89+    }
 90+
 91+    xs *cmd = xs_replace(q_path, "/api/v1", "");
 92+
 93+    if (strcmp(cmd, "/apps") == 0) {
 94+        const char *name = xs_dict_get(msg, "client_name");
 95+        const char *ruri = xs_dict_get(msg, "redirect_uris");
 96+
 97+        if (name && ruri) {
 98+            xs *app = xs_dict_new();
 99+            xs *id  = xs_replace_i(tid(0), ".", "");
100+
101+            app = xs_dict_append(app, "name", name);
102+            app = xs_dict_append(app, "redirect_uri", ruri);
103+            app = xs_dict_append(app, "client_id", "abcde");
104+            app = xs_dict_append(app, "client_secret", "abcde");
105+            app = xs_dict_append(app, "vapid_key", "abcde");
106+            app = xs_dict_append(app, "id", id);
107+
108+            *body  = xs_json_dumps_pp(app, 4);
109+            *ctype = "application/json";
110+            status = 200;
111+        }
112+    }
113 
114     return status;
115 }
M snac.h
+2, -0
1@@ -226,3 +226,5 @@ void job_wait(xs_val **job);
2 
3 int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
4                       char **body, int *b_size, char **ctype);
5+int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
6+                      char **body, int *b_size, char **ctype);