diff options
| author | 2023-04-08 07:04:40 +0200 | |
|---|---|---|
| committer | 2023-04-08 07:04:40 +0200 | |
| commit | eba6987fd55c2cc94393c61e6bbd86514e14993b (patch) | |
| tree | 0d1b2112e57bae091bc2bee8d3c77fcbd9d2319a | |
| parent | New file mastoapi.c. (diff) | |
| download | snac2-eba6987fd55c2cc94393c61e6bbd86514e14993b.tar.gz snac2-eba6987fd55c2cc94393c61e6bbd86514e14993b.tar.xz snac2-eba6987fd55c2cc94393c61e6bbd86514e14993b.zip | |
Added some OAuth scaffold code.
| -rw-r--r-- | httpd.c | 4 | ||||
| -rw-r--r-- | mastoapi.c | 102 | ||||
| -rw-r--r-- | snac.h | 2 |
3 files changed, 103 insertions, 5 deletions
| @@ -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 | ||
| @@ -8,17 +8,109 @@ | |||
| 8 | 8 | ||
| 9 | #include "snac.h" | 9 | #include "snac.h" |
| 10 | 10 | ||
| 11 | int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size, | 11 | int 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 | ||
| 65 | int 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 | } |
| @@ -226,3 +226,5 @@ void job_wait(xs_val **job); | |||
| 226 | 226 | ||
| 227 | int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size, | 227 | int 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); |
| 229 | int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size, | ||
| 230 | char **body, int *b_size, char **ctype); | ||