mastoapi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_encdec.h"
  5. #include "xs_json.h"
  6. #include "xs_io.h"
  7. #include "xs_time.h"
  8. #include "snac.h"
  9. static xs_str *random_str(void)
  10. /* just what is says in the tin */
  11. {
  12. unsigned int data[4] = {0};
  13. FILE *f;
  14. if ((f = fopen("/dev/random", "r")) != NULL) {
  15. fread(data, sizeof(data), 1, f);
  16. fclose(f);
  17. }
  18. else {
  19. data[0] = random() % 0xffffffff;
  20. data[1] = random() % 0xffffffff;
  21. data[2] = random() % 0xffffffff;
  22. data[3] = random() % 0xffffffff;
  23. }
  24. return xs_hex_enc((char *)data, sizeof(data));
  25. }
  26. int app_add(const char *id, const xs_dict *app)
  27. /* stores an app */
  28. {
  29. int status = 201;
  30. xs *fn = xs_fmt("%s/app/", srv_basedir);
  31. FILE *f;
  32. mkdirx(fn);
  33. fn = xs_str_cat(fn, id);
  34. fn = xs_str_cat(fn, ".json");
  35. if ((f = fopen(fn, "w")) != NULL) {
  36. xs *j = xs_json_dumps_pp(app, 4);
  37. fwrite(j, strlen(j), 1, f);
  38. fclose(f);
  39. }
  40. else
  41. status = 500;
  42. return status;
  43. }
  44. xs_dict *app_get(const char *id)
  45. /* gets an app */
  46. {
  47. xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
  48. xs_dict *app = NULL;
  49. FILE *f;
  50. if ((f = fopen(fn, "r")) != NULL) {
  51. xs *j = xs_readall(f);
  52. fclose(f);
  53. app = xs_json_loads(j);
  54. }
  55. return app;
  56. }
  57. const char *login_page = ""
  58. "<!DOCTYPE html>\n"
  59. "<body><h1>%s identify</h1>\n"
  60. "<form method=\"post\" action=\"https:/" "/%s/oauth/x-snac-login\">\n"
  61. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  62. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  63. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  64. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  65. "</form><p>%s</p></body>\n"
  66. "";
  67. int oauth_get_handler(const xs_dict *req, const char *q_path,
  68. char **body, int *b_size, char **ctype)
  69. {
  70. if (!xs_startswith(q_path, "/oauth/"))
  71. return 0;
  72. {
  73. xs *j = xs_json_dumps_pp(req, 4);
  74. printf("oauth:\n%s\n", j);
  75. }
  76. int status = 404;
  77. xs_dict *msg = xs_dict_get(req, "q_vars");
  78. xs *cmd = xs_replace(q_path, "/oauth", "");
  79. if (strcmp(cmd, "/authorize") == 0) {
  80. const char *cid = xs_dict_get(msg, "client_id");
  81. const char *ruri = xs_dict_get(msg, "redirect_uri");
  82. const char *rtype = xs_dict_get(msg, "response_type");
  83. status = 400;
  84. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  85. xs *app = app_get(cid);
  86. if (app != NULL) {
  87. const char *host = xs_dict_get(srv_config, "host");
  88. *body = xs_fmt(login_page, host, host, ruri, cid, USER_AGENT);
  89. *ctype = "text/html";
  90. status = 200;
  91. }
  92. }
  93. }
  94. return status;
  95. }
  96. int oauth_post_handler(const xs_dict *req, const char *q_path,
  97. const char *payload, int p_size,
  98. char **body, int *b_size, char **ctype)
  99. {
  100. if (!xs_startswith(q_path, "/oauth/"))
  101. return 0;
  102. int status = 404;
  103. xs_dict *msg = xs_dict_get(req, "p_vars");
  104. xs *cmd = xs_replace(q_path, "/oauth", "");
  105. printf("oauth: %s\n", q_path);
  106. if (strcmp(cmd, "/token") == 0) {
  107. const char *gtype = xs_dict_get(msg, "grant_type");
  108. const char *code = xs_dict_get(msg, "code");
  109. const char *cid = xs_dict_get(msg, "client_id");
  110. const char *csec = xs_dict_get(msg, "client_secret");
  111. const char *ruri = xs_dict_get(msg, "redirect_uri");
  112. const char *scope = xs_dict_get(msg, "scope");
  113. if (gtype && code && cid && csec && ruri) {
  114. xs *rsp = xs_dict_new();
  115. xs *cat = xs_number_new(time(NULL));
  116. xs *token = random_str();
  117. rsp = xs_dict_append(rsp, "access_token", token);
  118. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  119. rsp = xs_dict_append(rsp, "scope", scope);
  120. rsp = xs_dict_append(rsp, "created_at", cat);
  121. *body = xs_json_dumps_pp(rsp, 4);
  122. *ctype = "application/json";
  123. status = 200;
  124. }
  125. else
  126. status = 400;
  127. }
  128. else
  129. if (strcmp(cmd, "/revoke") == 0) {
  130. const char *cid = xs_dict_get(msg, "client_id");
  131. const char *csec = xs_dict_get(msg, "client_secret");
  132. const char *token = xs_dict_get(msg, "token");
  133. if (cid && csec && token) {
  134. *body = xs_str_new("{}");
  135. *ctype = "application/json";
  136. status = 200;
  137. }
  138. else
  139. status = 400;
  140. }
  141. return status;
  142. }
  143. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  144. const char *payload, int p_size,
  145. char **body, int *b_size, char **ctype)
  146. {
  147. if (!xs_startswith(q_path, "/api/v1/"))
  148. return 0;
  149. int status = 404;
  150. xs *msg = NULL;
  151. char *i_ctype = xs_dict_get(req, "content-type");
  152. if (xs_startswith(i_ctype, "application/json"))
  153. msg = xs_json_loads(payload);
  154. else
  155. msg = xs_dup(xs_dict_get(req, "p_vars"));
  156. if (msg == NULL)
  157. return 400;
  158. {
  159. xs *j = xs_json_dumps_pp(req, 4);
  160. printf("%s\n", j);
  161. }
  162. {
  163. xs *j = xs_json_dumps_pp(msg, 4);
  164. printf("%s\n", j);
  165. }
  166. xs *cmd = xs_replace(q_path, "/api/v1", "");
  167. if (strcmp(cmd, "/apps") == 0) {
  168. const char *name = xs_dict_get(msg, "client_name");
  169. const char *ruri = xs_dict_get(msg, "redirect_uris");
  170. if (name && ruri) {
  171. xs *app = xs_dict_new();
  172. xs *id = xs_replace_i(tid(0), ".", "");
  173. xs *cid = random_str();
  174. xs *csec = random_str();
  175. xs *vkey = random_str();
  176. app = xs_dict_append(app, "name", name);
  177. app = xs_dict_append(app, "redirect_uri", ruri);
  178. app = xs_dict_append(app, "client_id", cid);
  179. app = xs_dict_append(app, "client_secret", csec);
  180. app = xs_dict_append(app, "vapid_key", vkey);
  181. app = xs_dict_append(app, "id", id);
  182. *body = xs_json_dumps_pp(app, 4);
  183. *ctype = "application/json";
  184. status = 200;
  185. app = xs_dict_append(app, "code", "");
  186. app_add(cid, app);
  187. }
  188. }
  189. return status;
  190. }