Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 78... Línea 78...
78
    public function __construct(
78
    public function __construct(
79
        string $jwksUri,
79
        string $jwksUri,
80
        ClientInterface $httpClient,
80
        ClientInterface $httpClient,
81
        RequestFactoryInterface $httpFactory,
81
        RequestFactoryInterface $httpFactory,
82
        CacheItemPoolInterface $cache,
82
        CacheItemPoolInterface $cache,
83
        int $expiresAfter = null,
83
        ?int $expiresAfter = null,
84
        bool $rateLimit = false,
84
        bool $rateLimit = false,
85
        string $defaultAlg = null
85
        ?string $defaultAlg = null
86
    ) {
86
    ) {
87
        $this->jwksUri = $jwksUri;
87
        $this->jwksUri = $jwksUri;
88
        $this->httpClient = $httpClient;
88
        $this->httpClient = $httpClient;
89
        $this->httpFactory = $httpFactory;
89
        $this->httpFactory = $httpFactory;
90
        $this->cache = $cache;
90
        $this->cache = $cache;
Línea 178... Línea 178...
178
            }
178
            }
179
            $request = $this->httpFactory->createRequest('GET', $this->jwksUri);
179
            $request = $this->httpFactory->createRequest('GET', $this->jwksUri);
180
            $jwksResponse = $this->httpClient->sendRequest($request);
180
            $jwksResponse = $this->httpClient->sendRequest($request);
181
            if ($jwksResponse->getStatusCode() !== 200) {
181
            if ($jwksResponse->getStatusCode() !== 200) {
182
                throw new UnexpectedValueException(
182
                throw new UnexpectedValueException(
183
                    sprintf('HTTP Error: %d %s for URI "%s"',
183
                    \sprintf('HTTP Error: %d %s for URI "%s"',
184
                        $jwksResponse->getStatusCode(),
184
                        $jwksResponse->getStatusCode(),
185
                        $jwksResponse->getReasonPhrase(),
185
                        $jwksResponse->getReasonPhrase(),
186
                        $this->jwksUri,
186
                        $this->jwksUri,
187
                    ),
187
                    ),
188
                    $jwksResponse->getStatusCode()
188
                    $jwksResponse->getStatusCode()
Línea 210... Línea 210...
210
        if (!$this->rateLimit) {
210
        if (!$this->rateLimit) {
211
            return false;
211
            return false;
212
        }
212
        }
Línea 213... Línea 213...
213
 
213
 
-
 
214
        $cacheItem = $this->cache->getItem($this->rateLimitCacheKey);
214
        $cacheItem = $this->cache->getItem($this->rateLimitCacheKey);
215
 
215
        if (!$cacheItem->isHit()) {
216
        $cacheItemData = [];
-
 
217
        if ($cacheItem->isHit() && \is_array($data = $cacheItem->get())) {
216
            $cacheItem->expiresAfter(1); // # of calls are cached each minute
218
            $cacheItemData = $data;
Línea 217... Línea 219...
217
        }
219
        }
-
 
220
 
-
 
221
        $callsPerMinute = $cacheItemData['callsPerMinute'] ?? 0;
218
 
222
        $expiry = $cacheItemData['expiry'] ?? new \DateTime('+60 seconds', new \DateTimeZone('UTC'));
219
        $callsPerMinute = (int) $cacheItem->get();
223
 
220
        if (++$callsPerMinute > $this->maxCallsPerMinute) {
224
        if (++$callsPerMinute > $this->maxCallsPerMinute) {
-
 
225
            return true;
-
 
226
        }
221
            return true;
227
 
222
        }
228
        $cacheItem->set(['expiry' => $expiry, 'callsPerMinute' => $callsPerMinute]);
223
        $cacheItem->set($callsPerMinute);
229
        $cacheItem->expiresAt($expiry);
224
        $this->cache->save($cacheItem);
230
        $this->cache->save($cacheItem);
Línea 225... Línea 231...
225
        return false;
231
        return false;