mastoapi.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. acct = xs_dict_append(acct, "note", xs_dict_get(actor, "summary"));
  343. acct = xs_dict_append(acct, "url", id);
  344. xs *avatar = NULL;
  345. xs_dict *av = xs_dict_get(actor, "icon");
  346. if (xs_type(av) == XSTYPE_DICT)
  347. avatar = xs_dup(xs_dict_get(av, "url"));
  348. else
  349. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  350. acct = xs_dict_append(acct, "avatar", avatar);
  351. return acct;
  352. }
  353. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  354. /* converts an ActivityPub note to a Mastodon status */
  355. {
  356. xs *actor = NULL;
  357. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  358. /* if the author is not here, discard */
  359. if (actor == NULL)
  360. return NULL;
  361. xs *acct = mastoapi_account(actor);
  362. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  363. xs *f = xs_val_new(XSTYPE_FALSE);
  364. xs *t = xs_val_new(XSTYPE_TRUE);
  365. xs *n = xs_val_new(XSTYPE_NULL);
  366. xs *el = xs_list_new();
  367. xs *idx = NULL;
  368. xs *ixc = NULL;
  369. char *tmp;
  370. char *id = xs_dict_get(msg, "id");
  371. xs *mid = mastoapi_id(msg);
  372. xs_dict *st = xs_dict_new();
  373. st = xs_dict_append(st, "id", mid);
  374. st = xs_dict_append(st, "uri", id);
  375. st = xs_dict_append(st, "url", id);
  376. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  377. st = xs_dict_append(st, "account", acct);
  378. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  379. st = xs_dict_append(st, "visibility",
  380. is_msg_public(snac, msg) ? "public" : "private");
  381. tmp = xs_dict_get(msg, "sensitive");
  382. if (xs_is_null(tmp))
  383. tmp = f;
  384. st = xs_dict_append(st, "sensitive", tmp);
  385. tmp = xs_dict_get(msg, "summary");
  386. if (xs_is_null(tmp))
  387. tmp = "";
  388. st = xs_dict_append(st, "spoiler_text", tmp);
  389. /* create the list of attachments */
  390. xs *matt = xs_list_new();
  391. xs_list *att = xs_dict_get(msg, "attachment");
  392. xs_str *aobj;
  393. while (xs_list_iter(&att, &aobj)) {
  394. const char *mtype = xs_dict_get(aobj, "mediaType");
  395. if (!xs_is_null(mtype) && xs_startswith(mtype, "image/")) {
  396. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  397. xs *matte = xs_dict_new();
  398. matte = xs_dict_append(matte, "id", matteid);
  399. matte = xs_dict_append(matte, "type", "image");
  400. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  401. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  402. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  403. const char *name = xs_dict_get(aobj, "name");
  404. if (xs_is_null(name))
  405. name = "";
  406. matte = xs_dict_append(matte, "description", name);
  407. matt = xs_list_append(matt, matte);
  408. }
  409. }
  410. st = xs_dict_append(st, "media_attachments", matt);
  411. st = xs_dict_append(st, "mentions", el);
  412. st = xs_dict_append(st, "tags", el);
  413. st = xs_dict_append(st, "emojis", el);
  414. xs_free(idx);
  415. xs_free(ixc);
  416. idx = object_likes(id);
  417. ixc = xs_number_new(xs_list_len(idx));
  418. st = xs_dict_append(st, "favourites_count", ixc);
  419. st = xs_dict_append(st, "favourited",
  420. xs_list_in(idx, snac->md5) != -1 ? t : f);
  421. xs_free(idx);
  422. xs_free(ixc);
  423. idx = object_announces(id);
  424. ixc = xs_number_new(xs_list_len(idx));
  425. st = xs_dict_append(st, "reblogs_count", ixc);
  426. st = xs_dict_append(st, "reblogged",
  427. xs_list_in(idx, snac->md5) != -1 ? t : f);
  428. xs_free(idx);
  429. xs_free(ixc);
  430. idx = object_children(id);
  431. ixc = xs_number_new(xs_list_len(idx));
  432. st = xs_dict_append(st, "replies_count", ixc);
  433. /* default in_reply_to values */
  434. st = xs_dict_append(st, "in_reply_to_id", n);
  435. st = xs_dict_append(st, "in_reply_to_account_id", n);
  436. tmp = xs_dict_get(msg, "inReplyTo");
  437. if (!xs_is_null(tmp)) {
  438. xs *irto = NULL;
  439. if (valid_status(object_get(tmp, &irto))) {
  440. xs *irt_mid = mastoapi_id(irto);
  441. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  442. char *at = NULL;
  443. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  444. xs *at_md5 = xs_md5_hex(at, strlen(at));
  445. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  446. }
  447. }
  448. }
  449. st = xs_dict_append(st, "reblog", n);
  450. st = xs_dict_append(st, "poll", n);
  451. st = xs_dict_append(st, "card", n);
  452. st = xs_dict_append(st, "language", n);
  453. tmp = xs_dict_get(msg, "sourceContent");
  454. if (xs_is_null(tmp))
  455. tmp = "";
  456. st = xs_dict_append(st, "text", tmp);
  457. tmp = xs_dict_get(msg, "updated");
  458. if (xs_is_null(tmp))
  459. tmp = n;
  460. st = xs_dict_append(st, "edited_at", tmp);
  461. return st;
  462. }
  463. int process_auth_token(snac *snac, const xs_dict *req)
  464. /* processes an authorization token, if there is one */
  465. {
  466. int logged_in = 0;
  467. char *v;
  468. /* if there is an authorization field, try to validate it */
  469. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  470. xs *tokid = xs_replace(v, "Bearer ", "");
  471. xs *token = token_get(tokid);
  472. if (token != NULL) {
  473. const char *uid = xs_dict_get(token, "uid");
  474. if (!xs_is_null(uid) && user_open(snac, uid)) {
  475. logged_in = 1;
  476. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  477. }
  478. else
  479. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  480. }
  481. else
  482. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  483. }
  484. return logged_in;
  485. }
  486. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  487. char **body, int *b_size, char **ctype)
  488. {
  489. if (!xs_startswith(q_path, "/api/v1/"))
  490. return 0;
  491. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  492. /* {
  493. xs *j = xs_json_dumps_pp(req, 4);
  494. printf("mastoapi get:\n%s\n", j);
  495. }*/
  496. int status = 404;
  497. xs_dict *args = xs_dict_get(req, "q_vars");
  498. xs *cmd = xs_replace(q_path, "/api/v1", "");
  499. snac snac1 = {0};
  500. int logged_in = process_auth_token(&snac1, req);
  501. if (strcmp(cmd, "/accounts/verify_credentials") == 0) {
  502. if (logged_in) {
  503. xs *acct = xs_dict_new();
  504. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  505. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  506. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  507. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  508. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  509. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  510. acct = xs_dict_append(acct, "url", snac1.actor);
  511. xs *avatar = NULL;
  512. char *av = xs_dict_get(snac1.config, "avatar");
  513. if (xs_is_null(av) || *av == '\0')
  514. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  515. else
  516. avatar = xs_dup(av);
  517. acct = xs_dict_append(acct, "avatar", avatar);
  518. *body = xs_json_dumps_pp(acct, 4);
  519. *ctype = "application/json";
  520. status = 200;
  521. }
  522. else {
  523. status = 422; // "Unprocessable entity" (no login)
  524. }
  525. }
  526. else
  527. if (strcmp(cmd, "/accounts/relationships") == 0) {
  528. /* find if an account is followed, blocked, etc. */
  529. /* the account to get relationships about is in args "id[]" */
  530. /* dummy by now */
  531. if (logged_in) {
  532. *body = xs_dup("[]");
  533. *ctype = "application/json";
  534. status = 200;
  535. }
  536. }
  537. else
  538. if (xs_startswith(cmd, "/accounts/")) {
  539. /* account-related information */
  540. xs *l = xs_split(cmd, "/");
  541. const char *uid = xs_list_get(l, 2);
  542. const char *opt = xs_list_get(l, 3);
  543. if (uid != NULL) {
  544. snac snac2;
  545. xs *out = NULL;
  546. xs *actor = NULL;
  547. /* is it a local user? */
  548. if (user_open(&snac2, uid)) {
  549. if (opt == NULL) {
  550. /* account information */
  551. actor = msg_actor(&snac2);
  552. out = mastoapi_account(actor);
  553. }
  554. else
  555. if (strcmp(opt, "statuses") == 0) {
  556. /* the public list of posts of a user */
  557. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  558. xs_list *p = timeline;
  559. xs_str *v;
  560. out = xs_list_new();
  561. while (xs_list_iter(&p, &v)) {
  562. xs *msg = NULL;
  563. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  564. /* add only posts by the author */
  565. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  566. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  567. xs *st = mastoapi_status(&snac2, msg);
  568. out = xs_list_append(out, st);
  569. }
  570. }
  571. }
  572. }
  573. user_free(&snac2);
  574. }
  575. else {
  576. /* try the uid as the md5 of a possibly loaded actor */
  577. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  578. if (opt == NULL) {
  579. /* account information */
  580. out = mastoapi_account(actor);
  581. }
  582. else
  583. if (strcmp(opt, "statuses") == 0) {
  584. /* we don't serve statuses of others; return the empty list */
  585. out = xs_list_new();
  586. }
  587. }
  588. }
  589. if (out != NULL) {
  590. *body = xs_json_dumps_pp(out, 4);
  591. *ctype = "application/json";
  592. status = 200;
  593. }
  594. }
  595. }
  596. else
  597. if (strcmp(cmd, "/timelines/home") == 0) {
  598. /* the private timeline */
  599. if (logged_in) {
  600. const char *max_id = xs_dict_get(args, "max_id");
  601. const char *since_id = xs_dict_get(args, "since_id");
  602. const char *min_id = xs_dict_get(args, "min_id");
  603. const char *limit_s = xs_dict_get(args, "limit");
  604. int limit = 0;
  605. int cnt = 0;
  606. if (!xs_is_null(limit_s))
  607. limit = atoi(limit_s);
  608. if (limit == 0)
  609. limit = 20;
  610. xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
  611. xs *out = xs_list_new();
  612. xs_list *p = timeline;
  613. xs_str *v;
  614. while (xs_list_iter(&p, &v) && cnt < limit) {
  615. xs *msg = NULL;
  616. /* only return entries older that max_id */
  617. if (max_id) {
  618. if (strcmp(v, max_id) == 0)
  619. max_id = NULL;
  620. continue;
  621. }
  622. /* only returns entries newer than since_id */
  623. if (since_id) {
  624. if (strcmp(v, since_id) == 0)
  625. break;
  626. }
  627. /* only returns entries newer than min_id */
  628. /* what does really "Return results immediately newer than ID" mean? */
  629. if (min_id) {
  630. if (strcmp(v, min_id) == 0)
  631. break;
  632. }
  633. /* get the entry */
  634. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  635. continue;
  636. /* discard non-Notes */
  637. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  638. continue;
  639. /* convert the Note into a Mastodon status */
  640. xs *st = mastoapi_status(&snac1, msg);
  641. if (st != NULL)
  642. out = xs_list_append(out, st);
  643. cnt++;
  644. }
  645. *body = xs_json_dumps_pp(out, 4);
  646. *ctype = "application/json";
  647. status = 200;
  648. {
  649. xs *j = xs_json_loads(*body);
  650. if (j == NULL) {
  651. srv_log(xs_fmt("mastoapi timeline: bad JSON"));
  652. srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
  653. }
  654. }
  655. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  656. }
  657. else {
  658. status = 401; // unauthorized
  659. }
  660. }
  661. else
  662. if (strcmp(cmd, "/timelines/public") == 0) {
  663. /* the public timeline (public timelines for all users) */
  664. /* TBD */
  665. *body = xs_dup("[]");
  666. *ctype = "application/json";
  667. status = 200;
  668. }
  669. else
  670. if (strcmp(cmd, "/conversations") == 0) {
  671. /* TBD */
  672. *body = xs_dup("[]");
  673. *ctype = "application/json";
  674. status = 200;
  675. }
  676. else
  677. if (strcmp(cmd, "/notifications") == 0) {
  678. if (logged_in) {
  679. xs *l = notify_list(&snac1, 0);
  680. xs *out = xs_list_new();
  681. xs_list *p = l;
  682. xs_dict *v;
  683. while (xs_list_iter(&p, &v)) {
  684. const char *type = xs_dict_get(v, "type");
  685. const char *objid = xs_dict_get(v, "objid");
  686. xs *actor = NULL;
  687. xs *entry = NULL;
  688. if (!valid_status(object_get(xs_dict_get(v, "actor"), &actor)))
  689. continue;
  690. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  691. continue;
  692. /* convert the type */
  693. if (strcmp(type, "Like") == 0)
  694. type = "favourite";
  695. else
  696. if (strcmp(type, "Announce") == 0)
  697. type = "reblog";
  698. else
  699. if (strcmp(type, "Follow") == 0)
  700. type = "follow";
  701. else
  702. if (strcmp(type, "Create") == 0)
  703. type = "mention";
  704. else
  705. continue;
  706. xs *mn = xs_dict_new();
  707. mn = xs_dict_append(mn, "type", type);
  708. xs *id = xs_replace(xs_dict_get(v, "id"), ".", "");
  709. mn = xs_dict_append(mn, "id", id);
  710. mn = xs_dict_append(mn, "created_at", xs_dict_get(v, "date"));
  711. xs *acct = mastoapi_account(actor);
  712. mn = xs_dict_append(mn, "account", acct);
  713. if (objid != NULL) {
  714. xs *st = mastoapi_status(&snac1, entry);
  715. mn = xs_dict_append(mn, "status", st);
  716. }
  717. out = xs_list_append(out, mn);
  718. }
  719. *body = xs_json_dumps_pp(out, 4);
  720. *ctype = "application/json";
  721. status = 200;
  722. }
  723. else
  724. status = 401;
  725. }
  726. else
  727. if (strcmp(cmd, "/filters") == 0) {
  728. /* snac will never have filters */
  729. *body = xs_dup("[]");
  730. *ctype = "application/json";
  731. status = 200;
  732. }
  733. else
  734. if (strcmp(cmd, "/favourites") == 0) {
  735. /* snac will never support a list of favourites */
  736. *body = xs_dup("[]");
  737. *ctype = "application/json";
  738. status = 200;
  739. }
  740. else
  741. if (strcmp(cmd, "/bookmarks") == 0) {
  742. /* snac does not support bookmarks */
  743. *body = xs_dup("[]");
  744. *ctype = "application/json";
  745. status = 200;
  746. }
  747. else
  748. if (strcmp(cmd, "/lists") == 0) {
  749. /* snac does not support lists */
  750. *body = xs_dup("[]");
  751. *ctype = "application/json";
  752. status = 200;
  753. }
  754. else
  755. if (strcmp(cmd, "/scheduled_statuses") == 0) {
  756. /* snac does not scheduled notes */
  757. *body = xs_dup("[]");
  758. *ctype = "application/json";
  759. status = 200;
  760. }
  761. else
  762. if (strcmp(cmd, "/follow_requests") == 0) {
  763. /* snac does not support optional follow confirmations */
  764. *body = xs_dup("[]");
  765. *ctype = "application/json";
  766. status = 200;
  767. }
  768. else
  769. if (strcmp(cmd, "/announcements") == 0) {
  770. /* snac has no announcements (yet?) */
  771. *body = xs_dup("[]");
  772. *ctype = "application/json";
  773. status = 200;
  774. }
  775. else
  776. if (strcmp(cmd, "/custom_emojis") == 0) {
  777. /* are you kidding me? */
  778. *body = xs_dup("[]");
  779. *ctype = "application/json";
  780. status = 200;
  781. }
  782. else
  783. if (strcmp(cmd, "/instance") == 0) {
  784. /* returns an instance object */
  785. xs *ins = xs_dict_new();
  786. const char *host = xs_dict_get(srv_config, "host");
  787. ins = xs_dict_append(ins, "domain", host);
  788. ins = xs_dict_append(ins, "title", host);
  789. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  790. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/what-is-snac");
  791. ins = xs_dict_append(ins, "description", host);
  792. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  793. xs *d1 = xs_dict_new();
  794. d1 = xs_dict_append(d1, "url", susie);
  795. ins = xs_dict_append(ins, "thumbnail", d1);
  796. xs *d2 = xs_dict_new();
  797. d2 = xs_dict_append(d2, "email", "admin@localhost");
  798. ins = xs_dict_append(ins, "contact", d2);
  799. xs *l1 = xs_list_new();
  800. ins = xs_dict_append(ins, "rules", l1);
  801. ins = xs_dict_append(ins, "languages", l1);
  802. *body = xs_json_dumps_pp(ins, 4);
  803. *ctype = "application/json";
  804. status = 200;
  805. }
  806. else
  807. if (xs_startswith(cmd, "/statuses/")) {
  808. /* operations on a status */
  809. xs *l = xs_split(cmd, "/");
  810. const char *id = xs_list_get(l, 2);
  811. const char *op = xs_list_get(l, 3);
  812. if (!xs_is_null(id)) {
  813. xs *msg = NULL;
  814. xs *out = NULL;
  815. /* skip the 'fake' part of the id */
  816. id = MID_TO_MD5(id);
  817. if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
  818. if (op == NULL) {
  819. /* return the status itself */
  820. out = mastoapi_status(&snac1, msg);
  821. }
  822. else
  823. if (strcmp(op, "context") == 0) {
  824. /* return ancestors and children */
  825. xs *anc = xs_list_new();
  826. xs *des = xs_list_new();
  827. xs_list *p;
  828. xs_str *v;
  829. char pid[64];
  830. /* build the [grand]parent list, moving up */
  831. strcpy(pid, id);
  832. while (object_parent(pid, pid, sizeof(pid))) {
  833. xs *m2 = NULL;
  834. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  835. xs *st = mastoapi_status(&snac1, m2);
  836. anc = xs_list_append(anc, st);
  837. }
  838. else
  839. break;
  840. }
  841. /* build the children list */
  842. xs *children = object_children(xs_dict_get(msg, "id"));
  843. p = children;
  844. while (xs_list_iter(&p, &v)) {
  845. xs *m2 = NULL;
  846. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  847. xs *st = mastoapi_status(&snac1, m2);
  848. des = xs_list_append(des, st);
  849. }
  850. }
  851. out = xs_dict_new();
  852. out = xs_dict_append(out, "ancestors", anc);
  853. out = xs_dict_append(out, "descendants", des);
  854. }
  855. else
  856. if (strcmp(op, "reblogged_by") == 0 ||
  857. strcmp(op, "favourited_by") == 0) {
  858. /* return the list of people who liked or boosted this */
  859. out = xs_list_new();
  860. xs *l = NULL;
  861. if (op[0] == 'r')
  862. l = object_announces(xs_dict_get(msg, "id"));
  863. else
  864. l = object_likes(xs_dict_get(msg, "id"));
  865. xs_list *p = l;
  866. xs_str *v;
  867. while (xs_list_iter(&p, &v)) {
  868. xs *actor2 = NULL;
  869. if (valid_status(object_get_by_md5(v, &actor2))) {
  870. xs *acct2 = mastoapi_account(actor2);
  871. out = xs_list_append(out, acct2);
  872. }
  873. }
  874. }
  875. }
  876. else
  877. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  878. if (out != NULL) {
  879. *body = xs_json_dumps_pp(out, 4);
  880. *ctype = "application/json";
  881. status = 200;
  882. }
  883. }
  884. }
  885. else
  886. if (strcmp(cmd, "/filters") == 0) {
  887. *body = xs_dup("[]");
  888. *ctype = "application/json";
  889. status = 200;
  890. }
  891. /* user cleanup */
  892. if (logged_in)
  893. user_free(&snac1);
  894. return status;
  895. }
  896. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  897. const char *payload, int p_size,
  898. char **body, int *b_size, char **ctype)
  899. {
  900. if (!xs_startswith(q_path, "/api/v1/"))
  901. return 0;
  902. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  903. /* {
  904. xs *j = xs_json_dumps_pp(req, 4);
  905. printf("mastoapi post:\n%s\n", j);
  906. }*/
  907. int status = 404;
  908. xs *args = NULL;
  909. char *i_ctype = xs_dict_get(req, "content-type");
  910. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  911. args = xs_json_loads(payload);
  912. else
  913. args = xs_dup(xs_dict_get(req, "p_vars"));
  914. if (args == NULL)
  915. return 400;
  916. /* {
  917. xs *j = xs_json_dumps_pp(args, 4);
  918. printf("%s\n", j);
  919. }*/
  920. xs *cmd = xs_replace(q_path, "/api/v1", "");
  921. snac snac = {0};
  922. int logged_in = process_auth_token(&snac, req);
  923. if (strcmp(cmd, "/apps") == 0) {
  924. const char *name = xs_dict_get(args, "client_name");
  925. const char *ruri = xs_dict_get(args, "redirect_uris");
  926. const char *scope = xs_dict_get(args, "scope");
  927. if (xs_type(ruri) == XSTYPE_LIST)
  928. ruri = xs_dict_get(ruri, 0);
  929. if (name && ruri) {
  930. xs *app = xs_dict_new();
  931. xs *id = xs_replace_i(tid(0), ".", "");
  932. xs *cid = random_str();
  933. xs *csec = random_str();
  934. xs *vkey = random_str();
  935. app = xs_dict_append(app, "name", name);
  936. app = xs_dict_append(app, "redirect_uri", ruri);
  937. app = xs_dict_append(app, "client_id", cid);
  938. app = xs_dict_append(app, "client_secret", csec);
  939. app = xs_dict_append(app, "vapid_key", vkey);
  940. app = xs_dict_append(app, "id", id);
  941. *body = xs_json_dumps_pp(app, 4);
  942. *ctype = "application/json";
  943. status = 200;
  944. app = xs_dict_append(app, "code", "");
  945. if (scope)
  946. app = xs_dict_append(app, "scope", scope);
  947. app_add(cid, app);
  948. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  949. }
  950. }
  951. else
  952. if (strcmp(cmd, "/statuses") == 0) {
  953. if (logged_in) {
  954. /* post a new Note */
  955. /* TBD */
  956. }
  957. else
  958. status = 401;
  959. }
  960. else
  961. if (xs_startswith(cmd, "/statuses")) {
  962. if (logged_in) {
  963. /* operations on a status */
  964. xs *l = xs_split(cmd, "/");
  965. const char *mid = xs_list_get(l, 2);
  966. const char *op = xs_list_get(l, 3);
  967. if (!xs_is_null(mid)) {
  968. xs *msg = NULL;
  969. xs *out = NULL;
  970. /* skip the 'fake' part of the id */
  971. mid = MID_TO_MD5(mid);
  972. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  973. char *id = xs_dict_get(msg, "id");
  974. if (op == NULL) {
  975. /* no operation (?) */
  976. }
  977. else
  978. if (strcmp(op, "favourite") == 0) {
  979. xs *n_msg = msg_admiration(&snac, id, "Like");
  980. if (n_msg != NULL) {
  981. enqueue_message(&snac, n_msg);
  982. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  983. out = mastoapi_status(&snac, msg);
  984. }
  985. }
  986. else
  987. if (strcmp(op, "unfavourite") == 0) {
  988. /* snac does not support Undo+Like */
  989. }
  990. else
  991. if (strcmp(op, "reblog") == 0) {
  992. xs *n_msg = msg_admiration(&snac, id, "Announce");
  993. if (n_msg != NULL) {
  994. enqueue_message(&snac, n_msg);
  995. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  996. out = mastoapi_status(&snac, msg);
  997. }
  998. }
  999. else
  1000. if (strcmp(op, "unreblog") == 0) {
  1001. /* snac does not support Undo+Announce */
  1002. }
  1003. else
  1004. if (strcmp(op, "bookmark") == 0) {
  1005. /* snac does not support bookmarks */
  1006. }
  1007. else
  1008. if (strcmp(op, "unbookmark") == 0) {
  1009. /* snac does not support bookmarks */
  1010. }
  1011. else
  1012. if (strcmp(op, "pin") == 0) {
  1013. /* snac does not support pinning */
  1014. }
  1015. else
  1016. if (strcmp(op, "unpin") == 0) {
  1017. /* snac does not support pinning */
  1018. }
  1019. else
  1020. if (strcmp(op, "mute") == 0) {
  1021. /* Mastodon's mute is snac's hide */
  1022. }
  1023. else
  1024. if (strcmp(op, "unmute") == 0) {
  1025. /* Mastodon's unmute is snac's unhide */
  1026. }
  1027. }
  1028. if (out != NULL) {
  1029. *body = xs_json_dumps_pp(out, 4);
  1030. *ctype = "application/json";
  1031. status = 200;
  1032. }
  1033. }
  1034. }
  1035. else
  1036. status = 401;
  1037. }
  1038. /* user cleanup */
  1039. if (logged_in)
  1040. user_free(&snac);
  1041. return status;
  1042. }