nonceCache = $nonceCache; } /** @throws InvalidArgumentException|Exception */ protected function makeNonce(int $retries = 3): string { /* convert raw binary into base64url */ $nonce = rtrim(strtr(base64_encode(random_bytes( static::NONCE_LENGTH )), '+/', '-_'), '='); $nonceItem = $this->nonceCache->getItem($this->makeCacheKey($nonce)); if ($nonceItem->isHit()) { if ($retries < 1) { $this->logger->error("aborting: multiple nonce collisions"); throw new HttpException( Response::HTTP_INTERNAL_SERVER_ERROR, 'Internal Server Error' ); } /* managed to have a collision, try again */ return $this->makeNonce($retries - 1); } $nonceItem->set(true); /* valid */ $nonceItem->expiresAfter(static::NONCE_TTL); $this->logger->debug("added nonce: $nonce"); $this->nonceCache->save($nonceItem); return $nonce; } }