snac2/webfinger.c

234 lines
6.8 KiB
C
Raw Permalink Normal View History

2022-09-21 19:12:49 +00:00
/* snac - A simple, minimalistic ActivityPub instance */
2024-01-04 08:22:03 +00:00
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
2022-09-21 19:12:49 +00:00
#include "xs.h"
#include "xs_json.h"
#include "xs_curl.h"
#include "xs_mime.h"
2022-09-21 19:12:49 +00:00
#include "snac.h"
int webfinger_request_signed(snac *snac, const char *qs, char **actor, char **user)
/* queries the webfinger for qs and fills the required fields */
{
int status;
xs *payload = NULL;
int p_size = 0;
xs *headers = xs_dict_new();
2022-09-22 09:28:13 +00:00
xs *l = NULL;
2024-05-21 12:12:15 +00:00
const char *host = NULL;
2022-09-22 09:28:13 +00:00
xs *resource = NULL;
2024-04-18 15:13:31 +00:00
if (xs_startswith(qs, "https:/") || xs_startswith(qs, "http:/")) {
/* actor query: pick the host */
xs *s1 = xs_replace_n(qs, "http:/" "/", "", 1);
xs *s = xs_replace_n(s1, "https:/" "/", "", 1);
2022-09-22 09:28:13 +00:00
l = xs_split_n(s, "/", 1);
host = xs_list_get(l, 0);
resource = xs_dup(qs);
}
else {
/* it's a user */
xs *s = xs_strip_chars_i(xs_dup(qs), "@.");
l = xs_split_n(s, "@", 1);
if (xs_list_len(l) == 2) {
2022-09-22 09:28:13 +00:00
host = xs_list_get(l, 1);
2022-11-09 06:34:04 +00:00
resource = xs_fmt("acct:%s", s);
}
}
if (host == NULL || resource == NULL)
return HTTP_STATUS_BAD_REQUEST;
2023-05-12 18:33:29 +00:00
headers = xs_dict_append(headers, "accept", "application/json");
headers = xs_dict_append(headers, "user-agent", USER_AGENT);
xs *obj = NULL;
xs *cached_qs = xs_fmt("webfinger:%s", qs);
/* is it cached? */
if (valid_status(status = object_get(cached_qs, &obj))) {
/* nothing more to do */
}
else
2022-09-22 09:28:13 +00:00
/* is it a query about one of us? */
if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) {
/* route internally */
xs *req = xs_dict_new();
xs *q_vars = xs_dict_new();
char *ctype;
q_vars = xs_dict_append(q_vars, "resource", resource);
req = xs_dict_append(req, "q_vars", q_vars);
status = webfinger_get_handler(req, "/.well-known/webfinger",
&payload, &p_size, &ctype);
2022-09-22 09:28:13 +00:00
}
else {
2024-04-18 15:13:31 +00:00
const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
xs *url = xs_fmt("%s:/" "/%s/.well-known/webfinger?resource=%s", proto, host, resource);
2022-09-22 09:28:13 +00:00
if (snac == NULL)
xs_http_request("GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
else
http_signed_request(snac, "GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
2022-09-22 09:28:13 +00:00
}
if (obj == NULL && valid_status(status) && payload) {
obj = xs_json_loads(payload);
object_add(cached_qs, obj);
}
2022-09-22 09:28:13 +00:00
if (obj) {
2022-09-22 09:28:13 +00:00
if (user != NULL) {
2024-05-21 12:12:15 +00:00
const char *subject = xs_dict_get(obj, "subject");
2022-09-22 09:35:44 +00:00
if (subject)
2023-05-02 04:49:00 +00:00
*user = xs_replace_n(subject, "acct:", "", 1);
2022-09-22 09:28:13 +00:00
}
if (actor != NULL) {
2024-05-21 12:12:15 +00:00
const xs_list *list = xs_dict_get(obj, "links");
int c = 0;
2024-05-23 08:01:37 +00:00
const char *v;
2022-09-22 09:28:13 +00:00
2024-05-21 12:12:15 +00:00
while (xs_list_next(list, &v, &c)) {
2022-09-25 20:57:18 +00:00
if (xs_type(v) == XSTYPE_DICT) {
2024-05-21 12:12:15 +00:00
const char *type = xs_dict_get(v, "type");
2022-09-22 09:28:13 +00:00
2023-05-07 05:42:47 +00:00
if (type && (strcmp(type, "application/activity+json") == 0 ||
strcmp(type, "application/ld+json; profile=\"https:/"
"/www.w3.org/ns/activitystreams\"") == 0)) {
2022-09-22 09:28:13 +00:00
*actor = xs_dup(xs_dict_get(v, "href"));
break;
}
}
}
}
}
return status;
}
int webfinger_request(const char *qs, char **actor, char **user)
/* queries the webfinger for qs and fills the required fields */
{
return webfinger_request_signed(NULL, qs, actor, user);
}
2023-08-31 05:00:15 +00:00
int webfinger_get_handler(xs_dict *req, char *q_path,
char **body, int *b_size, char **ctype)
2022-09-21 19:12:49 +00:00
/* serves webfinger queries */
{
int status;
2023-05-04 07:28:36 +00:00
(void)b_size;
2022-09-21 19:12:49 +00:00
if (strcmp(q_path, "/.well-known/webfinger") != 0)
return 0;
2022-09-21 19:12:49 +00:00
2024-05-21 12:12:15 +00:00
const char *q_vars = xs_dict_get(req, "q_vars");
const char *resource = xs_dict_get(q_vars, "resource");
2022-09-21 19:12:49 +00:00
if (resource == NULL)
return HTTP_STATUS_BAD_REQUEST;
2022-09-21 19:12:49 +00:00
snac snac;
int found = 0;
2024-04-18 15:13:31 +00:00
if (xs_startswith(resource, "https:/") || xs_startswith(resource, "http:/")) {
2022-09-21 19:12:49 +00:00
/* actor search: find a user with this actor */
xs *l = xs_split(resource, "/");
2024-05-21 12:12:15 +00:00
const char *uid = xs_list_get(l, -1);
2022-09-21 19:12:49 +00:00
if (uid)
found = user_open(&snac, uid);
2022-09-21 19:12:49 +00:00
}
else
if (xs_startswith(resource, "acct:")) {
/* it's an account name */
2023-05-02 04:49:00 +00:00
xs *an = xs_replace_n(resource, "acct:", "", 1);
2022-09-21 19:12:49 +00:00
xs *l = NULL;
/* strip a possible leading @ */
if (xs_startswith(an, "@"))
2023-01-12 08:28:02 +00:00
an = xs_crop_i(an, 1, 0);
2022-09-21 19:12:49 +00:00
l = xs_split_n(an, "@", 1);
if (xs_list_len(l) == 2) {
2024-05-21 12:12:15 +00:00
const char *uid = xs_list_get(l, 0);
const char *host = xs_list_get(l, 1);
2022-09-21 19:12:49 +00:00
if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
found = user_open(&snac, uid);
}
}
if (found) {
/* build the object */
xs *acct;
xs *aaj = xs_dict_new();
2024-02-16 19:58:10 +00:00
xs *prof = xs_dict_new();
2022-09-21 19:12:49 +00:00
xs *links = xs_list_new();
xs *obj = xs_dict_new();
acct = xs_fmt("acct:%s@%s",
xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
aaj = xs_dict_append(aaj, "rel", "self");
aaj = xs_dict_append(aaj, "type", "application/activity+json");
aaj = xs_dict_append(aaj, "href", snac.actor);
links = xs_list_append(links, aaj);
/* duplicate with the ld+json type */
aaj = xs_dict_set(aaj, "type", "application/ld+json; profile=\"https:/"
"/www.w3.org/ns/activitystreams\"");
links = xs_list_append(links, aaj);
2024-02-16 19:58:10 +00:00
prof = xs_dict_append(prof, "rel", "http://webfinger.net/rel/profile-page");
prof = xs_dict_append(prof, "type", "text/html");
prof = xs_dict_append(prof, "href", snac.actor);
2024-02-16 19:58:10 +00:00
links = xs_list_append(links, prof);
2024-05-21 12:12:15 +00:00
const char *avatar = xs_dict_get(snac.config, "avatar");
if (!xs_is_null(avatar) && *avatar) {
xs *d = xs_dict_new();
d = xs_dict_append(d, "rel", "http:/" "/webfinger.net/rel/avatar");
d = xs_dict_append(d, "type", xs_mime_by_ext(avatar));
d = xs_dict_append(d, "href", avatar);
links = xs_list_append(links, d);
}
2022-09-21 19:12:49 +00:00
obj = xs_dict_append(obj, "subject", acct);
obj = xs_dict_append(obj, "links", links);
2023-08-31 05:00:15 +00:00
xs_str *j = xs_json_dumps(obj, 4);
2022-09-21 19:12:49 +00:00
user_free(&snac);
status = HTTP_STATUS_OK;
*body = j;
*ctype = "application/jrd+json";
2022-09-21 19:12:49 +00:00
}
2022-09-22 09:28:13 +00:00
else
status = HTTP_STATUS_NOT_FOUND;
2024-05-05 07:34:14 +00:00
srv_debug(1, xs_fmt("webfinger_get_handler resource=%s %d", resource, status));
2024-05-05 07:30:24 +00:00
return status;
2022-09-21 19:12:49 +00:00
}