diff options
| -rw-r--r-- | http.c | 65 | ||||
| -rw-r--r-- | main.c | 7 | ||||
| -rw-r--r-- | snac.h | 5 |
3 files changed, 74 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 | } |
| @@ -15,6 +15,13 @@ int main(int argc, char *argv[]) | |||
| 15 | 15 | ||
| 16 | user_open(&snac, "mike"); | 16 | user_open(&snac, "mike"); |
| 17 | 17 | ||
| 18 | d_char *headers = xs_dict_new(); | ||
| 19 | int status; | ||
| 20 | d_char *payload; | ||
| 21 | int p_size; | ||
| 22 | http_signed_request(&snac, "GET", "https://comam.es/snac/jessie", | ||
| 23 | headers, NULL, 0, &status, &payload, &p_size); | ||
| 24 | |||
| 18 | { | 25 | { |
| 19 | xs *list = queue(&snac); | 26 | xs *list = queue(&snac); |
| 20 | char *p, *fn; | 27 | char *p, *fn; |
| @@ -61,3 +61,8 @@ int is_muted(snac *snac, char *actor); | |||
| 61 | void enqueue(snac *snac, char *actor, char *msg, int retries); | 61 | void enqueue(snac *snac, char *actor, char *msg, int retries); |
| 62 | d_char *queue(snac *snac); | 62 | d_char *queue(snac *snac); |
| 63 | d_char *dequeue(snac *snac, char *fn); | 63 | d_char *dequeue(snac *snac, char *fn); |
| 64 | |||
| 65 | d_char *http_signed_request(snac *snac, char *method, char *url, | ||
| 66 | d_char *headers, | ||
| 67 | d_char *body, int b_size, | ||
| 68 | int *status, d_char **payload, int *p_size); | ||