diff options
| author | 2023-04-08 10:29:33 +0200 | |
|---|---|---|
| committer | 2023-04-08 10:29:33 +0200 | |
| commit | 752058bf662ce4025fd1a445625c012ab83b1edd (patch) | |
| tree | 041c29d87eba0747c9d89aabc670cf8feec70c0e | |
| parent | The /oauth/authorize URL generates a login page. (diff) | |
| download | snac2-752058bf662ce4025fd1a445625c012ab83b1edd.tar.gz snac2-752058bf662ce4025fd1a445625c012ab83b1edd.tar.xz snac2-752058bf662ce4025fd1a445625c012ab83b1edd.zip | |
More mastoapi work.
| -rw-r--r-- | mastoapi.c | 63 |
1 files changed, 56 insertions, 7 deletions
| @@ -4,6 +4,7 @@ | |||
| 4 | #include "xs.h" | 4 | #include "xs.h" |
| 5 | #include "xs_encdec.h" | 5 | #include "xs_encdec.h" |
| 6 | #include "xs_json.h" | 6 | #include "xs_json.h" |
| 7 | #include "xs_io.h" | ||
| 7 | #include "xs_time.h" | 8 | #include "xs_time.h" |
| 8 | 9 | ||
| 9 | #include "snac.h" | 10 | #include "snac.h" |
| @@ -29,6 +30,47 @@ static xs_str *random_str(void) | |||
| 29 | } | 30 | } |
| 30 | 31 | ||
| 31 | 32 | ||
| 33 | int app_add(const char *id, const xs_dict *app) | ||
| 34 | /* stores an app */ | ||
| 35 | { | ||
| 36 | int status = 201; | ||
| 37 | xs *fn = xs_fmt("%s/app/", srv_basedir); | ||
| 38 | FILE *f; | ||
| 39 | |||
| 40 | mkdirx(fn); | ||
| 41 | fn = xs_str_cat(fn, id); | ||
| 42 | fn = xs_str_cat(fn, ".json"); | ||
| 43 | |||
| 44 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 45 | xs *j = xs_json_dumps_pp(app, 4); | ||
| 46 | fwrite(j, strlen(j), 1, f); | ||
| 47 | fclose(f); | ||
| 48 | } | ||
| 49 | else | ||
| 50 | status = 500; | ||
| 51 | |||
| 52 | return status; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | xs_dict *app_get(const char *id) | ||
| 57 | /* gets an app */ | ||
| 58 | { | ||
| 59 | xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id); | ||
| 60 | xs_dict *app = NULL; | ||
| 61 | FILE *f; | ||
| 62 | |||
| 63 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 64 | xs *j = xs_readall(f); | ||
| 65 | fclose(f); | ||
| 66 | |||
| 67 | app = xs_json_loads(j); | ||
| 68 | } | ||
| 69 | |||
| 70 | return app; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 32 | const char *login_page = "" | 74 | const char *login_page = "" |
| 33 | "<!DOCTYPE html>\n" | 75 | "<!DOCTYPE html>\n" |
| 34 | "<body><h1>%s identify</h1>\n" | 76 | "<body><h1>%s identify</h1>\n" |
| @@ -36,6 +78,7 @@ const char *login_page = "" | |||
| 36 | "<p>Login: <input type=\"text\" name=\"login\"></p>\n" | 78 | "<p>Login: <input type=\"text\" name=\"login\"></p>\n" |
| 37 | "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n" | 79 | "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n" |
| 38 | "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n" | 80 | "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n" |
| 81 | "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n" | ||
| 39 | "</form><p>%s</p></body>\n" | 82 | "</form><p>%s</p></body>\n" |
| 40 | ""; | 83 | ""; |
| 41 | 84 | ||
| @@ -58,17 +101,20 @@ int oauth_get_handler(const xs_dict *req, const char *q_path, | |||
| 58 | const char *cid = xs_dict_get(msg, "client_id"); | 101 | const char *cid = xs_dict_get(msg, "client_id"); |
| 59 | const char *ruri = xs_dict_get(msg, "redirect_uri"); | 102 | const char *ruri = xs_dict_get(msg, "redirect_uri"); |
| 60 | const char *rtype = xs_dict_get(msg, "response_type"); | 103 | const char *rtype = xs_dict_get(msg, "response_type"); |
| 61 | const char *scope = xs_dict_get(msg, "scope"); | 104 | |
| 105 | status = 400; | ||
| 62 | 106 | ||
| 63 | if (cid && ruri && rtype && strcmp(rtype, "code") == 0) { | 107 | if (cid && ruri && rtype && strcmp(rtype, "code") == 0) { |
| 64 | const char *host = xs_dict_get(srv_config, "host"); | 108 | xs *app = app_get(cid); |
| 65 | 109 | ||
| 66 | *body = xs_fmt(login_page, host, host, ruri, USER_AGENT); | 110 | if (app != NULL) { |
| 67 | *ctype = "text/html"; | 111 | const char *host = xs_dict_get(srv_config, "host"); |
| 68 | status = 200; | 112 | |
| 113 | *body = xs_fmt(login_page, host, host, ruri, cid, USER_AGENT); | ||
| 114 | *ctype = "text/html"; | ||
| 115 | status = 200; | ||
| 116 | } | ||
| 69 | } | 117 | } |
| 70 | else | ||
| 71 | status = 400; | ||
| 72 | } | 118 | } |
| 73 | 119 | ||
| 74 | return status; | 120 | return status; |
| @@ -183,6 +229,9 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 183 | *body = xs_json_dumps_pp(app, 4); | 229 | *body = xs_json_dumps_pp(app, 4); |
| 184 | *ctype = "application/json"; | 230 | *ctype = "application/json"; |
| 185 | status = 200; | 231 | status = 200; |
| 232 | |||
| 233 | app = xs_dict_append(app, "code", ""); | ||
| 234 | app_add(cid, app); | ||
| 186 | } | 235 | } |
| 187 | } | 236 | } |
| 188 | 237 | ||