diff options
| author | 2022-09-22 11:28:13 +0200 | |
|---|---|---|
| committer | 2022-09-22 11:28:13 +0200 | |
| commit | f2e4de7f9046533795b54a5930b4186506f538e0 (patch) | |
| tree | cfaf4f35dc63e54b6018982e64909b26489f67b8 | |
| parent | New function webfinger_request() (incomplete). (diff) | |
| download | penes-snac2-f2e4de7f9046533795b54a5930b4186506f538e0.tar.gz penes-snac2-f2e4de7f9046533795b54a5930b4186506f538e0.tar.xz penes-snac2-f2e4de7f9046533795b54a5930b4186506f538e0.zip | |
More webfinger work.
| -rw-r--r-- | main.c | 22 | ||||
| -rw-r--r-- | webfinger.c | 64 |
2 files changed, 77 insertions, 9 deletions
| @@ -18,6 +18,7 @@ int main(int argc, char *argv[]) | |||
| 18 | { | 18 | { |
| 19 | char *cmd; | 19 | char *cmd; |
| 20 | char *basedir; | 20 | char *basedir; |
| 21 | char *user; | ||
| 21 | int argi = 1; | 22 | int argi = 1; |
| 22 | 23 | ||
| 23 | argc--; | 24 | argc--; |
| @@ -45,6 +46,27 @@ int main(int argc, char *argv[]) | |||
| 45 | return 0; | 46 | return 0; |
| 46 | } | 47 | } |
| 47 | 48 | ||
| 49 | if (argc < argi) | ||
| 50 | return usage(); | ||
| 51 | |||
| 52 | user = argv[argi++]; | ||
| 53 | |||
| 54 | if (strcmp(cmd, "webfinger") == 0) { | ||
| 55 | xs *actor = NULL; | ||
| 56 | xs *uid = NULL; | ||
| 57 | int status; | ||
| 58 | |||
| 59 | webfinger_request(user, &status, &actor, &uid); | ||
| 60 | |||
| 61 | printf("status: %d\n", status); | ||
| 62 | if (actor != NULL) | ||
| 63 | printf("actor: %s\n", actor); | ||
| 64 | if (uid != NULL) | ||
| 65 | printf("uid: %s\n", uid); | ||
| 66 | |||
| 67 | return 0; | ||
| 68 | } | ||
| 69 | |||
| 48 | return 0; | 70 | return 0; |
| 49 | } | 71 | } |
| 50 | 72 | ||
diff --git a/webfinger.c b/webfinger.c index bcc5982..aeaf754 100644 --- a/webfinger.c +++ b/webfinger.c | |||
| @@ -13,21 +13,23 @@ void webfinger_request(char *qs, int *status, char **actor, char **user) | |||
| 13 | { | 13 | { |
| 14 | xs *payload = NULL; | 14 | xs *payload = NULL; |
| 15 | int p_size = 0; | 15 | int p_size = 0; |
| 16 | xs *url = NULL; | ||
| 17 | xs *headers = xs_dict_new(); | 16 | xs *headers = xs_dict_new(); |
| 17 | xs *l = NULL; | ||
| 18 | d_char *host = NULL; | ||
| 19 | xs *resource = NULL; | ||
| 18 | 20 | ||
| 19 | if (xs_startswith(qs, "https:/" "/")) { | 21 | if (xs_startswith(qs, "https:/" "/")) { |
| 20 | /* actor query: pick the host */ | 22 | /* actor query: pick the host */ |
| 21 | xs *s = xs_replace(qs, "https:/" "/", ""); | 23 | xs *s = xs_replace(qs, "https:/" "/", ""); |
| 22 | xs *l = xs_split_n(s, "/", 1); | ||
| 23 | 24 | ||
| 24 | url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource=%s", | 25 | l = xs_split_n(s, "/", 1); |
| 25 | xs_list_get(l, 0), qs); | 26 | |
| 27 | host = xs_list_get(l, 0); | ||
| 28 | resource = xs_dup(qs); | ||
| 26 | } | 29 | } |
| 27 | else { | 30 | else { |
| 28 | /* it's a user */ | 31 | /* it's a user */ |
| 29 | xs *s = xs_dup(qs); | 32 | xs *s = xs_dup(qs); |
| 30 | xs *l; | ||
| 31 | 33 | ||
| 32 | if (xs_startswith(s, "@")) | 34 | if (xs_startswith(s, "@")) |
| 33 | s = xs_crop(s, 1, 0); | 35 | s = xs_crop(s, 1, 0); |
| @@ -35,19 +37,61 @@ void webfinger_request(char *qs, int *status, char **actor, char **user) | |||
| 35 | l = xs_split_n(s, "@", 1); | 37 | l = xs_split_n(s, "@", 1); |
| 36 | 38 | ||
| 37 | if (xs_list_len(l) == 2) { | 39 | if (xs_list_len(l) == 2) { |
| 38 | url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource:acct:%s", | 40 | host = xs_list_get(l, 1); |
| 39 | xs_list_get(l, 1), qs); | 41 | resource = xs_fmt("acct:%s", qs); |
| 40 | } | 42 | } |
| 41 | } | 43 | } |
| 42 | 44 | ||
| 43 | if (url == NULL) { | 45 | if (host == NULL || resource == NULL) { |
| 44 | *status = 400; | 46 | *status = 400; |
| 45 | return; | 47 | return; |
| 46 | } | 48 | } |
| 47 | 49 | ||
| 48 | headers = xs_dict_append(headers, "accept", "application/json"); | 50 | headers = xs_dict_append(headers, "accept", "application/json"); |
| 49 | 51 | ||
| 50 | xs_http_request("GET", url, headers, NULL, 0, status, &payload, &p_size); | 52 | /* is it a query about one of us? */ |
| 53 | if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) { | ||
| 54 | /* route internally */ | ||
| 55 | xs *req = xs_dict_new(); | ||
| 56 | xs *q_vars = xs_dict_new(); | ||
| 57 | char *ctype; | ||
| 58 | |||
| 59 | q_vars = xs_dict_append(q_vars, "resource", resource); | ||
| 60 | req = xs_dict_append(req, "q_vars", q_vars); | ||
| 61 | |||
| 62 | webfinger_get_handler(req, "/.well-known/webfinger", | ||
| 63 | status, &payload, &p_size, &ctype); | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | xs *url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource=%s", host, resource); | ||
| 67 | |||
| 68 | printf("url: %s\n", url); | ||
| 69 | xs_http_request("GET", url, headers, NULL, 0, status, &payload, &p_size); | ||
| 70 | } | ||
| 71 | |||
| 72 | if (*status >= 200 && *status <= 299) { | ||
| 73 | xs *obj = xs_json_loads(payload); | ||
| 74 | |||
| 75 | if (user != NULL) { | ||
| 76 | *user = xs_replace(xs_dict_get(obj, "subject"), "acct:", ""); | ||
| 77 | } | ||
| 78 | |||
| 79 | if (actor != NULL) { | ||
| 80 | char *list = xs_dict_get(obj, "links"); | ||
| 81 | char *v; | ||
| 82 | |||
| 83 | while (xs_list_iter(&list, &v)) { | ||
| 84 | if (xs_type(v) == XSTYPE_SOD) { | ||
| 85 | char *type = xs_dict_get(v, "type"); | ||
| 86 | |||
| 87 | if (type && strcmp(type, "application/activity+json") == 0) { | ||
| 88 | *actor = xs_dup(xs_dict_get(v, "href")); | ||
| 89 | break; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 51 | } | 95 | } |
| 52 | 96 | ||
| 53 | 97 | ||
| @@ -135,4 +179,6 @@ void webfinger_get_handler(d_char *req, char *q_path, int *status, | |||
| 135 | *body = j; | 179 | *body = j; |
| 136 | *ctype = "application/json"; | 180 | *ctype = "application/json"; |
| 137 | } | 181 | } |
| 182 | else | ||
| 183 | *status = 404; | ||
| 138 | } | 184 | } |