|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace TMV\OpenIdClient\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use TMV\OpenIdClient\Client\ClientInterface; |
|
12
|
|
|
use TMV\OpenIdClient\Exception\LogicException; |
|
13
|
|
|
use TMV\OpenIdClient\Service\AuthorizationService; |
|
14
|
|
|
use TMV\OpenIdClient\Session\AuthSessionInterface; |
|
15
|
|
|
use TMV\OpenIdClient\Token\TokenSetInterface; |
|
16
|
|
|
|
|
17
|
|
|
class CallbackMiddleware implements MiddlewareInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var AuthorizationService */ |
|
20
|
|
|
private $authorizationService; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string|null */ |
|
23
|
|
|
private $redirectUri; |
|
24
|
|
|
|
|
25
|
|
|
/** @var null|ClientInterface */ |
|
26
|
|
|
private $client; |
|
27
|
|
|
|
|
28
|
|
|
/** @var null|int */ |
|
29
|
|
|
private $maxAge; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
AuthorizationService $authorizationService, |
|
33
|
|
|
?ClientInterface $client = null, |
|
34
|
|
|
?string $redirectUri = null, |
|
35
|
|
|
?int $maxAge = null |
|
36
|
|
|
) { |
|
37
|
|
|
$this->authorizationService = $authorizationService; |
|
38
|
|
|
$this->client = $client; |
|
39
|
|
|
$this->redirectUri = $redirectUri; |
|
40
|
|
|
$this->maxAge = $maxAge; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
44
|
|
|
{ |
|
45
|
|
|
$client = $this->client ?: $request->getAttribute(ClientInterface::class); |
|
46
|
|
|
$authSession = $request->getAttribute(AuthSessionInterface::class); |
|
47
|
|
|
|
|
48
|
|
|
if (! $client instanceof ClientInterface) { |
|
49
|
|
|
throw new LogicException('No OpenID client provided'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (null !== $authSession && ! $authSession instanceof AuthSessionInterface) { |
|
53
|
|
|
throw new LogicException('Invalid auth session provided in attribute ' . AuthSessionInterface::class); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$params = $this->authorizationService->getCallbackParams($request, $client); |
|
57
|
|
|
$tokenSet = $this->authorizationService->callback( |
|
58
|
|
|
$client, |
|
59
|
|
|
$params, |
|
60
|
|
|
$this->redirectUri, |
|
61
|
|
|
$authSession, |
|
62
|
|
|
$this->maxAge |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
return $handler->handle($request->withAttribute(TokenSetInterface::class, $tokenSet)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|