mastoapi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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_openssl.h"
  6. #include "xs_json.h"
  7. #include "xs_io.h"
  8. #include "xs_time.h"
  9. #include "snac.h"
  10. static xs_str *random_str(void)
  11. /* just what is says in the tin */
  12. {
  13. unsigned int data[4] = {0};
  14. FILE *f;
  15. if ((f = fopen("/dev/random", "r")) != NULL) {
  16. fread(data, sizeof(data), 1, f);
  17. fclose(f);
  18. }
  19. else {
  20. data[0] = random() % 0xffffffff;
  21. data[1] = random() % 0xffffffff;
  22. data[2] = random() % 0xffffffff;
  23. data[3] = random() % 0xffffffff;
  24. }
  25. return xs_hex_enc((char *)data, sizeof(data));
  26. }
  27. int app_add(const char *id, const xs_dict *app)
  28. /* stores an app */
  29. {
  30. if (!xs_is_hex(id))
  31. return 500;
  32. int status = 201;
  33. xs *fn = xs_fmt("%s/app/", srv_basedir);
  34. FILE *f;
  35. mkdirx(fn);
  36. fn = xs_str_cat(fn, id);
  37. fn = xs_str_cat(fn, ".json");
  38. if ((f = fopen(fn, "w")) != NULL) {
  39. xs *j = xs_json_dumps_pp(app, 4);
  40. fwrite(j, strlen(j), 1, f);
  41. fclose(f);
  42. }
  43. else
  44. status = 500;
  45. return status;
  46. }
  47. xs_dict *app_get(const char *id)
  48. /* gets an app */
  49. {
  50. if (!xs_is_hex(id))
  51. return NULL;
  52. xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
  53. xs_dict *app = NULL;
  54. FILE *f;
  55. if ((f = fopen(fn, "r")) != NULL) {
  56. xs *j = xs_readall(f);
  57. fclose(f);
  58. app = xs_json_loads(j);
  59. }
  60. return app;
  61. }
  62. int app_del(const char *id)
  63. /* deletes an app */
  64. {
  65. if (!xs_is_hex(id))
  66. return -1;
  67. xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
  68. return unlink(fn);
  69. }
  70. int token_add(const char *id, const xs_dict *token)
  71. /* stores a token */
  72. {
  73. if (!xs_is_hex(id))
  74. return 500;
  75. int status = 201;
  76. xs *fn = xs_fmt("%s/token/", srv_basedir);
  77. FILE *f;
  78. mkdirx(fn);
  79. fn = xs_str_cat(fn, id);
  80. fn = xs_str_cat(fn, ".json");
  81. if ((f = fopen(fn, "w")) != NULL) {
  82. xs *j = xs_json_dumps_pp(token, 4);
  83. fwrite(j, strlen(j), 1, f);
  84. fclose(f);
  85. }
  86. else
  87. status = 500;
  88. return status;
  89. }
  90. xs_dict *token_get(const char *id)
  91. /* gets a token */
  92. {
  93. if (!xs_is_hex(id))
  94. return NULL;
  95. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  96. xs_dict *token = NULL;
  97. FILE *f;
  98. if ((f = fopen(fn, "r")) != NULL) {
  99. xs *j = xs_readall(f);
  100. fclose(f);
  101. token = xs_json_loads(j);
  102. }
  103. return token;
  104. }
  105. int token_del(const char *id)
  106. /* deletes a token */
  107. {
  108. if (!xs_is_hex(id))
  109. return -1;
  110. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  111. return unlink(fn);
  112. }
  113. const char *login_page = ""
  114. "<!DOCTYPE html>\n"
  115. "<body><h1>%s OAuth identify</h1>\n"
  116. "<div style=\"background-color: red; color: white\">%s</div>\n"
  117. "<form method=\"post\" action=\"https:/" "/%s/oauth/x-snac-login\">\n"
  118. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  119. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  120. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  121. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  122. "<input type=\"submit\" value=\"OK\">\n"
  123. "</form><p>%s</p></body>\n"
  124. "";
  125. int oauth_get_handler(const xs_dict *req, const char *q_path,
  126. char **body, int *b_size, char **ctype)
  127. {
  128. if (!xs_startswith(q_path, "/oauth/"))
  129. return 0;
  130. {
  131. xs *j = xs_json_dumps_pp(req, 4);
  132. printf("oauth get:\n%s\n", j);
  133. }
  134. int status = 404;
  135. xs_dict *msg = xs_dict_get(req, "q_vars");
  136. xs *cmd = xs_replace(q_path, "/oauth", "");
  137. srv_debug(0, xs_fmt("oauth_get_handler %s", q_path));
  138. if (strcmp(cmd, "/authorize") == 0) {
  139. const char *cid = xs_dict_get(msg, "client_id");
  140. const char *ruri = xs_dict_get(msg, "redirect_uri");
  141. const char *rtype = xs_dict_get(msg, "response_type");
  142. status = 400;
  143. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  144. xs *app = app_get(cid);
  145. if (app != NULL) {
  146. const char *host = xs_dict_get(srv_config, "host");
  147. *body = xs_fmt(login_page, host, "", host, ruri, cid, USER_AGENT);
  148. *ctype = "text/html";
  149. status = 200;
  150. srv_debug(0, xs_fmt("oauth authorize: generating login page"));
  151. }
  152. else
  153. srv_debug(0, xs_fmt("oauth authorize: bad client_id %s", cid));
  154. }
  155. else
  156. srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
  157. }
  158. return status;
  159. }
  160. int oauth_post_handler(const xs_dict *req, const char *q_path,
  161. const char *payload, int p_size,
  162. char **body, int *b_size, char **ctype)
  163. {
  164. if (!xs_startswith(q_path, "/oauth/"))
  165. return 0;
  166. {
  167. xs *j = xs_json_dumps_pp(req, 4);
  168. printf("oauth post:\n%s\n", j);
  169. }
  170. int status = 404;
  171. xs_dict *msg = xs_dict_get(req, "p_vars");
  172. xs *cmd = xs_replace(q_path, "/oauth", "");
  173. srv_debug(0, xs_fmt("oauth_post_handler %s", q_path));
  174. if (strcmp(cmd, "/x-snac-login") == 0) {
  175. const char *login = xs_dict_get(msg, "login");
  176. const char *passwd = xs_dict_get(msg, "passwd");
  177. const char *redir = xs_dict_get(msg, "redir");
  178. const char *cid = xs_dict_get(msg, "cid");
  179. const char *host = xs_dict_get(srv_config, "host");
  180. /* by default, generate another login form with an error */
  181. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, redir, cid, USER_AGENT);
  182. *ctype = "text/html";
  183. status = 200;
  184. if (login && passwd && redir && cid) {
  185. snac snac;
  186. if (user_open(&snac, login)) {
  187. /* check the login + password */
  188. if (check_password(login, passwd,
  189. xs_dict_get(snac.config, "passwd"))) {
  190. /* success! redirect to the desired uri */
  191. xs *code = random_str();
  192. xs_free(*body);
  193. *body = xs_fmt("%s?code=%s", redir, code);
  194. status = 303;
  195. srv_debug(0, xs_fmt("oauth x-snac-login: success, redirect to %s", *body));
  196. /* assign the login to the app */
  197. xs *app = app_get(cid);
  198. if (app != NULL) {
  199. app = xs_dict_set(app, "uid", login);
  200. app = xs_dict_set(app, "code", code);
  201. app_add(cid, app);
  202. }
  203. else
  204. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  205. }
  206. else
  207. srv_debug(0, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  208. user_free(&snac);
  209. }
  210. else
  211. srv_debug(0, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  212. }
  213. else
  214. srv_debug(0, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  215. }
  216. else
  217. if (strcmp(cmd, "/token") == 0) {
  218. const char *gtype = xs_dict_get(msg, "grant_type");
  219. const char *code = xs_dict_get(msg, "code");
  220. const char *cid = xs_dict_get(msg, "client_id");
  221. const char *csec = xs_dict_get(msg, "client_secret");
  222. const char *ruri = xs_dict_get(msg, "redirect_uri");
  223. if (gtype && code && cid && csec && ruri) {
  224. xs *app = app_get(cid);
  225. if (app == NULL) {
  226. status = 401;
  227. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  228. }
  229. else
  230. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  231. status = 401;
  232. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  233. }
  234. else {
  235. xs *rsp = xs_dict_new();
  236. xs *cat = xs_number_new(time(NULL));
  237. xs *tokid = random_str();
  238. rsp = xs_dict_append(rsp, "access_token", tokid);
  239. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  240. rsp = xs_dict_append(rsp, "created_at", cat);
  241. *body = xs_json_dumps_pp(rsp, 4);
  242. *ctype = "application/json";
  243. status = 200;
  244. const char *uid = xs_dict_get(app, "uid");
  245. srv_debug(0, xs_fmt("oauth token: "
  246. "successful login for %s, new token %s", uid, tokid));
  247. xs *token = xs_dict_new();
  248. token = xs_dict_append(token, "token", tokid);
  249. token = xs_dict_append(token, "client_id", cid);
  250. token = xs_dict_append(token, "client_secret", csec);
  251. token = xs_dict_append(token, "uid", uid);
  252. token = xs_dict_append(token, "code", code);
  253. token_add(tokid, token);
  254. }
  255. }
  256. else {
  257. srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
  258. status = 400;
  259. }
  260. }
  261. else
  262. if (strcmp(cmd, "/revoke") == 0) {
  263. const char *cid = xs_dict_get(msg, "client_id");
  264. const char *csec = xs_dict_get(msg, "client_secret");
  265. const char *tokid = xs_dict_get(msg, "token");
  266. if (cid && csec && tokid) {
  267. xs *token = token_get(tokid);
  268. *body = xs_str_new("{}");
  269. *ctype = "application/json";
  270. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  271. srv_debug(0, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  272. status = 403;
  273. }
  274. else {
  275. token_del(tokid);
  276. srv_debug(0, xs_fmt("oauth revoke: revoked token %s", tokid));
  277. status = 200;
  278. /* also delete the app, as it serves no purpose from now on */
  279. app_del(cid);
  280. }
  281. }
  282. else {
  283. srv_debug(0, xs_fmt("oauth revoke: invalid or unset arguments"));
  284. status = 403;
  285. }
  286. }
  287. return status;
  288. }
  289. xs_str *mastoapi_id(const xs_dict *msg)
  290. /* returns a somewhat Mastodon-compatible status id */
  291. {
  292. char tmp[256] = "";
  293. int n = 0;
  294. const char *id = xs_dict_get(msg, "id");
  295. const char *published = xs_dict_get(msg, "published");
  296. if (!xs_is_null(published)) {
  297. /* transfer all numbers from the published date */
  298. while (*published && n < sizeof(tmp) - 1) {
  299. if (*published >= '0' && *published <= '9')
  300. tmp[n++] = *published;
  301. published++;
  302. }
  303. tmp[n] = '\0';
  304. }
  305. xs *md5 = xs_md5_hex(id, strlen(id));
  306. return xs_str_cat(xs_str_new(tmp), md5);
  307. }
  308. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  309. char **body, int *b_size, char **ctype)
  310. {
  311. if (!xs_startswith(q_path, "/api/v1/"))
  312. return 0;
  313. srv_debug(0, xs_fmt("mastoapi_get_handler %s", q_path));
  314. {
  315. xs *j = xs_json_dumps_pp(req, 4);
  316. printf("mastoapi get:\n%s\n", j);
  317. }
  318. int status = 404;
  319. xs_dict *args = xs_dict_get(req, "q_vars");
  320. xs *cmd = xs_replace(q_path, "/api/v1", "");
  321. char *v;
  322. snac snac = {0};
  323. int logged_in = 0;
  324. /* if there is an authorization field, try to validate it */
  325. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  326. xs *tokid = xs_replace(v, "Bearer ", "");
  327. xs *token = token_get(tokid);
  328. if (token != NULL) {
  329. const char *uid = xs_dict_get(token, "uid");
  330. if (!xs_is_null(uid) && user_open(&snac, uid)) {
  331. logged_in = 1;
  332. srv_debug(0, xs_fmt("mastoapi auth: valid token for user %s", uid));
  333. }
  334. else
  335. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  336. }
  337. else
  338. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  339. }
  340. if (strcmp(cmd, "/accounts/verify_credentials") == 0) {
  341. if (logged_in) {
  342. xs *acct = xs_dict_new();
  343. acct = xs_dict_append(acct, "id", xs_dict_get(snac.config, "uid"));
  344. acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
  345. acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
  346. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
  347. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
  348. acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
  349. acct = xs_dict_append(acct, "url", snac.actor);
  350. xs *avatar = NULL;
  351. char *av = xs_dict_get(snac.config, "avatar");
  352. if (xs_is_null(av) || *av == '\0')
  353. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  354. else
  355. avatar = xs_dup(av);
  356. acct = xs_dict_append(acct, "avatar", avatar);
  357. *body = xs_json_dumps_pp(acct, 4);
  358. *ctype = "application/json";
  359. status = 200;
  360. }
  361. else {
  362. status = 422; // "Unprocessable entity" (no login)
  363. }
  364. }
  365. else
  366. if (strcmp(cmd, "/timelines/home") == 0) {
  367. /* the private timeline */
  368. if (logged_in) {
  369. const char *max_id = xs_dict_get(args, "max_id");
  370. const char *since_id = xs_dict_get(args, "since_id");
  371. const char *min_id = xs_dict_get(args, "min_id");
  372. const char *limit_s = xs_dict_get(args, "limit");
  373. int limit = 0;
  374. int cnt = 0;
  375. if (!xs_is_null(limit_s))
  376. limit = atoi(limit_s);
  377. if (limit == 0)
  378. limit = 20;
  379. xs *timeline = timeline_simple_list(&snac, "private", 0, XS_ALL);
  380. xs *out = xs_list_new();
  381. xs_list *p = timeline;
  382. xs_str *v;
  383. while (xs_list_iter(&p, &v) && cnt < limit) {
  384. xs *msg = NULL;
  385. /* only return entries older that max_id */
  386. if (max_id) {
  387. if (strcmp(v, max_id) == 0)
  388. max_id = NULL;
  389. continue;
  390. }
  391. /* only returns entries newer than since_id */
  392. if (since_id) {
  393. if (strcmp(v, since_id) == 0)
  394. break;
  395. }
  396. /* only returns entries newer than min_id */
  397. /* what does really "Return results immediately newer than ID" mean? */
  398. if (min_id) {
  399. if (strcmp(v, min_id) == 0)
  400. break;
  401. }
  402. /* get the entry */
  403. if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
  404. continue;
  405. /* discard non-Notes */
  406. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  407. continue;
  408. xs *actor = NULL;
  409. actor_get(&snac, xs_dict_get(msg, "attributedTo"), &actor);
  410. /* if the author is not here, discard */
  411. if (actor == NULL)
  412. continue;
  413. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  414. xs *acct = xs_dict_new();
  415. const char *display_name = xs_dict_get(actor, "name");
  416. if (xs_is_null(display_name) || *display_name == '\0')
  417. display_name = xs_dict_get(actor, "preferredUsername");
  418. const char *id = xs_dict_get(actor, "id");
  419. const char *pub = xs_dict_get(actor, "published");
  420. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  421. acct = xs_dict_append(acct, "id", acct_md5);
  422. acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
  423. acct = xs_dict_append(acct, "acct", xs_dict_get(actor, "preferredUsername"));
  424. acct = xs_dict_append(acct, "display_name", display_name);
  425. if (pub)
  426. acct = xs_dict_append(acct, "created_at", pub);
  427. acct = xs_dict_append(acct, "note", xs_dict_get(actor, "summary"));
  428. acct = xs_dict_append(acct, "url", id);
  429. xs *avatar = NULL;
  430. xs_dict *av = xs_dict_get(actor, "icon");
  431. if (xs_type(av) == XSTYPE_DICT)
  432. avatar = xs_dup(xs_dict_get(av, "url"));
  433. else
  434. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  435. acct = xs_dict_append(acct, "avatar", avatar);
  436. xs *f = xs_val_new(XSTYPE_FALSE);
  437. xs *t = xs_val_new(XSTYPE_TRUE);
  438. xs *n = xs_val_new(XSTYPE_NULL);
  439. xs *el = xs_list_new();
  440. xs *idx = NULL;
  441. xs *ixc = NULL;
  442. char *tmp;
  443. id = xs_dict_get(msg, "id");
  444. xs *mid = mastoapi_id(msg);
  445. xs *st = xs_dict_new();
  446. st = xs_dict_append(st, "id", mid);
  447. st = xs_dict_append(st, "uri", id);
  448. st = xs_dict_append(st, "url", id);
  449. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  450. st = xs_dict_append(st, "account", acct);
  451. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  452. st = xs_dict_append(st, "visibility",
  453. is_msg_public(&snac, msg) ? "public" : "private");
  454. tmp = xs_dict_get(msg, "sensitive");
  455. if (xs_is_null(tmp))
  456. tmp = f;
  457. st = xs_dict_append(st, "sensitive", tmp);
  458. tmp = xs_dict_get(msg, "summary");
  459. if (xs_is_null(tmp))
  460. tmp = "";
  461. st = xs_dict_append(st, "spoiler_text", tmp);
  462. st = xs_dict_append(st, "media_attachments", el);
  463. st = xs_dict_append(st, "mentions", el);
  464. st = xs_dict_append(st, "tags", el);
  465. st = xs_dict_append(st, "emojis", el);
  466. xs_free(idx);
  467. xs_free(ixc);
  468. idx = object_likes(id);
  469. ixc = xs_number_new(xs_list_len(idx));
  470. st = xs_dict_append(st, "favourites_count", ixc);
  471. st = xs_dict_append(st, "favourited",
  472. xs_list_in(idx, snac.md5) != -1 ? t : f);
  473. xs_free(idx);
  474. xs_free(ixc);
  475. idx = object_announces(id);
  476. ixc = xs_number_new(xs_list_len(idx));
  477. st = xs_dict_append(st, "reblogs_count", ixc);
  478. st = xs_dict_append(st, "reblogged",
  479. xs_list_in(idx, snac.md5) != -1 ? t : f);
  480. xs_free(idx);
  481. xs_free(ixc);
  482. idx = object_children(id);
  483. ixc = xs_number_new(xs_list_len(idx));
  484. st = xs_dict_append(st, "replies_count", ixc);
  485. /* default in_reply_to values */
  486. st = xs_dict_append(st, "in_reply_to_id", n);
  487. st = xs_dict_append(st, "in_reply_to_account_id", n);
  488. tmp = xs_dict_get(msg, "inReplyTo");
  489. if (!xs_is_null(tmp)) {
  490. xs *irto = NULL;
  491. if (valid_status(object_get(tmp, &irto))) {
  492. xs *irt_mid = mastoapi_id(irto);
  493. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  494. char *at = NULL;
  495. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  496. xs *at_md5 = xs_md5_hex(at, strlen(at));
  497. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  498. }
  499. }
  500. }
  501. st = xs_dict_append(st, "reblog", n);
  502. st = xs_dict_append(st, "poll", n);
  503. st = xs_dict_append(st, "card", n);
  504. st = xs_dict_append(st, "language", n);
  505. tmp = xs_dict_get(msg, "sourceContent");
  506. if (xs_is_null(tmp))
  507. tmp = "";
  508. st = xs_dict_append(st, "text", tmp);
  509. tmp = xs_dict_get(msg, "updated");
  510. if (xs_is_null(tmp))
  511. tmp = n;
  512. st = xs_dict_append(st, "edited_at", tmp);
  513. out = xs_list_append(out, st);
  514. cnt++;
  515. }
  516. *body = xs_json_dumps_pp(out, 4);
  517. *ctype = "application/json";
  518. status = 200;
  519. srv_debug(0, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  520. }
  521. else {
  522. status = 401; // unauthorized
  523. }
  524. }
  525. else
  526. if (strcmp(cmd, "/timelines/public") == 0) {
  527. /* the public timeline */
  528. }
  529. /* user cleanup */
  530. if (logged_in)
  531. user_free(&snac);
  532. return status;
  533. }
  534. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  535. const char *payload, int p_size,
  536. char **body, int *b_size, char **ctype)
  537. {
  538. if (!xs_startswith(q_path, "/api/v1/"))
  539. return 0;
  540. srv_debug(0, xs_fmt("mastoapi_post_handler %s", q_path));
  541. {
  542. xs *j = xs_json_dumps_pp(req, 4);
  543. printf("mastoapi post:\n%s\n", j);
  544. }
  545. int status = 404;
  546. xs *msg = NULL;
  547. char *i_ctype = xs_dict_get(req, "content-type");
  548. if (xs_startswith(i_ctype, "application/json"))
  549. msg = xs_json_loads(payload);
  550. else
  551. msg = xs_dup(xs_dict_get(req, "p_vars"));
  552. if (msg == NULL)
  553. return 400;
  554. {
  555. xs *j = xs_json_dumps_pp(msg, 4);
  556. printf("%s\n", j);
  557. }
  558. xs *cmd = xs_replace(q_path, "/api/v1", "");
  559. if (strcmp(cmd, "/apps") == 0) {
  560. const char *name = xs_dict_get(msg, "client_name");
  561. const char *ruri = xs_dict_get(msg, "redirect_uris");
  562. const char *scope = xs_dict_get(msg, "scope");
  563. if (xs_type(ruri) == XSTYPE_LIST)
  564. ruri = xs_dict_get(ruri, 0);
  565. if (name && ruri) {
  566. xs *app = xs_dict_new();
  567. xs *id = xs_replace_i(tid(0), ".", "");
  568. xs *cid = random_str();
  569. xs *csec = random_str();
  570. xs *vkey = random_str();
  571. app = xs_dict_append(app, "name", name);
  572. app = xs_dict_append(app, "redirect_uri", ruri);
  573. app = xs_dict_append(app, "client_id", cid);
  574. app = xs_dict_append(app, "client_secret", csec);
  575. app = xs_dict_append(app, "vapid_key", vkey);
  576. app = xs_dict_append(app, "id", id);
  577. *body = xs_json_dumps_pp(app, 4);
  578. *ctype = "application/json";
  579. status = 200;
  580. app = xs_dict_append(app, "code", "");
  581. if (scope)
  582. app = xs_dict_append(app, "scope", scope);
  583. app_add(cid, app);
  584. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  585. }
  586. }
  587. return status;
  588. }