mastoapi.c 5.5 KB

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