Skip to content

Commit 1a4272f

Browse files
committed
authority.v2: invalidate nonces after 2 minutes and re-request
Boulder seems to invalidate older nonces after some time. Therefore we allow nonces from the cache to be used for up to 2 minutes and after those they will be considered invalid (and re-requested with an extra request to the nonce endpoint when necessary)
1 parent 514ff7c commit 1a4272f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

acertmgr/authority/v2.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from acertmgr.authority.acme import ACMEAuthority as AbstractACMEAuthority
1515
from acertmgr.tools import log
1616

17+
# Maximum age for nonce values (Boulder invalidates them after some time, so we use a low value of 2 minutes here)
18+
MAX_NONCE_AGE = 120
19+
1720

1821
class ACMEAuthority(AbstractACMEAuthority):
1922
# @brief Init class with config
@@ -45,6 +48,7 @@ def __init__(self, config, key):
4548
log("API directory retrieval failed ({}). Guessed necessary values: {}".format(code, self.directory),
4649
warning=True)
4750
self.nonce = None
51+
self.nonce_time = 0
4852

4953
self.algorithm, jwk = tools.get_key_alg_and_jwk(key)
5054
self.account_protected = {
@@ -71,6 +75,7 @@ def _request_url(self, url, data=None, raw_result=False):
7175
# Store next Replay-Nonce if it is in the header
7276
if 'Replay-Nonce' in resp.headers:
7377
self.nonce = resp.headers['Replay-Nonce']
78+
self.nonce_time = time.time()
7479

7580
body = resp.read()
7681
if getattr(body, 'decode', None):
@@ -95,7 +100,7 @@ def _request_acme_url(self, url, payload=None, protected=None, raw_result=False)
95100
payload64 = "" # for POST-as-GET
96101

97102
# Request a new nonce if there is none in cache
98-
if not self.nonce:
103+
if not self.nonce or time.time() > self.nonce_time + MAX_NONCE_AGE:
99104
self._request_url(self.directory['newNonce'])
100105
# Set request nonce to current cache value
101106
protected["nonce"] = self.nonce

0 commit comments

Comments
 (0)