1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\ClientRegistrationEndpoint; |
15
|
|
|
|
16
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
17
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
18
|
|
|
use OAuth2Framework\Component\BearerTokenType\BearerToken; |
19
|
|
|
use OAuth2Framework\Component\Core\Message\OAuth2Message; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
|
23
|
|
|
final class InitialAccessTokenMiddleware implements MiddlewareInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var BearerToken |
27
|
|
|
*/ |
28
|
|
|
private $bearerToken; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var InitialAccessTokenRepository |
32
|
|
|
*/ |
33
|
|
|
private $initialAccessTokenRepository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $isRequired; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* InitialAccessTokenMiddleware constructor. |
42
|
|
|
* |
43
|
|
|
* @param BearerToken $bearerToken |
44
|
|
|
* @param InitialAccessTokenRepository $initialAccessTokenRepository |
45
|
|
|
* @param bool $isRequired |
46
|
|
|
*/ |
47
|
|
|
public function __construct(BearerToken $bearerToken, InitialAccessTokenRepository $initialAccessTokenRepository, bool $isRequired) |
48
|
|
|
{ |
49
|
|
|
$this->bearerToken = $bearerToken; |
50
|
|
|
$this->initialAccessTokenRepository = $initialAccessTokenRepository; |
51
|
|
|
$this->isRequired = $isRequired; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$values = []; |
61
|
|
|
$token = $this->bearerToken->find($request, $values); |
62
|
|
|
if (null === $token) { |
63
|
|
|
if (!$this->isRequired) { |
64
|
|
|
return $handler->handle($request); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
throw new \InvalidArgumentException('Initial Access Token is missing or invalid.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$initialAccessToken = $this->initialAccessTokenRepository->find(InitialAccessTokenId::create($token)); |
71
|
|
|
|
72
|
|
|
if (null === $initialAccessToken || $initialAccessToken->isRevoked()) { |
73
|
|
|
throw new \InvalidArgumentException('Initial Access Token is missing or invalid.'); |
74
|
|
|
} |
75
|
|
|
if ($initialAccessToken->hasExpired()) { |
76
|
|
|
throw new \InvalidArgumentException('Initial Access Token expired.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$request = $request->withAttribute('initial_access_token', $initialAccessToken); |
80
|
|
|
} catch (\InvalidArgumentException $e) { |
81
|
|
|
throw new OAuth2Message(400, OAuth2Message::ERROR_INVALID_REQUEST, $e->getMessage(), [], $e); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $handler->handle($request); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|