Completed
Pull Request — master (#1074)
by Andrew
35:29 queued 47s
created

DeviceGrantMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 11
c 3
b 2
f 0
dl 0
loc 28
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A process() 0 14 2
1
<?php
2
3
namespace League\OAuth2\Server\Middleware;
4
5
use League\OAuth2\Server\Exception\OAuthServerException;
6
use League\OAuth2\Server\Repositories\DeviceAuthorizationRequestRepository;
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
13
class DeviceGrantMiddleware implements MiddlewareInterface
14
{
15
    private $deviceAuthorizationRequestRepository;
16
    private $responseFactory;
17
18
    public function __construct(
19
        DeviceAuthorizationRequestRepository $deviceAuthorizationRequestRepository,
20
        ResponseFactoryInterface $responseFactory
21
    )
22
    {
23
        $this->deviceAuthorizationRequestRepository = $deviceAuthorizationRequestRepository;
24
        $this->responseFactory = $responseFactory;
25
    }
26
27
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
28
    {
29
        $queryParameters = $request->getQueryParams();
30
        $deviceCode = $queryParameters['device_code'];
31
32
        // Get the last timestamp this client requested an access code
33
        $lastRequestTimeStamp = $this->deviceAuthorizationRequestRepository->getLast($deviceCode);
34
35
        // If the request is within the last 5 seconds, issue a slowdown notification
36
        if ($lastRequestTimeStamp + 5 > \time()) {
37
            return OAuthServerException::slowDown()->generateHttpResponse($this->responseFactory->createResponse());
38
        }
39
40
        return $handler->handle($request);
41
    }
42
}
43