diff options
| author | 2022-09-20 21:00:16 +0200 | |
|---|---|---|
| committer | 2022-09-20 21:00:16 +0200 | |
| commit | d76131b4737832585e58bda6e47125d8fb9bd8b4 (patch) | |
| tree | 537982247ce0d00efa309fea46a7de345b76c4c3 /http.c | |
| parent | Updated Makefile. (diff) | |
| download | penes-snac2-d76131b4737832585e58bda6e47125d8fb9bd8b4.tar.gz penes-snac2-d76131b4737832585e58bda6e47125d8fb9bd8b4.tar.xz penes-snac2-d76131b4737832585e58bda6e47125d8fb9bd8b4.zip | |
Added http signed request code (untested).
Diffstat (limited to 'http.c')
| -rw-r--r-- | http.c | 65 |
1 files changed, 62 insertions, 3 deletions
| @@ -13,8 +13,67 @@ d_char *http_signed_request(snac *snac, char *method, char *url, | |||
| 13 | d_char *headers, | 13 | d_char *headers, |
| 14 | d_char *body, int b_size, | 14 | d_char *body, int b_size, |
| 15 | int *status, d_char **payload, int *p_size) | 15 | int *status, d_char **payload, int *p_size) |
| 16 | /* does an HTTP request */ | 16 | /* does a signed HTTP request */ |
| 17 | { | 17 | { |
| 18 | return xs_http_request(method, url, headers, | 18 | xs *l1; |
| 19 | body, b_size, status, payload, p_size); | 19 | xs *date; |
| 20 | xs *digest; | ||
| 21 | xs *s64; | ||
| 22 | xs *signature; | ||
| 23 | char *host; | ||
| 24 | char *target; | ||
| 25 | char *seckey; | ||
| 26 | |||
| 27 | date = xs_utc_time("%a, %d %b %Y %H:%M:%S GMT"); | ||
| 28 | |||
| 29 | { | ||
| 30 | xs *s = xs_replace(url, "https:/" "/", ""); | ||
| 31 | l1 = xs_split_n(s, "/", 1); | ||
| 32 | } | ||
| 33 | |||
| 34 | /* strip the url to get host and target */ | ||
| 35 | host = xs_list_get(l1, 0); | ||
| 36 | |||
| 37 | if (xs_list_len(l1) == 2) | ||
| 38 | target = xs_list_get(l1, 1); | ||
| 39 | else | ||
| 40 | target = ""; | ||
| 41 | |||
| 42 | /* digest */ | ||
| 43 | if (body != NULL) | ||
| 44 | digest = xs_sha256_hex(body, b_size); | ||
| 45 | else | ||
| 46 | digest = xs_sha256_hex("", 0); | ||
| 47 | |||
| 48 | seckey = xs_dict_get(snac->key, "secret"); | ||
| 49 | |||
| 50 | { | ||
| 51 | /* build the string to be signed */ | ||
| 52 | xs *s = xs_fmt("(request-target): %s /%s\n" | ||
| 53 | "host: %s\n" | ||
| 54 | "digest: SHA-256=%s\n" | ||
| 55 | "date: %s", | ||
| 56 | strcmp(method, "POST") == 0 ? "post" : "get", | ||
| 57 | target, host, digest, date); | ||
| 58 | |||
| 59 | s64 = xs_rsa_sign(seckey, s, strlen(s)); | ||
| 60 | } | ||
| 61 | |||
| 62 | /* build now the signature header */ | ||
| 63 | signature = xs_fmt("keyId=\"%s#main-key\"," | ||
| 64 | "algorithm=\"rsa-sha256\"," | ||
| 65 | "headers=\"(request-target) host digest date\"," | ||
| 66 | "signature=\"%s\"", | ||
| 67 | snac->actor, s64); | ||
| 68 | |||
| 69 | /* now add all these things to the headers */ | ||
| 70 | headers = xs_dict_append(headers, "content-type", "application/activity+json"); | ||
| 71 | headers = xs_dict_append(headers, "date", date); | ||
| 72 | headers = xs_dict_append(headers, "signature", signature); | ||
| 73 | headers = xs_dict_append(headers, "digest", digest); | ||
| 74 | headers = xs_dict_append(headers, "user-agent", "snac/2.x"); | ||
| 75 | |||
| 76 | // return xs_http_request(method, url, headers, | ||
| 77 | // body, b_size, status, payload, p_size); | ||
| 78 | return NULL; | ||
| 20 | } | 79 | } |