AuthRedirectHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 57
ccs 0
cts 33
cp 0
rs 10
c 1
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 31 7
A __construct() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\OpenIdClient\Middleware;
6
7
use Http\Discovery\Psr17FactoryDiscovery;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use function random_bytes;
13
use TMV\OpenIdClient\Authorization\AuthRequestInterface;
14
use function TMV\OpenIdClient\base64url_encode;
15
use TMV\OpenIdClient\Client\ClientInterface;
16
use TMV\OpenIdClient\Exception\LogicException;
17
use TMV\OpenIdClient\Exception\RuntimeException;
18
use TMV\OpenIdClient\Service\AuthorizationService;
19
use TMV\OpenIdClient\Session\AuthSessionInterface;
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
        $authSession = $request->getAttribute(AuthSessionInterface::class);
56
57
        if ($authSession instanceof AuthSessionInterface) {
58
            $state = $authRequest->getState() ?: base64url_encode(random_bytes($this->randomBytes));
59
            $nonce = $authRequest->getNonce() ?: base64url_encode(random_bytes($this->randomBytes));
60
61
            $authSession->setState($state);
62
            $authSession->setNonce($nonce);
63
64
            $authRequest = $authRequest->withParams(['state' => $state]);
65
            $authRequest = $authRequest->withParams(['nonce' => $nonce]);
66
        }
67
68
        $client = $this->client ?: $request->getAttribute(ClientInterface::class);
69
70
        if (! $client instanceof ClientInterface) {
71
            throw new LogicException('No OpenID client provided');
72
        }
73
74
        $uri = $this->authorizationService->getAuthorizationUri($client, $authRequest->createParams());
75
76
        return $this->responseFactory->createResponse(302)
77
            ->withHeader('location', $uri);
78
    }
79
}
80