2.55
webfinger.c
1/* snac - A simple, minimalistic ActivityPub instance */
2/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
3
4#include "xs.h"
5#include "xs_json.h"
6#include "xs_curl.h"
7#include "xs_mime.h"
8
9#include "snac.h"
10
11int webfinger_request_signed(snac *snac, const char *qs, char **actor, char **user)
12/* queries the webfinger for qs and fills the required fields */
13{
14 int status;
15 xs *payload = NULL;
16 int p_size = 0;
17 xs *headers = xs_dict_new();
18 xs *l = NULL;
19 const char *host = NULL;
20 xs *resource = NULL;
21
22 if (xs_startswith(qs, "https:/") || xs_startswith(qs, "http:/")) {
23 /* actor query: pick the host */
24 xs *s1 = xs_replace_n(qs, "http:/" "/", "", 1);
25 xs *s = xs_replace_n(s1, "https:/" "/", "", 1);
26
27 l = xs_split_n(s, "/", 1);
28
29 host = xs_list_get(l, 0);
30 resource = xs_dup(qs);
31 }
32 else {
33 /* it's a user */
34 xs *s = xs_strip_chars_i(xs_dup(qs), "@.");
35
36 l = xs_split_n(s, "@", 1);
37
38 if (xs_list_len(l) == 2) {
39 host = xs_list_get(l, 1);
40 resource = xs_fmt("acct:%s", s);
41 }
42 }
43
44 if (host == NULL || resource == NULL)
45 return HTTP_STATUS_BAD_REQUEST;
46
47 headers = xs_dict_append(headers, "accept", "application/json");
48 headers = xs_dict_append(headers, "user-agent", USER_AGENT);
49
50 xs *obj = NULL;
51
52 xs *cached_qs = xs_fmt("webfinger:%s", qs);
53
54 /* is it cached? */
55 if (valid_status(status = object_get(cached_qs, &obj))) {
56 /* nothing more to do */
57 }
58 else
59 /* is it a query about one of us? */
60 if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) {
61 /* route internally */
62 xs *req = xs_dict_new();
63 xs *q_vars = xs_dict_new();
64 char *ctype;
65
66 q_vars = xs_dict_append(q_vars, "resource", resource);
67 req = xs_dict_append(req, "q_vars", q_vars);
68
69 status = webfinger_get_handler(req, "/.well-known/webfinger",
70 &payload, &p_size, &ctype);
71 }
72 else {
73 const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
74
75 xs *url = xs_fmt("%s:/" "/%s/.well-known/webfinger?resource=%s", proto, host, resource);
76
77 if (snac == NULL)
78 xs_http_request("GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
79 else
80 http_signed_request(snac, "GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
81 }
82
83 if (obj == NULL && valid_status(status) && payload) {
84 obj = xs_json_loads(payload);
85 object_add(cached_qs, obj);
86 }
87
88 if (obj) {
89 if (user != NULL) {
90 const char *subject = xs_dict_get(obj, "subject");
91
92 if (subject)
93 *user = xs_replace_n(subject, "acct:", "", 1);
94 }
95
96 if (actor != NULL) {
97 const xs_list *list = xs_dict_get(obj, "links");
98 int c = 0;
99 const char *v;
100
101 while (xs_list_next(list, &v, &c)) {
102 if (xs_type(v) == XSTYPE_DICT) {
103 const char *type = xs_dict_get(v, "type");
104
105 if (type && (strcmp(type, "application/activity+json") == 0 ||
106 strcmp(type, "application/ld+json; profile=\"https:/"
107 "/www.w3.org/ns/activitystreams\"") == 0)) {
108 *actor = xs_dup(xs_dict_get(v, "href"));
109 break;
110 }
111 }
112 }
113 }
114 }
115
116 return status;
117}
118
119
120int webfinger_request(const char *qs, char **actor, char **user)
121/* queries the webfinger for qs and fills the required fields */
122{
123 return webfinger_request_signed(NULL, qs, actor, user);
124}
125
126
127int webfinger_get_handler(xs_dict *req, char *q_path,
128 char **body, int *b_size, char **ctype)
129/* serves webfinger queries */
130{
131 int status;
132
133 (void)b_size;
134
135 if (strcmp(q_path, "/.well-known/webfinger") != 0)
136 return 0;
137
138 const char *q_vars = xs_dict_get(req, "q_vars");
139 const char *resource = xs_dict_get(q_vars, "resource");
140
141 if (resource == NULL)
142 return HTTP_STATUS_BAD_REQUEST;
143
144 snac snac;
145 int found = 0;
146
147 if (xs_startswith(resource, "https:/") || xs_startswith(resource, "http:/")) {
148 /* actor search: find a user with this actor */
149 xs *l = xs_split(resource, "/");
150 const char *uid = xs_list_get(l, -1);
151
152 if (uid)
153 found = user_open(&snac, uid);
154 }
155 else
156 if (xs_startswith(resource, "acct:")) {
157 /* it's an account name */
158 xs *an = xs_replace_n(resource, "acct:", "", 1);
159 xs *l = NULL;
160
161 /* strip a possible leading @ */
162 if (xs_startswith(an, "@"))
163 an = xs_crop_i(an, 1, 0);
164
165 l = xs_split_n(an, "@", 1);
166
167 if (xs_list_len(l) == 2) {
168 const char *uid = xs_list_get(l, 0);
169 const char *host = xs_list_get(l, 1);
170
171 if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
172 found = user_open(&snac, uid);
173 }
174 }
175
176 if (found) {
177 /* build the object */
178 xs *acct;
179 xs *aaj = xs_dict_new();
180 xs *prof = xs_dict_new();
181 xs *links = xs_list_new();
182 xs *obj = xs_dict_new();
183
184 acct = xs_fmt("acct:%s@%s",
185 xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
186
187 aaj = xs_dict_append(aaj, "rel", "self");
188 aaj = xs_dict_append(aaj, "type", "application/activity+json");
189 aaj = xs_dict_append(aaj, "href", snac.actor);
190
191 links = xs_list_append(links, aaj);
192
193 /* duplicate with the ld+json type */
194 aaj = xs_dict_set(aaj, "type", "application/ld+json; profile=\"https:/"
195 "/www.w3.org/ns/activitystreams\"");
196
197 links = xs_list_append(links, aaj);
198
199 prof = xs_dict_append(prof, "rel", "http://webfinger.net/rel/profile-page");
200 prof = xs_dict_append(prof, "type", "text/html");
201 prof = xs_dict_append(prof, "href", snac.actor);
202
203 links = xs_list_append(links, prof);
204
205 const char *avatar = xs_dict_get(snac.config, "avatar");
206 if (!xs_is_null(avatar) && *avatar) {
207 xs *d = xs_dict_new();
208
209 d = xs_dict_append(d, "rel", "http:/" "/webfinger.net/rel/avatar");
210 d = xs_dict_append(d, "type", xs_mime_by_ext(avatar));
211 d = xs_dict_append(d, "href", avatar);
212
213 links = xs_list_append(links, d);
214 }
215
216 obj = xs_dict_append(obj, "subject", acct);
217 obj = xs_dict_append(obj, "links", links);
218
219 xs_str *j = xs_json_dumps(obj, 4);
220
221 user_free(&snac);
222
223 status = HTTP_STATUS_OK;
224 *body = j;
225 *ctype = "application/jrd+json";
226 }
227 else
228 status = HTTP_STATUS_NOT_FOUND;
229
230 srv_debug(1, xs_fmt("webfinger_get_handler resource=%s %d", resource, status));
231
232 return status;
233}