diff options
| author | 2024-05-25 08:05:36 +0000 | |
|---|---|---|
| committer | 2024-05-25 08:05:36 +0000 | |
| commit | 84a767dd0878013194ed7551b5ae6ef715e841a6 (patch) | |
| tree | 9fb1b2b89e0bfbb4b8bf1e85d840c8653e646bb7 /http.c | |
| parent | Prevent some browsers from caching servers basic auth request (diff) | |
| parent | Backport from xs (fix regex.h compilation with tcc). (diff) | |
| download | snac2-84a767dd0878013194ed7551b5ae6ef715e841a6.tar.gz snac2-84a767dd0878013194ed7551b5ae6ef715e841a6.tar.xz snac2-84a767dd0878013194ed7551b5ae6ef715e841a6.zip | |
Merge pull request 'master' (#1) from grunfink/snac2:master into master
Reviewed-on: https://codeberg.org/louis77/snac2/pulls/1
Diffstat (limited to 'http.c')
| -rw-r--r-- | http.c | 36 |
1 files changed, 19 insertions, 17 deletions
| @@ -12,7 +12,7 @@ | |||
| 12 | 12 | ||
| 13 | xs_dict *http_signed_request_raw(const char *keyid, const char *seckey, | 13 | xs_dict *http_signed_request_raw(const char *keyid, const char *seckey, |
| 14 | const char *method, const char *url, | 14 | const char *method, const char *url, |
| 15 | xs_dict *headers, | 15 | const xs_dict *headers, |
| 16 | const char *body, int b_size, | 16 | const char *body, int b_size, |
| 17 | int *status, xs_str **payload, int *p_size, | 17 | int *status, xs_str **payload, int *p_size, |
| 18 | int timeout) | 18 | int timeout) |
| @@ -24,15 +24,16 @@ xs_dict *http_signed_request_raw(const char *keyid, const char *seckey, | |||
| 24 | xs *s64 = NULL; | 24 | xs *s64 = NULL; |
| 25 | xs *signature = NULL; | 25 | xs *signature = NULL; |
| 26 | xs *hdrs = NULL; | 26 | xs *hdrs = NULL; |
| 27 | char *host; | 27 | const char *host; |
| 28 | char *target; | 28 | const char *target; |
| 29 | char *k, *v; | 29 | const char *k, *v; |
| 30 | xs_dict *response; | 30 | xs_dict *response; |
| 31 | 31 | ||
| 32 | date = xs_str_utctime(0, "%a, %d %b %Y %H:%M:%S GMT"); | 32 | date = xs_str_utctime(0, "%a, %d %b %Y %H:%M:%S GMT"); |
| 33 | 33 | ||
| 34 | { | 34 | { |
| 35 | xs *s = xs_replace_n(url, "https:/" "/", "", 1); | 35 | xs *s1 = xs_replace_n(url, "http:/" "/", "", 1); |
| 36 | xs *s = xs_replace_n(s1, "https:/" "/", "", 1); | ||
| 36 | l1 = xs_split_n(s, "/", 1); | 37 | l1 = xs_split_n(s, "/", 1); |
| 37 | } | 38 | } |
| 38 | 39 | ||
| @@ -105,13 +106,13 @@ xs_dict *http_signed_request_raw(const char *keyid, const char *seckey, | |||
| 105 | 106 | ||
| 106 | 107 | ||
| 107 | xs_dict *http_signed_request(snac *snac, const char *method, const char *url, | 108 | xs_dict *http_signed_request(snac *snac, const char *method, const char *url, |
| 108 | xs_dict *headers, | 109 | const xs_dict *headers, |
| 109 | const char *body, int b_size, | 110 | const char *body, int b_size, |
| 110 | int *status, xs_str **payload, int *p_size, | 111 | int *status, xs_str **payload, int *p_size, |
| 111 | int timeout) | 112 | int timeout) |
| 112 | /* does a signed HTTP request */ | 113 | /* does a signed HTTP request */ |
| 113 | { | 114 | { |
| 114 | char *seckey = xs_dict_get(snac->key, "secret"); | 115 | const char *seckey = xs_dict_get(snac->key, "secret"); |
| 115 | xs_dict *response; | 116 | xs_dict *response; |
| 116 | 117 | ||
| 117 | response = http_signed_request_raw(snac->actor, seckey, method, url, | 118 | response = http_signed_request_raw(snac->actor, seckey, method, url, |
| @@ -121,17 +122,18 @@ xs_dict *http_signed_request(snac *snac, const char *method, const char *url, | |||
| 121 | } | 122 | } |
| 122 | 123 | ||
| 123 | 124 | ||
| 124 | int check_signature(xs_dict *req, xs_str **err) | 125 | int check_signature(const xs_dict *req, xs_str **err) |
| 125 | /* check the signature */ | 126 | /* check the signature */ |
| 126 | { | 127 | { |
| 127 | char *sig_hdr = xs_dict_get(req, "signature"); | 128 | const char *sig_hdr = xs_dict_get(req, "signature"); |
| 128 | xs *keyId = NULL; | 129 | xs *keyId = NULL; |
| 129 | xs *headers = NULL; | 130 | xs *headers = NULL; |
| 130 | xs *signature = NULL; | 131 | xs *signature = NULL; |
| 131 | xs *created = NULL; | 132 | xs *created = NULL; |
| 132 | xs *expires = NULL; | 133 | xs *expires = NULL; |
| 133 | char *pubkey; | ||
| 134 | char *p; | 134 | char *p; |
| 135 | const char *pubkey; | ||
| 136 | const char *k; | ||
| 135 | 137 | ||
| 136 | if (xs_is_null(sig_hdr)) { | 138 | if (xs_is_null(sig_hdr)) { |
| 137 | *err = xs_fmt("missing 'signature' header"); | 139 | *err = xs_fmt("missing 'signature' header"); |
| @@ -141,10 +143,10 @@ int check_signature(xs_dict *req, xs_str **err) | |||
| 141 | { | 143 | { |
| 142 | /* extract the values */ | 144 | /* extract the values */ |
| 143 | xs *l = xs_split(sig_hdr, ","); | 145 | xs *l = xs_split(sig_hdr, ","); |
| 144 | xs_list *p = l; | 146 | int c = 0; |
| 145 | xs_val *v; | 147 | const xs_val *v; |
| 146 | 148 | ||
| 147 | while (xs_list_iter(&p, &v)) { | 149 | while (xs_list_next(l, &v, &c)) { |
| 148 | xs *kv = xs_split_n(v, "=", 1); | 150 | xs *kv = xs_split_n(v, "=", 1); |
| 149 | 151 | ||
| 150 | if (xs_list_len(kv) != 2) | 152 | if (xs_list_len(kv) != 2) |
| @@ -191,8 +193,8 @@ int check_signature(xs_dict *req, xs_str **err) | |||
| 191 | return 0; | 193 | return 0; |
| 192 | } | 194 | } |
| 193 | 195 | ||
| 194 | if ((p = xs_dict_get(actor, "publicKey")) == NULL || | 196 | if ((k = xs_dict_get(actor, "publicKey")) == NULL || |
| 195 | ((pubkey = xs_dict_get(p, "publicKeyPem")) == NULL)) { | 197 | ((pubkey = xs_dict_get(k, "publicKeyPem")) == NULL)) { |
| 196 | *err = xs_fmt("cannot get pubkey from %s", keyId); | 198 | *err = xs_fmt("cannot get pubkey from %s", keyId); |
| 197 | return 0; | 199 | return 0; |
| 198 | } | 200 | } |
| @@ -203,11 +205,11 @@ int check_signature(xs_dict *req, xs_str **err) | |||
| 203 | { | 205 | { |
| 204 | xs *l = xs_split(headers, " "); | 206 | xs *l = xs_split(headers, " "); |
| 205 | xs_list *p; | 207 | xs_list *p; |
| 206 | xs_val *v; | 208 | const xs_val *v; |
| 207 | 209 | ||
| 208 | p = l; | 210 | p = l; |
| 209 | while (xs_list_iter(&p, &v)) { | 211 | while (xs_list_iter(&p, &v)) { |
| 210 | char *hc; | 212 | const char *hc; |
| 211 | xs *ss = NULL; | 213 | xs *ss = NULL; |
| 212 | 214 | ||
| 213 | if (*sig_str != '\0') | 215 | if (*sig_str != '\0') |