AuthRedirectHandler::handle()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
nc 5
nop 1
dl 0
loc 32
ccs 0
cts 17
cp 0
crap 20
rs 9.7
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClient\Middleware;
6
7
use Facile\OpenIDClient\Authorization\AuthRequestInterface;
8
use function Facile\OpenIDClient\base64url_encode;
9
use Facile\OpenIDClient\Client\ClientInterface;
10
use Facile\OpenIDClient\Exception\LogicException;
11
use Facile\OpenIDClient\Exception\RuntimeException;
12
use Facile\OpenIDClient\Service\AuthorizationService;
13
use Facile\OpenIDClient\Session\AuthSessionInterface;
14
use Http\Discovery\Psr17FactoryDiscovery;
15
use Psr\Http\Message\ResponseFactoryInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
use function random_bytes;
20
21
class AuthRedirectHandler implements RequestHandlerInterface
22
{
23
    /** @var AuthorizationService */
24
    private $authorizationService;
25
26
    /** @var ResponseFactoryInterface */
27
    private $responseFactory;
28
29
    /** @var null|ClientInterface */
30
    private $client;
31
32
    /** @var int */
33
    private $randomBytes;
34
35
    public function __construct(
36
        AuthorizationService $authorizationService,
37
        ?ResponseFactoryInterface $responseFactory = null,
38
        ?ClientInterface $client = null,
39
        int $randomBytes = 32
40
    ) {
41
        $this->authorizationService = $authorizationService;
42
        $this->responseFactory = $responseFactory ?? Psr17FactoryDiscovery::findResponseFactory();
43
        $this->client = $client;
44
        $this->randomBytes = $randomBytes;
45
    }
46
47
    public function handle(ServerRequestInterface $request): ResponseInterface
48
    {
49
        $authRequest = $request->getAttribute(AuthRequestInterface::class);
50
51
        if (! $authRequest instanceof AuthRequestInterface) {
52
            throw new RuntimeException('Unable to find a valid attribute for ' . AuthRequestInterface::class);
53
        }
54
55
        /** @var null|AuthSessionInterface $authSession */
56
        $authSession = $request->getAttribute(AuthSessionInterface::class);
57
58
        if ($authSession instanceof AuthSessionInterface) {
59
            $state = $authRequest->getState() ?? base64url_encode(random_bytes($this->randomBytes));
60
            $nonce = $authRequest->getNonce() ?? base64url_encode(random_bytes($this->randomBytes));
61
62
            $authSession->setState($state);
63
            $authSession->setNonce($nonce);
64
65
            $authRequest = $authRequest->withParams(['state' => $state]);
66
            $authRequest = $authRequest->withParams(['nonce' => $nonce]);
67
        }
68
69
        $client = $this->client ?? $request->getAttribute(ClientInterface::class);
70
71
        if (! $client instanceof ClientInterface) {
72
            throw new LogicException('No OpenID client provided');
73
        }
74
75
        $uri = $this->authorizationService->getAuthorizationUri($client, $authRequest->createParams());
76
77
        return $this->responseFactory->createResponse(302)
78
            ->withHeader('location', $uri);
79
    }
80
}
81