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
|
|
|
|