mastoapi.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. int oauth_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
  9. char **body, int *b_size, char **ctype)
  10. {
  11. if (!xs_startswith(q_path, "/oauth/"))
  12. return 0;
  13. int status = 404;
  14. xs_dict *msg = xs_dict_get(req, "p_vars");
  15. xs *cmd = xs_replace(q_path, "/oauth", "");
  16. if (strcmp(cmd, "/authorize") == 0) {
  17. const char *cid = xs_dict_get(msg, "client_id");
  18. const char *ruri = xs_dict_get(msg, "redirect_uri");
  19. const char *rtype = xs_dict_get(msg, "response_type");
  20. const char *scope = xs_dict_get(msg, "scope");
  21. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  22. }
  23. else
  24. status = 400;
  25. }
  26. else
  27. if (strcmp(cmd, "/token") == 0) {
  28. const char *gtype = xs_dict_get(msg, "grant_type");
  29. const char *code = xs_dict_get(msg, "code");
  30. const char *cid = xs_dict_get(msg, "client_id");
  31. const char *csec = xs_dict_get(msg, "client_secret");
  32. const char *ruri = xs_dict_get(msg, "redirect_uri");
  33. const char *scope = xs_dict_get(msg, "scope");
  34. if (gtype && code && cid && csec && ruri) {
  35. xs *rsp = xs_dict_new();
  36. xs *cat = xs_number_new(time(NULL));
  37. rsp = xs_dict_append(rsp, "access_token", "abcde");
  38. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  39. rsp = xs_dict_append(rsp, "scope", scope);
  40. rsp = xs_dict_append(rsp, "created_at", cat);
  41. *body = xs_json_dumps_pp(rsp, 4);
  42. *ctype = "application/json";
  43. status = 200;
  44. }
  45. else
  46. status = 400;
  47. }
  48. else
  49. if (strcmp(cmd, "/revoke") == 0) {
  50. }
  51. return status;
  52. }
  53. int mastoapi_post_handler(xs_dict *req, char *q_path, char *payload, int p_size,
  54. char **body, int *b_size, char **ctype)
  55. {
  56. if (!xs_startswith(q_path, "/api/v1/"))
  57. return 0;
  58. int status = 404;
  59. xs *msg = NULL;
  60. char *i_ctype = xs_dict_get(req, "content-type");
  61. if (xs_startswith(i_ctype, "application/json"))
  62. msg = xs_json_loads(payload);
  63. else
  64. msg = xs_dup(xs_dict_get(req, "p_vars"));
  65. if (msg == NULL)
  66. return 400;
  67. {
  68. xs *j = xs_json_dumps_pp(req, 4);
  69. printf("%s\n", j);
  70. }
  71. {
  72. xs *j = xs_json_dumps_pp(msg, 4);
  73. printf("%s\n", j);
  74. }
  75. xs *cmd = xs_replace(q_path, "/api/v1", "");
  76. if (strcmp(cmd, "/apps") == 0) {
  77. const char *name = xs_dict_get(msg, "client_name");
  78. const char *ruri = xs_dict_get(msg, "redirect_uris");
  79. if (name && ruri) {
  80. xs *app = xs_dict_new();
  81. xs *id = xs_replace_i(tid(0), ".", "");
  82. app = xs_dict_append(app, "name", name);
  83. app = xs_dict_append(app, "redirect_uri", ruri);
  84. app = xs_dict_append(app, "client_id", "abcde");
  85. app = xs_dict_append(app, "client_secret", "abcde");
  86. app = xs_dict_append(app, "vapid_key", "abcde");
  87. app = xs_dict_append(app, "id", id);
  88. *body = xs_json_dumps_pp(app, 4);
  89. *ctype = "application/json";
  90. status = 200;
  91. }
  92. }
  93. return status;
  94. }