Added http signed request code (untested).

This commit is contained in:
default 2022-09-20 21:00:16 +02:00
parent 9a2e7d4b82
commit d76131b473
3 changed files with 74 additions and 3 deletions

65
http.c
View file

@ -13,8 +13,67 @@ d_char *http_signed_request(snac *snac, char *method, char *url,
d_char *headers, d_char *headers,
d_char *body, int b_size, d_char *body, int b_size,
int *status, d_char **payload, int *p_size) int *status, d_char **payload, int *p_size)
/* does an HTTP request */ /* does a signed HTTP request */
{ {
return xs_http_request(method, url, headers, xs *l1;
body, b_size, status, payload, p_size); xs *date;
xs *digest;
xs *s64;
xs *signature;
char *host;
char *target;
char *seckey;
date = xs_utc_time("%a, %d %b %Y %H:%M:%S GMT");
{
xs *s = xs_replace(url, "https:/" "/", "");
l1 = xs_split_n(s, "/", 1);
}
/* strip the url to get host and target */
host = xs_list_get(l1, 0);
if (xs_list_len(l1) == 2)
target = xs_list_get(l1, 1);
else
target = "";
/* digest */
if (body != NULL)
digest = xs_sha256_hex(body, b_size);
else
digest = xs_sha256_hex("", 0);
seckey = xs_dict_get(snac->key, "secret");
{
/* build the string to be signed */
xs *s = xs_fmt("(request-target): %s /%s\n"
"host: %s\n"
"digest: SHA-256=%s\n"
"date: %s",
strcmp(method, "POST") == 0 ? "post" : "get",
target, host, digest, date);
s64 = xs_rsa_sign(seckey, s, strlen(s));
}
/* build now the signature header */
signature = xs_fmt("keyId=\"%s#main-key\","
"algorithm=\"rsa-sha256\","
"headers=\"(request-target) host digest date\","
"signature=\"%s\"",
snac->actor, s64);
/* now add all these things to the headers */
headers = xs_dict_append(headers, "content-type", "application/activity+json");
headers = xs_dict_append(headers, "date", date);
headers = xs_dict_append(headers, "signature", signature);
headers = xs_dict_append(headers, "digest", digest);
headers = xs_dict_append(headers, "user-agent", "snac/2.x");
// return xs_http_request(method, url, headers,
// body, b_size, status, payload, p_size);
return NULL;
} }

7
main.c
View file

@ -15,6 +15,13 @@ int main(int argc, char *argv[])
user_open(&snac, "mike"); user_open(&snac, "mike");
d_char *headers = xs_dict_new();
int status;
d_char *payload;
int p_size;
http_signed_request(&snac, "GET", "https://comam.es/snac/jessie",
headers, NULL, 0, &status, &payload, &p_size);
{ {
xs *list = queue(&snac); xs *list = queue(&snac);
char *p, *fn; char *p, *fn;

5
snac.h
View file

@ -61,3 +61,8 @@ int is_muted(snac *snac, char *actor);
void enqueue(snac *snac, char *actor, char *msg, int retries); void enqueue(snac *snac, char *actor, char *msg, int retries);
d_char *queue(snac *snac); d_char *queue(snac *snac);
d_char *dequeue(snac *snac, char *fn); d_char *dequeue(snac *snac, char *fn);
d_char *http_signed_request(snac *snac, char *method, char *url,
d_char *headers,
d_char *body, int b_size,
int *status, d_char **payload, int *p_size);