mastoapi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. int oauth_get_handler(const xs_dict *req, const char *q_path,
  26. char **body, int *b_size, char **ctype)
  27. {
  28. if (!xs_startswith(q_path, "/oauth/"))
  29. return 0;
  30. {
  31. xs *j = xs_json_dumps_pp(req, 4);
  32. printf("oauth:\n%s\n", j);
  33. }
  34. int status = 404;
  35. xs_dict *msg = xs_dict_get(req, "q_vars");
  36. xs *cmd = xs_replace(q_path, "/oauth", "");
  37. if (strcmp(cmd, "/authorize") == 0) {
  38. const char *cid = xs_dict_get(msg, "client_id");
  39. const char *ruri = xs_dict_get(msg, "redirect_uri");
  40. const char *rtype = xs_dict_get(msg, "response_type");
  41. const char *scope = xs_dict_get(msg, "scope");
  42. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  43. /* redirect to an identification page */
  44. status = 303;
  45. // *body = xs_fmt("%s/test1/admin?redir=%s", srv_baseurl, ruri);
  46. *body = xs_fmt("%s/test1/admin", srv_baseurl);
  47. }
  48. else
  49. status = 400;
  50. }
  51. return status;
  52. }
  53. int oauth_post_handler(const xs_dict *req, const char *q_path,
  54. const char *payload, int p_size,
  55. char **body, int *b_size, char **ctype)
  56. {
  57. if (!xs_startswith(q_path, "/oauth/"))
  58. return 0;
  59. int status = 404;
  60. xs_dict *msg = xs_dict_get(req, "p_vars");
  61. xs *cmd = xs_replace(q_path, "/oauth", "");
  62. printf("oauth: %s\n", q_path);
  63. if (strcmp(cmd, "/token") == 0) {
  64. const char *gtype = xs_dict_get(msg, "grant_type");
  65. const char *code = xs_dict_get(msg, "code");
  66. const char *cid = xs_dict_get(msg, "client_id");
  67. const char *csec = xs_dict_get(msg, "client_secret");
  68. const char *ruri = xs_dict_get(msg, "redirect_uri");
  69. const char *scope = xs_dict_get(msg, "scope");
  70. if (gtype && code && cid && csec && ruri) {
  71. xs *rsp = xs_dict_new();
  72. xs *cat = xs_number_new(time(NULL));
  73. xs *token = random_str();
  74. rsp = xs_dict_append(rsp, "access_token", token);
  75. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  76. rsp = xs_dict_append(rsp, "scope", scope);
  77. rsp = xs_dict_append(rsp, "created_at", cat);
  78. *body = xs_json_dumps_pp(rsp, 4);
  79. *ctype = "application/json";
  80. status = 200;
  81. }
  82. else
  83. status = 400;
  84. }
  85. else
  86. if (strcmp(cmd, "/revoke") == 0) {
  87. const char *cid = xs_dict_get(msg, "client_id");
  88. const char *csec = xs_dict_get(msg, "client_secret");
  89. const char *token = xs_dict_get(msg, "token");
  90. if (cid && csec && token) {
  91. *body = xs_str_new("{}");
  92. *ctype = "application/json";
  93. status = 200;
  94. }
  95. else
  96. status = 400;
  97. }
  98. return status;
  99. }
  100. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  101. const char *payload, int p_size,
  102. char **body, int *b_size, char **ctype)
  103. {
  104. if (!xs_startswith(q_path, "/api/v1/"))
  105. return 0;
  106. int status = 404;
  107. xs *msg = NULL;
  108. char *i_ctype = xs_dict_get(req, "content-type");
  109. if (xs_startswith(i_ctype, "application/json"))
  110. msg = xs_json_loads(payload);
  111. else
  112. msg = xs_dup(xs_dict_get(req, "p_vars"));
  113. if (msg == NULL)
  114. return 400;
  115. {
  116. xs *j = xs_json_dumps_pp(req, 4);
  117. printf("%s\n", j);
  118. }
  119. {
  120. xs *j = xs_json_dumps_pp(msg, 4);
  121. printf("%s\n", j);
  122. }
  123. xs *cmd = xs_replace(q_path, "/api/v1", "");
  124. if (strcmp(cmd, "/apps") == 0) {
  125. const char *name = xs_dict_get(msg, "client_name");
  126. const char *ruri = xs_dict_get(msg, "redirect_uris");
  127. if (name && ruri) {
  128. xs *app = xs_dict_new();
  129. xs *id = xs_replace_i(tid(0), ".", "");
  130. xs *cid = random_str();
  131. xs *csec = random_str();
  132. xs *vkey = random_str();
  133. app = xs_dict_append(app, "name", name);
  134. app = xs_dict_append(app, "redirect_uri", ruri);
  135. app = xs_dict_append(app, "client_id", cid);
  136. app = xs_dict_append(app, "client_secret", csec);
  137. app = xs_dict_append(app, "vapid_key", vkey);
  138. app = xs_dict_append(app, "id", id);
  139. *body = xs_json_dumps_pp(app, 4);
  140. *ctype = "application/json";
  141. status = 200;
  142. }
  143. }
  144. return status;
  145. }