mastoapi.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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=\"hidden\" name=\"state\" value=\"%s\">\n"
  123. "<input type=\"submit\" value=\"OK\">\n"
  124. "</form><p>%s</p></body>\n"
  125. "";
  126. int oauth_get_handler(const xs_dict *req, const char *q_path,
  127. char **body, int *b_size, char **ctype)
  128. {
  129. if (!xs_startswith(q_path, "/oauth/"))
  130. return 0;
  131. /* {
  132. xs *j = xs_json_dumps_pp(req, 4);
  133. printf("oauth get:\n%s\n", j);
  134. }*/
  135. int status = 404;
  136. xs_dict *msg = xs_dict_get(req, "q_vars");
  137. xs *cmd = xs_replace(q_path, "/oauth", "");
  138. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  139. if (strcmp(cmd, "/authorize") == 0) {
  140. const char *cid = xs_dict_get(msg, "client_id");
  141. const char *ruri = xs_dict_get(msg, "redirect_uri");
  142. const char *rtype = xs_dict_get(msg, "response_type");
  143. const char *state = xs_dict_get(msg, "state");
  144. status = 400;
  145. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  146. xs *app = app_get(cid);
  147. if (app != NULL) {
  148. const char *host = xs_dict_get(srv_config, "host");
  149. if (xs_is_null(state))
  150. state = "";
  151. *body = xs_fmt(login_page, host, "", host, ruri, cid, state, USER_AGENT);
  152. *ctype = "text/html";
  153. status = 200;
  154. srv_debug(0, xs_fmt("oauth authorize: generating login page"));
  155. }
  156. else
  157. srv_debug(0, xs_fmt("oauth authorize: bad client_id %s", cid));
  158. }
  159. else
  160. srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
  161. }
  162. return status;
  163. }
  164. int oauth_post_handler(const xs_dict *req, const char *q_path,
  165. const char *payload, int p_size,
  166. char **body, int *b_size, char **ctype)
  167. {
  168. if (!xs_startswith(q_path, "/oauth/"))
  169. return 0;
  170. /* {
  171. xs *j = xs_json_dumps_pp(req, 4);
  172. printf("oauth post:\n%s\n", j);
  173. }*/
  174. int status = 404;
  175. xs_dict *msg = xs_dict_get(req, "p_vars");
  176. xs *cmd = xs_replace(q_path, "/oauth", "");
  177. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  178. if (strcmp(cmd, "/x-snac-login") == 0) {
  179. const char *login = xs_dict_get(msg, "login");
  180. const char *passwd = xs_dict_get(msg, "passwd");
  181. const char *redir = xs_dict_get(msg, "redir");
  182. const char *cid = xs_dict_get(msg, "cid");
  183. const char *state = xs_dict_get(msg, "state");
  184. const char *host = xs_dict_get(srv_config, "host");
  185. /* by default, generate another login form with an error */
  186. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, redir, cid, state, USER_AGENT);
  187. *ctype = "text/html";
  188. status = 200;
  189. if (login && passwd && redir && cid) {
  190. snac snac;
  191. if (user_open(&snac, login)) {
  192. /* check the login + password */
  193. if (check_password(login, passwd,
  194. xs_dict_get(snac.config, "passwd"))) {
  195. /* success! redirect to the desired uri */
  196. xs *code = random_str();
  197. xs_free(*body);
  198. *body = xs_fmt("%s?code=%s", redir, code);
  199. status = 303;
  200. /* if there is a state, add it */
  201. if (!xs_is_null(state) && *state) {
  202. *body = xs_str_cat(*body, "&state=");
  203. *body = xs_str_cat(*body, state);
  204. }
  205. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  206. login, *body));
  207. /* assign the login to the app */
  208. xs *app = app_get(cid);
  209. if (app != NULL) {
  210. app = xs_dict_set(app, "uid", login);
  211. app = xs_dict_set(app, "code", code);
  212. app_add(cid, app);
  213. }
  214. else
  215. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  216. }
  217. else
  218. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  219. user_free(&snac);
  220. }
  221. else
  222. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  223. }
  224. else
  225. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  226. }
  227. else
  228. if (strcmp(cmd, "/token") == 0) {
  229. const char *gtype = xs_dict_get(msg, "grant_type");
  230. const char *code = xs_dict_get(msg, "code");
  231. const char *cid = xs_dict_get(msg, "client_id");
  232. const char *csec = xs_dict_get(msg, "client_secret");
  233. const char *ruri = xs_dict_get(msg, "redirect_uri");
  234. xs *wrk = NULL;
  235. /* no client_secret? check if it's inside an authorization header
  236. (AndStatus does it this way) */
  237. if (xs_is_null(csec)) {
  238. const char *auhdr = xs_dict_get(req, "authorization");
  239. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  240. xs *s1 = xs_replace(auhdr, "Basic ", "");
  241. int size;
  242. xs *s2 = xs_base64_dec(s1, &size);
  243. if (!xs_is_null(s2)) {
  244. xs *l1 = xs_split(s2, ":");
  245. if (xs_list_len(l1) == 2) {
  246. wrk = xs_dup(xs_list_get(l1, 1));
  247. csec = wrk;
  248. }
  249. }
  250. }
  251. }
  252. if (gtype && code && cid && csec && ruri) {
  253. xs *app = app_get(cid);
  254. if (app == NULL) {
  255. status = 401;
  256. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  257. }
  258. else
  259. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  260. status = 401;
  261. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  262. }
  263. else {
  264. xs *rsp = xs_dict_new();
  265. xs *cat = xs_number_new(time(NULL));
  266. xs *tokid = random_str();
  267. rsp = xs_dict_append(rsp, "access_token", tokid);
  268. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  269. rsp = xs_dict_append(rsp, "created_at", cat);
  270. *body = xs_json_dumps_pp(rsp, 4);
  271. *ctype = "application/json";
  272. status = 200;
  273. const char *uid = xs_dict_get(app, "uid");
  274. srv_debug(0, xs_fmt("oauth token: "
  275. "successful login for %s, new token %s", uid, tokid));
  276. xs *token = xs_dict_new();
  277. token = xs_dict_append(token, "token", tokid);
  278. token = xs_dict_append(token, "client_id", cid);
  279. token = xs_dict_append(token, "client_secret", csec);
  280. token = xs_dict_append(token, "uid", uid);
  281. token = xs_dict_append(token, "code", code);
  282. token_add(tokid, token);
  283. }
  284. }
  285. else {
  286. srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
  287. status = 400;
  288. }
  289. }
  290. else
  291. if (strcmp(cmd, "/revoke") == 0) {
  292. const char *cid = xs_dict_get(msg, "client_id");
  293. const char *csec = xs_dict_get(msg, "client_secret");
  294. const char *tokid = xs_dict_get(msg, "token");
  295. if (cid && csec && tokid) {
  296. xs *token = token_get(tokid);
  297. *body = xs_str_new("{}");
  298. *ctype = "application/json";
  299. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  300. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  301. status = 403;
  302. }
  303. else {
  304. token_del(tokid);
  305. srv_debug(0, xs_fmt("oauth revoke: revoked token %s", tokid));
  306. status = 200;
  307. /* also delete the app, as it serves no purpose from now on */
  308. app_del(cid);
  309. }
  310. }
  311. else {
  312. srv_debug(0, xs_fmt("oauth revoke: invalid or unset arguments"));
  313. status = 403;
  314. }
  315. }
  316. return status;
  317. }
  318. xs_str *mastoapi_id(const xs_dict *msg)
  319. /* returns a somewhat Mastodon-compatible status id */
  320. {
  321. const char *id = xs_dict_get(msg, "id");
  322. xs *md5 = xs_md5_hex(id, strlen(id));
  323. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  324. }
  325. #define MID_TO_MD5(id) (id + 10)
  326. xs_dict *mastoapi_account(const xs_dict *actor)
  327. /* converts an ActivityPub actor to a Mastodon account */
  328. {
  329. xs_dict *acct = xs_dict_new();
  330. const char *display_name = xs_dict_get(actor, "name");
  331. if (xs_is_null(display_name) || *display_name == '\0')
  332. display_name = xs_dict_get(actor, "preferredUsername");
  333. const char *id = xs_dict_get(actor, "id");
  334. const char *pub = xs_dict_get(actor, "published");
  335. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  336. acct = xs_dict_append(acct, "id", acct_md5);
  337. acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
  338. acct = xs_dict_append(acct, "acct", xs_dict_get(actor, "preferredUsername"));
  339. acct = xs_dict_append(acct, "display_name", display_name);
  340. if (pub)
  341. acct = xs_dict_append(acct, "created_at", pub);
  342. else {
  343. /* unset created_at crashes Tusky, so lie like a mf */
  344. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  345. acct = xs_dict_append(acct, "created_at", date);
  346. }
  347. acct = xs_dict_append(acct, "note", xs_dict_get(actor, "summary"));
  348. acct = xs_dict_append(acct, "url", id);
  349. xs *avatar = NULL;
  350. xs_dict *av = xs_dict_get(actor, "icon");
  351. if (xs_type(av) == XSTYPE_DICT)
  352. avatar = xs_dup(xs_dict_get(av, "url"));
  353. else
  354. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  355. acct = xs_dict_append(acct, "avatar", avatar);
  356. return acct;
  357. }
  358. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  359. /* converts an ActivityPub note to a Mastodon status */
  360. {
  361. xs *actor = NULL;
  362. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  363. /* if the author is not here, discard */
  364. if (actor == NULL)
  365. return NULL;
  366. xs *acct = mastoapi_account(actor);
  367. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  368. xs *f = xs_val_new(XSTYPE_FALSE);
  369. xs *t = xs_val_new(XSTYPE_TRUE);
  370. xs *n = xs_val_new(XSTYPE_NULL);
  371. xs *el = xs_list_new();
  372. xs *idx = NULL;
  373. xs *ixc = NULL;
  374. char *tmp;
  375. char *id = xs_dict_get(msg, "id");
  376. xs *mid = mastoapi_id(msg);
  377. xs_dict *st = xs_dict_new();
  378. st = xs_dict_append(st, "id", mid);
  379. st = xs_dict_append(st, "uri", id);
  380. st = xs_dict_append(st, "url", id);
  381. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  382. st = xs_dict_append(st, "account", acct);
  383. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  384. st = xs_dict_append(st, "visibility",
  385. is_msg_public(snac, msg) ? "public" : "private");
  386. tmp = xs_dict_get(msg, "sensitive");
  387. if (xs_is_null(tmp))
  388. tmp = f;
  389. st = xs_dict_append(st, "sensitive", tmp);
  390. tmp = xs_dict_get(msg, "summary");
  391. if (xs_is_null(tmp))
  392. tmp = "";
  393. st = xs_dict_append(st, "spoiler_text", tmp);
  394. /* create the list of attachments */
  395. xs *matt = xs_list_new();
  396. xs_list *att = xs_dict_get(msg, "attachment");
  397. xs_str *aobj;
  398. while (xs_list_iter(&att, &aobj)) {
  399. const char *mtype = xs_dict_get(aobj, "mediaType");
  400. if (!xs_is_null(mtype) && xs_startswith(mtype, "image/")) {
  401. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  402. xs *matte = xs_dict_new();
  403. matte = xs_dict_append(matte, "id", matteid);
  404. matte = xs_dict_append(matte, "type", "image");
  405. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  406. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  407. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  408. const char *name = xs_dict_get(aobj, "name");
  409. if (xs_is_null(name))
  410. name = "";
  411. matte = xs_dict_append(matte, "description", name);
  412. matt = xs_list_append(matt, matte);
  413. }
  414. }
  415. st = xs_dict_append(st, "media_attachments", matt);
  416. st = xs_dict_append(st, "mentions", el);
  417. st = xs_dict_append(st, "tags", el);
  418. st = xs_dict_append(st, "emojis", el);
  419. xs_free(idx);
  420. xs_free(ixc);
  421. idx = object_likes(id);
  422. ixc = xs_number_new(xs_list_len(idx));
  423. st = xs_dict_append(st, "favourites_count", ixc);
  424. st = xs_dict_append(st, "favourited",
  425. xs_list_in(idx, snac->md5) != -1 ? t : f);
  426. xs_free(idx);
  427. xs_free(ixc);
  428. idx = object_announces(id);
  429. ixc = xs_number_new(xs_list_len(idx));
  430. st = xs_dict_append(st, "reblogs_count", ixc);
  431. st = xs_dict_append(st, "reblogged",
  432. xs_list_in(idx, snac->md5) != -1 ? t : f);
  433. xs_free(idx);
  434. xs_free(ixc);
  435. idx = object_children(id);
  436. ixc = xs_number_new(xs_list_len(idx));
  437. st = xs_dict_append(st, "replies_count", ixc);
  438. /* default in_reply_to values */
  439. st = xs_dict_append(st, "in_reply_to_id", n);
  440. st = xs_dict_append(st, "in_reply_to_account_id", n);
  441. tmp = xs_dict_get(msg, "inReplyTo");
  442. if (!xs_is_null(tmp)) {
  443. xs *irto = NULL;
  444. if (valid_status(object_get(tmp, &irto))) {
  445. xs *irt_mid = mastoapi_id(irto);
  446. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  447. char *at = NULL;
  448. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  449. xs *at_md5 = xs_md5_hex(at, strlen(at));
  450. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  451. }
  452. }
  453. }
  454. st = xs_dict_append(st, "reblog", n);
  455. st = xs_dict_append(st, "poll", n);
  456. st = xs_dict_append(st, "card", n);
  457. st = xs_dict_append(st, "language", n);
  458. tmp = xs_dict_get(msg, "sourceContent");
  459. if (xs_is_null(tmp))
  460. tmp = "";
  461. st = xs_dict_append(st, "text", tmp);
  462. tmp = xs_dict_get(msg, "updated");
  463. if (xs_is_null(tmp))
  464. tmp = n;
  465. st = xs_dict_append(st, "edited_at", tmp);
  466. return st;
  467. }
  468. int process_auth_token(snac *snac, const xs_dict *req)
  469. /* processes an authorization token, if there is one */
  470. {
  471. int logged_in = 0;
  472. char *v;
  473. /* if there is an authorization field, try to validate it */
  474. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  475. xs *tokid = xs_replace(v, "Bearer ", "");
  476. xs *token = token_get(tokid);
  477. if (token != NULL) {
  478. const char *uid = xs_dict_get(token, "uid");
  479. if (!xs_is_null(uid) && user_open(snac, uid)) {
  480. logged_in = 1;
  481. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  482. }
  483. else
  484. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  485. }
  486. else
  487. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  488. }
  489. return logged_in;
  490. }
  491. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  492. char **body, int *b_size, char **ctype)
  493. {
  494. if (!xs_startswith(q_path, "/api/v1/"))
  495. return 0;
  496. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  497. /* {
  498. xs *j = xs_json_dumps_pp(req, 4);
  499. printf("mastoapi get:\n%s\n", j);
  500. }*/
  501. int status = 404;
  502. xs_dict *args = xs_dict_get(req, "q_vars");
  503. xs *cmd = xs_replace(q_path, "/api/v1", "");
  504. snac snac1 = {0};
  505. int logged_in = process_auth_token(&snac1, req);
  506. if (strcmp(cmd, "/accounts/verify_credentials") == 0) {
  507. if (logged_in) {
  508. xs *acct = xs_dict_new();
  509. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  510. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  511. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  512. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  513. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  514. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  515. acct = xs_dict_append(acct, "url", snac1.actor);
  516. xs *avatar = NULL;
  517. char *av = xs_dict_get(snac1.config, "avatar");
  518. if (xs_is_null(av) || *av == '\0')
  519. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  520. else
  521. avatar = xs_dup(av);
  522. acct = xs_dict_append(acct, "avatar", avatar);
  523. *body = xs_json_dumps_pp(acct, 4);
  524. *ctype = "application/json";
  525. status = 200;
  526. }
  527. else {
  528. status = 422; // "Unprocessable entity" (no login)
  529. }
  530. }
  531. else
  532. if (strcmp(cmd, "/accounts/relationships") == 0) {
  533. /* find if an account is followed, blocked, etc. */
  534. /* the account to get relationships about is in args "id[]" */
  535. /* dummy by now */
  536. if (logged_in) {
  537. *body = xs_dup("[]");
  538. *ctype = "application/json";
  539. status = 200;
  540. }
  541. }
  542. else
  543. if (xs_startswith(cmd, "/accounts/")) {
  544. /* account-related information */
  545. xs *l = xs_split(cmd, "/");
  546. const char *uid = xs_list_get(l, 2);
  547. const char *opt = xs_list_get(l, 3);
  548. if (uid != NULL) {
  549. snac snac2;
  550. xs *out = NULL;
  551. xs *actor = NULL;
  552. /* is it a local user? */
  553. if (user_open(&snac2, uid)) {
  554. if (opt == NULL) {
  555. /* account information */
  556. actor = msg_actor(&snac2);
  557. out = mastoapi_account(actor);
  558. }
  559. else
  560. if (strcmp(opt, "statuses") == 0) {
  561. /* the public list of posts of a user */
  562. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  563. xs_list *p = timeline;
  564. xs_str *v;
  565. out = xs_list_new();
  566. while (xs_list_iter(&p, &v)) {
  567. xs *msg = NULL;
  568. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  569. /* add only posts by the author */
  570. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  571. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  572. xs *st = mastoapi_status(&snac2, msg);
  573. out = xs_list_append(out, st);
  574. }
  575. }
  576. }
  577. }
  578. user_free(&snac2);
  579. }
  580. else {
  581. /* try the uid as the md5 of a possibly loaded actor */
  582. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  583. if (opt == NULL) {
  584. /* account information */
  585. out = mastoapi_account(actor);
  586. }
  587. else
  588. if (strcmp(opt, "statuses") == 0) {
  589. /* we don't serve statuses of others; return the empty list */
  590. out = xs_list_new();
  591. }
  592. }
  593. }
  594. if (out != NULL) {
  595. *body = xs_json_dumps_pp(out, 4);
  596. *ctype = "application/json";
  597. status = 200;
  598. }
  599. }
  600. }
  601. else
  602. if (strcmp(cmd, "/timelines/home") == 0) {
  603. /* the private timeline */
  604. if (logged_in) {
  605. const char *max_id = xs_dict_get(args, "max_id");
  606. const char *since_id = xs_dict_get(args, "since_id");
  607. const char *min_id = xs_dict_get(args, "min_id");
  608. const char *limit_s = xs_dict_get(args, "limit");
  609. int limit = 0;
  610. int cnt = 0;
  611. if (!xs_is_null(limit_s))
  612. limit = atoi(limit_s);
  613. if (limit == 0)
  614. limit = 20;
  615. xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
  616. xs *out = xs_list_new();
  617. xs_list *p = timeline;
  618. xs_str *v;
  619. while (xs_list_iter(&p, &v) && cnt < limit) {
  620. xs *msg = NULL;
  621. /* only return entries older that max_id */
  622. if (max_id) {
  623. if (strcmp(v, max_id) == 0)
  624. max_id = NULL;
  625. continue;
  626. }
  627. /* only returns entries newer than since_id */
  628. if (since_id) {
  629. if (strcmp(v, since_id) == 0)
  630. break;
  631. }
  632. /* only returns entries newer than min_id */
  633. /* what does really "Return results immediately newer than ID" mean? */
  634. if (min_id) {
  635. if (strcmp(v, min_id) == 0)
  636. break;
  637. }
  638. /* get the entry */
  639. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  640. continue;
  641. /* discard non-Notes */
  642. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  643. continue;
  644. /* convert the Note into a Mastodon status */
  645. xs *st = mastoapi_status(&snac1, msg);
  646. if (st != NULL)
  647. out = xs_list_append(out, st);
  648. cnt++;
  649. }
  650. *body = xs_json_dumps_pp(out, 4);
  651. *ctype = "application/json";
  652. status = 200;
  653. {
  654. xs *j = xs_json_loads(*body);
  655. if (j == NULL) {
  656. srv_log(xs_fmt("mastoapi timeline: bad JSON"));
  657. srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
  658. }
  659. }
  660. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  661. }
  662. else {
  663. status = 401; // unauthorized
  664. }
  665. }
  666. else
  667. if (strcmp(cmd, "/timelines/public") == 0) {
  668. /* the public timeline (public timelines for all users) */
  669. /* TBD */
  670. *body = xs_dup("[]");
  671. *ctype = "application/json";
  672. status = 200;
  673. }
  674. else
  675. if (strcmp(cmd, "/conversations") == 0) {
  676. /* TBD */
  677. *body = xs_dup("[]");
  678. *ctype = "application/json";
  679. status = 200;
  680. }
  681. else
  682. if (strcmp(cmd, "/notifications") == 0) {
  683. if (logged_in) {
  684. xs *l = notify_list(&snac1, 0);
  685. xs *out = xs_list_new();
  686. xs_list *p = l;
  687. xs_dict *v;
  688. while (xs_list_iter(&p, &v)) {
  689. const char *type = xs_dict_get(v, "type");
  690. const char *objid = xs_dict_get(v, "objid");
  691. xs *actor = NULL;
  692. xs *entry = NULL;
  693. if (!valid_status(object_get(xs_dict_get(v, "actor"), &actor)))
  694. continue;
  695. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  696. continue;
  697. /* convert the type */
  698. if (strcmp(type, "Like") == 0)
  699. type = "favourite";
  700. else
  701. if (strcmp(type, "Announce") == 0)
  702. type = "reblog";
  703. else
  704. if (strcmp(type, "Follow") == 0)
  705. type = "follow";
  706. else
  707. if (strcmp(type, "Create") == 0)
  708. type = "mention";
  709. else
  710. continue;
  711. xs *mn = xs_dict_new();
  712. mn = xs_dict_append(mn, "type", type);
  713. xs *id = xs_replace(xs_dict_get(v, "id"), ".", "");
  714. mn = xs_dict_append(mn, "id", id);
  715. mn = xs_dict_append(mn, "created_at", xs_dict_get(v, "date"));
  716. xs *acct = mastoapi_account(actor);
  717. mn = xs_dict_append(mn, "account", acct);
  718. if (objid != NULL) {
  719. xs *st = mastoapi_status(&snac1, entry);
  720. mn = xs_dict_append(mn, "status", st);
  721. }
  722. out = xs_list_append(out, mn);
  723. }
  724. *body = xs_json_dumps_pp(out, 4);
  725. *ctype = "application/json";
  726. status = 200;
  727. }
  728. else
  729. status = 401;
  730. }
  731. else
  732. if (strcmp(cmd, "/filters") == 0) {
  733. /* snac will never have filters */
  734. *body = xs_dup("[]");
  735. *ctype = "application/json";
  736. status = 200;
  737. }
  738. else
  739. if (strcmp(cmd, "/favourites") == 0) {
  740. /* snac will never support a list of favourites */
  741. *body = xs_dup("[]");
  742. *ctype = "application/json";
  743. status = 200;
  744. }
  745. else
  746. if (strcmp(cmd, "/bookmarks") == 0) {
  747. /* snac does not support bookmarks */
  748. *body = xs_dup("[]");
  749. *ctype = "application/json";
  750. status = 200;
  751. }
  752. else
  753. if (strcmp(cmd, "/lists") == 0) {
  754. /* snac does not support lists */
  755. *body = xs_dup("[]");
  756. *ctype = "application/json";
  757. status = 200;
  758. }
  759. else
  760. if (strcmp(cmd, "/scheduled_statuses") == 0) {
  761. /* snac does not scheduled notes */
  762. *body = xs_dup("[]");
  763. *ctype = "application/json";
  764. status = 200;
  765. }
  766. else
  767. if (strcmp(cmd, "/follow_requests") == 0) {
  768. /* snac does not support optional follow confirmations */
  769. *body = xs_dup("[]");
  770. *ctype = "application/json";
  771. status = 200;
  772. }
  773. else
  774. if (strcmp(cmd, "/announcements") == 0) {
  775. /* snac has no announcements (yet?) */
  776. *body = xs_dup("[]");
  777. *ctype = "application/json";
  778. status = 200;
  779. }
  780. else
  781. if (strcmp(cmd, "/custom_emojis") == 0) {
  782. /* are you kidding me? */
  783. *body = xs_dup("[]");
  784. *ctype = "application/json";
  785. status = 200;
  786. }
  787. else
  788. if (strcmp(cmd, "/instance") == 0) {
  789. /* returns an instance object */
  790. xs *ins = xs_dict_new();
  791. const char *host = xs_dict_get(srv_config, "host");
  792. ins = xs_dict_append(ins, "domain", host);
  793. ins = xs_dict_append(ins, "title", host);
  794. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  795. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/what-is-snac");
  796. ins = xs_dict_append(ins, "description", host);
  797. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  798. xs *d1 = xs_dict_new();
  799. d1 = xs_dict_append(d1, "url", susie);
  800. ins = xs_dict_append(ins, "thumbnail", d1);
  801. xs *d2 = xs_dict_new();
  802. d2 = xs_dict_append(d2, "email", "admin@localhost");
  803. ins = xs_dict_append(ins, "contact", d2);
  804. xs *l1 = xs_list_new();
  805. ins = xs_dict_append(ins, "rules", l1);
  806. ins = xs_dict_append(ins, "languages", l1);
  807. *body = xs_json_dumps_pp(ins, 4);
  808. *ctype = "application/json";
  809. status = 200;
  810. }
  811. else
  812. if (xs_startswith(cmd, "/statuses/")) {
  813. /* operations on a status */
  814. xs *l = xs_split(cmd, "/");
  815. const char *id = xs_list_get(l, 2);
  816. const char *op = xs_list_get(l, 3);
  817. if (!xs_is_null(id)) {
  818. xs *msg = NULL;
  819. xs *out = NULL;
  820. /* skip the 'fake' part of the id */
  821. id = MID_TO_MD5(id);
  822. if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
  823. if (op == NULL) {
  824. /* return the status itself */
  825. out = mastoapi_status(&snac1, msg);
  826. }
  827. else
  828. if (strcmp(op, "context") == 0) {
  829. /* return ancestors and children */
  830. xs *anc = xs_list_new();
  831. xs *des = xs_list_new();
  832. xs_list *p;
  833. xs_str *v;
  834. char pid[64];
  835. /* build the [grand]parent list, moving up */
  836. strcpy(pid, id);
  837. while (object_parent(pid, pid, sizeof(pid))) {
  838. xs *m2 = NULL;
  839. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  840. xs *st = mastoapi_status(&snac1, m2);
  841. anc = xs_list_append(anc, st);
  842. }
  843. else
  844. break;
  845. }
  846. /* build the children list */
  847. xs *children = object_children(xs_dict_get(msg, "id"));
  848. p = children;
  849. while (xs_list_iter(&p, &v)) {
  850. xs *m2 = NULL;
  851. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  852. xs *st = mastoapi_status(&snac1, m2);
  853. des = xs_list_append(des, st);
  854. }
  855. }
  856. out = xs_dict_new();
  857. out = xs_dict_append(out, "ancestors", anc);
  858. out = xs_dict_append(out, "descendants", des);
  859. }
  860. else
  861. if (strcmp(op, "reblogged_by") == 0 ||
  862. strcmp(op, "favourited_by") == 0) {
  863. /* return the list of people who liked or boosted this */
  864. out = xs_list_new();
  865. xs *l = NULL;
  866. if (op[0] == 'r')
  867. l = object_announces(xs_dict_get(msg, "id"));
  868. else
  869. l = object_likes(xs_dict_get(msg, "id"));
  870. xs_list *p = l;
  871. xs_str *v;
  872. while (xs_list_iter(&p, &v)) {
  873. xs *actor2 = NULL;
  874. if (valid_status(object_get_by_md5(v, &actor2))) {
  875. xs *acct2 = mastoapi_account(actor2);
  876. out = xs_list_append(out, acct2);
  877. }
  878. }
  879. }
  880. }
  881. else
  882. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  883. if (out != NULL) {
  884. *body = xs_json_dumps_pp(out, 4);
  885. *ctype = "application/json";
  886. status = 200;
  887. }
  888. }
  889. }
  890. else
  891. if (strcmp(cmd, "/filters") == 0) {
  892. *body = xs_dup("[]");
  893. *ctype = "application/json";
  894. status = 200;
  895. }
  896. /* user cleanup */
  897. if (logged_in)
  898. user_free(&snac1);
  899. return status;
  900. }
  901. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  902. const char *payload, int p_size,
  903. char **body, int *b_size, char **ctype)
  904. {
  905. if (!xs_startswith(q_path, "/api/v1/"))
  906. return 0;
  907. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  908. /* {
  909. xs *j = xs_json_dumps_pp(req, 4);
  910. printf("mastoapi post:\n%s\n", j);
  911. }*/
  912. int status = 404;
  913. xs *args = NULL;
  914. char *i_ctype = xs_dict_get(req, "content-type");
  915. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  916. args = xs_json_loads(payload);
  917. else
  918. args = xs_dup(xs_dict_get(req, "p_vars"));
  919. if (args == NULL)
  920. return 400;
  921. /* {
  922. xs *j = xs_json_dumps_pp(args, 4);
  923. printf("%s\n", j);
  924. }*/
  925. xs *cmd = xs_replace(q_path, "/api/v1", "");
  926. snac snac = {0};
  927. int logged_in = process_auth_token(&snac, req);
  928. if (strcmp(cmd, "/apps") == 0) {
  929. const char *name = xs_dict_get(args, "client_name");
  930. const char *ruri = xs_dict_get(args, "redirect_uris");
  931. const char *scope = xs_dict_get(args, "scope");
  932. if (xs_type(ruri) == XSTYPE_LIST)
  933. ruri = xs_dict_get(ruri, 0);
  934. if (name && ruri) {
  935. xs *app = xs_dict_new();
  936. xs *id = xs_replace_i(tid(0), ".", "");
  937. xs *cid = random_str();
  938. xs *csec = random_str();
  939. xs *vkey = random_str();
  940. app = xs_dict_append(app, "name", name);
  941. app = xs_dict_append(app, "redirect_uri", ruri);
  942. app = xs_dict_append(app, "client_id", cid);
  943. app = xs_dict_append(app, "client_secret", csec);
  944. app = xs_dict_append(app, "vapid_key", vkey);
  945. app = xs_dict_append(app, "id", id);
  946. *body = xs_json_dumps_pp(app, 4);
  947. *ctype = "application/json";
  948. status = 200;
  949. app = xs_dict_append(app, "code", "");
  950. if (scope)
  951. app = xs_dict_append(app, "scope", scope);
  952. app_add(cid, app);
  953. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  954. }
  955. }
  956. else
  957. if (strcmp(cmd, "/statuses") == 0) {
  958. if (logged_in) {
  959. /* post a new Note */
  960. /* TBD */
  961. }
  962. else
  963. status = 401;
  964. }
  965. else
  966. if (xs_startswith(cmd, "/statuses")) {
  967. if (logged_in) {
  968. /* operations on a status */
  969. xs *l = xs_split(cmd, "/");
  970. const char *mid = xs_list_get(l, 2);
  971. const char *op = xs_list_get(l, 3);
  972. if (!xs_is_null(mid)) {
  973. xs *msg = NULL;
  974. xs *out = NULL;
  975. /* skip the 'fake' part of the id */
  976. mid = MID_TO_MD5(mid);
  977. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  978. char *id = xs_dict_get(msg, "id");
  979. if (op == NULL) {
  980. /* no operation (?) */
  981. }
  982. else
  983. if (strcmp(op, "favourite") == 0) {
  984. xs *n_msg = msg_admiration(&snac, id, "Like");
  985. if (n_msg != NULL) {
  986. enqueue_message(&snac, n_msg);
  987. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  988. out = mastoapi_status(&snac, msg);
  989. }
  990. }
  991. else
  992. if (strcmp(op, "unfavourite") == 0) {
  993. /* snac does not support Undo+Like */
  994. }
  995. else
  996. if (strcmp(op, "reblog") == 0) {
  997. xs *n_msg = msg_admiration(&snac, id, "Announce");
  998. if (n_msg != NULL) {
  999. enqueue_message(&snac, n_msg);
  1000. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1001. out = mastoapi_status(&snac, msg);
  1002. }
  1003. }
  1004. else
  1005. if (strcmp(op, "unreblog") == 0) {
  1006. /* snac does not support Undo+Announce */
  1007. }
  1008. else
  1009. if (strcmp(op, "bookmark") == 0) {
  1010. /* snac does not support bookmarks */
  1011. }
  1012. else
  1013. if (strcmp(op, "unbookmark") == 0) {
  1014. /* snac does not support bookmarks */
  1015. }
  1016. else
  1017. if (strcmp(op, "pin") == 0) {
  1018. /* snac does not support pinning */
  1019. }
  1020. else
  1021. if (strcmp(op, "unpin") == 0) {
  1022. /* snac does not support pinning */
  1023. }
  1024. else
  1025. if (strcmp(op, "mute") == 0) {
  1026. /* Mastodon's mute is snac's hide */
  1027. }
  1028. else
  1029. if (strcmp(op, "unmute") == 0) {
  1030. /* Mastodon's unmute is snac's unhide */
  1031. }
  1032. }
  1033. if (out != NULL) {
  1034. *body = xs_json_dumps_pp(out, 4);
  1035. *ctype = "application/json";
  1036. status = 200;
  1037. }
  1038. }
  1039. }
  1040. else
  1041. status = 401;
  1042. }
  1043. /* user cleanup */
  1044. if (logged_in)
  1045. user_free(&snac);
  1046. return status;
  1047. }