diff options
Diffstat (limited to 'mastoapi.c')
| -rw-r--r-- | mastoapi.c | 102 |
1 files changed, 97 insertions, 5 deletions
| @@ -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 | } |