mastoapi.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_encdec.h"
  5. #include "xs_json.h"
  6. #include "xs_io.h"
  7. #include "xs_time.h"
  8. #include "snac.h"
  9. static xs_str *random_str(void)
  10. /* just what is says in the tin */
  11. {
  12. unsigned int data[4] = {0};
  13. FILE *f;
  14. if ((f = fopen("/dev/random", "r")) != NULL) {
  15. fread(data, sizeof(data), 1, f);
  16. fclose(f);
  17. }
  18. else {
  19. data[0] = random() % 0xffffffff;
  20. data[1] = random() % 0xffffffff;
  21. data[2] = random() % 0xffffffff;
  22. data[3] = random() % 0xffffffff;
  23. }
  24. return xs_hex_enc((char *)data, sizeof(data));
  25. }
  26. int app_add(const char *id, const xs_dict *app)
  27. /* stores an app */
  28. {
  29. int status = 201;
  30. xs *fn = xs_fmt("%s/app/", srv_basedir);
  31. FILE *f;
  32. mkdirx(fn);
  33. fn = xs_str_cat(fn, id);
  34. fn = xs_str_cat(fn, ".json");
  35. if ((f = fopen(fn, "w")) != NULL) {
  36. xs *j = xs_json_dumps_pp(app, 4);
  37. fwrite(j, strlen(j), 1, f);
  38. fclose(f);
  39. }
  40. else
  41. status = 500;
  42. return status;
  43. }
  44. xs_dict *app_get(const char *id)
  45. /* gets an app */
  46. {
  47. xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
  48. xs_dict *app = NULL;
  49. FILE *f;
  50. if ((f = fopen(fn, "r")) != NULL) {
  51. xs *j = xs_readall(f);
  52. fclose(f);
  53. app = xs_json_loads(j);
  54. }
  55. return app;
  56. }
  57. const char *login_page = ""
  58. "<!DOCTYPE html>\n"
  59. "<body><h1>%s OAuth identify</h1>\n"
  60. "<div style=\"background-color: red; color: white\">%s</div>\n"
  61. "<form method=\"post\" action=\"https:/" "/%s/oauth/x-snac-login\">\n"
  62. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  63. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  64. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  65. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  66. "<input type=\"submit\" value=\"OK\">\n"
  67. "</form><p>%s</p></body>\n"
  68. "";
  69. int oauth_get_handler(const xs_dict *req, const char *q_path,
  70. char **body, int *b_size, char **ctype)
  71. {
  72. if (!xs_startswith(q_path, "/oauth/"))
  73. return 0;
  74. {
  75. xs *j = xs_json_dumps_pp(req, 4);
  76. printf("oauth get:\n%s\n", j);
  77. }
  78. int status = 404;
  79. xs_dict *msg = xs_dict_get(req, "q_vars");
  80. xs *cmd = xs_replace(q_path, "/oauth", "");
  81. srv_debug(0, xs_fmt("oauth_get_handler %s", q_path));
  82. if (strcmp(cmd, "/authorize") == 0) {
  83. const char *cid = xs_dict_get(msg, "client_id");
  84. const char *ruri = xs_dict_get(msg, "redirect_uri");
  85. const char *rtype = xs_dict_get(msg, "response_type");
  86. status = 400;
  87. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  88. xs *app = app_get(cid);
  89. if (app != NULL) {
  90. const char *host = xs_dict_get(srv_config, "host");
  91. *body = xs_fmt(login_page, host, "", host, ruri, cid, USER_AGENT);
  92. *ctype = "text/html";
  93. status = 200;
  94. srv_debug(0, xs_fmt("oauth authorize: generating login page"));
  95. }
  96. else
  97. srv_debug(0, xs_fmt("oauth authorize: bad client_id %s", cid));
  98. }
  99. else
  100. srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
  101. }
  102. return status;
  103. }
  104. int oauth_post_handler(const xs_dict *req, const char *q_path,
  105. const char *payload, int p_size,
  106. char **body, int *b_size, char **ctype)
  107. {
  108. if (!xs_startswith(q_path, "/oauth/"))
  109. return 0;
  110. {
  111. xs *j = xs_json_dumps_pp(req, 4);
  112. printf("oauth post:\n%s\n", j);
  113. }
  114. int status = 404;
  115. xs_dict *msg = xs_dict_get(req, "p_vars");
  116. xs *cmd = xs_replace(q_path, "/oauth", "");
  117. srv_debug(0, xs_fmt("oauth_post_handler %s", q_path));
  118. if (strcmp(cmd, "/x-snac-login") == 0) {
  119. const char *login = xs_dict_get(msg, "login");
  120. const char *passwd = xs_dict_get(msg, "passwd");
  121. const char *redir = xs_dict_get(msg, "redir");
  122. const char *cid = xs_dict_get(msg, "cid");
  123. const char *host = xs_dict_get(srv_config, "host");
  124. /* by default, generate another login form with an error */
  125. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, redir, cid, USER_AGENT);
  126. *ctype = "text/html";
  127. status = 200;
  128. if (login && passwd && redir && cid) {
  129. snac snac;
  130. if (user_open(&snac, login)) {
  131. /* check the login + password */
  132. if (check_password(login, passwd,
  133. xs_dict_get(snac.config, "passwd"))) {
  134. /* success! redirect to the desired uri */
  135. xs *code = random_str();
  136. xs_free(*body);
  137. *body = xs_fmt("%s?code=%s", redir, code);
  138. status = 303;
  139. srv_debug(0, xs_fmt("oauth x-snac-login: redirect to %s", *body));
  140. }
  141. else
  142. srv_debug(0, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  143. user_free(&snac);
  144. }
  145. else
  146. srv_debug(0, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  147. }
  148. else
  149. srv_debug(0, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  150. }
  151. else
  152. if (strcmp(cmd, "/token") == 0) {
  153. const char *gtype = xs_dict_get(msg, "grant_type");
  154. const char *code = xs_dict_get(msg, "code");
  155. const char *cid = xs_dict_get(msg, "client_id");
  156. const char *csec = xs_dict_get(msg, "client_secret");
  157. const char *ruri = xs_dict_get(msg, "redirect_uri");
  158. if (gtype && code && cid && csec && ruri) {
  159. xs *rsp = xs_dict_new();
  160. xs *cat = xs_number_new(time(NULL));
  161. xs *token = random_str();
  162. rsp = xs_dict_append(rsp, "access_token", token);
  163. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  164. rsp = xs_dict_append(rsp, "created_at", cat);
  165. *body = xs_json_dumps_pp(rsp, 4);
  166. *ctype = "application/json";
  167. status = 200;
  168. srv_debug(0, xs_fmt("oauth token: successful login, token %s", token));
  169. }
  170. else {
  171. srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
  172. status = 400;
  173. }
  174. }
  175. else
  176. if (strcmp(cmd, "/revoke") == 0) {
  177. const char *cid = xs_dict_get(msg, "client_id");
  178. const char *csec = xs_dict_get(msg, "client_secret");
  179. const char *token = xs_dict_get(msg, "token");
  180. if (cid && csec && token) {
  181. *body = xs_str_new("{}");
  182. *ctype = "application/json";
  183. status = 200;
  184. }
  185. else
  186. status = 400;
  187. }
  188. return status;
  189. }
  190. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  191. char **body, int *b_size, char **ctype)
  192. {
  193. if (!xs_startswith(q_path, "/api/v1/"))
  194. return 0;
  195. {
  196. xs *j = xs_json_dumps_pp(req, 4);
  197. printf("mastoapi get:\n%s\n", j);
  198. }
  199. int status = 404;
  200. xs_dict *msg = xs_dict_get(req, "q_vars");
  201. xs *cmd = xs_replace(q_path, "/api/v1", "");
  202. srv_debug(0, xs_fmt("mastoapi_get_handler %s", q_path));
  203. if (strcmp(cmd, "/accounts/verify_credentials") == 0) {
  204. }
  205. return status;
  206. }
  207. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  208. const char *payload, int p_size,
  209. char **body, int *b_size, char **ctype)
  210. {
  211. if (!xs_startswith(q_path, "/api/v1/"))
  212. return 0;
  213. {
  214. xs *j = xs_json_dumps_pp(req, 4);
  215. printf("mastoapi post:\n%s\n", j);
  216. }
  217. int status = 404;
  218. xs *msg = NULL;
  219. char *i_ctype = xs_dict_get(req, "content-type");
  220. if (xs_startswith(i_ctype, "application/json"))
  221. msg = xs_json_loads(payload);
  222. else
  223. msg = xs_dup(xs_dict_get(req, "p_vars"));
  224. if (msg == NULL)
  225. return 400;
  226. {
  227. xs *j = xs_json_dumps_pp(req, 4);
  228. printf("%s\n", j);
  229. }
  230. {
  231. xs *j = xs_json_dumps_pp(msg, 4);
  232. printf("%s\n", j);
  233. }
  234. xs *cmd = xs_replace(q_path, "/api/v1", "");
  235. if (strcmp(cmd, "/apps") == 0) {
  236. const char *name = xs_dict_get(msg, "client_name");
  237. const char *ruri = xs_dict_get(msg, "redirect_uris");
  238. if (name && ruri) {
  239. xs *app = xs_dict_new();
  240. xs *id = xs_replace_i(tid(0), ".", "");
  241. xs *cid = random_str();
  242. xs *csec = random_str();
  243. xs *vkey = random_str();
  244. app = xs_dict_append(app, "name", name);
  245. app = xs_dict_append(app, "redirect_uri", ruri);
  246. app = xs_dict_append(app, "client_id", cid);
  247. app = xs_dict_append(app, "client_secret", csec);
  248. app = xs_dict_append(app, "vapid_key", vkey);
  249. app = xs_dict_append(app, "id", id);
  250. *body = xs_json_dumps_pp(app, 4);
  251. *ctype = "application/json";
  252. status = 200;
  253. app = xs_dict_append(app, "code", "");
  254. app_add(cid, app);
  255. }
  256. }
  257. return status;
  258. }