* Signed HTTP request * * https://datatracker.ietf.org/doc/html/rfc8555#section-6.2 * * @param {string} url Request URL * @param {object} payload Request payload * @param {object} [opts] * @param {string} [opts.kid] JWS KID * @param {string} [opts.nonce] JWS a
(url, payload, {
kid = null,
nonce = null,
includeExternalAccountBinding = false
} = {}, attempts = 0)
| 288 | */ |
| 289 | |
| 290 | async signedRequest(url, payload, { |
| 291 | kid = null, |
| 292 | nonce = null, |
| 293 | includeExternalAccountBinding = false |
| 294 | } = {}, attempts = 0) { |
| 295 | if (!nonce) { |
| 296 | nonce = await this.getNonce(); |
| 297 | } |
| 298 | |
| 299 | /* External account binding */ |
| 300 | if (includeExternalAccountBinding && this.externalAccountBinding) { |
| 301 | if (this.externalAccountBinding.kid && this.externalAccountBinding.hmacKey) { |
| 302 | const jwk = this.getJwk(); |
| 303 | const eabKid = this.externalAccountBinding.kid; |
| 304 | const eabHmacKey = this.externalAccountBinding.hmacKey; |
| 305 | |
| 306 | payload.externalAccountBinding = this.createSignedHmacBody(eabHmacKey, url, jwk, {kid: eabKid}); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* Sign body and send request */ |
| 311 | const data = this.createSignedBody(url, payload, {nonce, kid}); |
| 312 | const resp = await this.request(url, 'post', {data}); |
| 313 | |
| 314 | /* Retry on bad nonce - https://datatracker.ietf.org/doc/html/rfc8555#section-6.5 */ |
| 315 | if (resp.data && resp.data.type && (resp.status === 400) && (resp.data.type === 'urn:ietf:params:acme:error:badNonce') && (attempts < this.maxBadNonceRetries)) { |
| 316 | nonce = resp.headers['replay-nonce'] || null; |
| 317 | attempts += 1; |
| 318 | |
| 319 | log(`Caught invalid nonce error, retrying (${attempts}/${this.maxBadNonceRetries}) signed request to: ${url}`); |
| 320 | return this.signedRequest(url, payload, {kid, nonce, includeExternalAccountBinding}, attempts); |
| 321 | } |
| 322 | |
| 323 | /* Return response */ |
| 324 | return resp; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* Export client */ |
nothing calls this directly
no test coverage detected