1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Alex Bilbie <[email protected]> |
4
|
|
|
* @copyright Copyright (c) Alex Bilbie |
5
|
|
|
* @license http://mit-license.org/ |
6
|
|
|
* |
7
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace League\OAuth2\Server; |
11
|
|
|
|
12
|
|
|
use League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface; |
13
|
|
|
use League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator; |
14
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
15
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
17
|
|
|
|
18
|
|
|
class ResourceServer |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var AccessTokenRepositoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $accessTokenRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CryptKey |
27
|
|
|
*/ |
28
|
|
|
private $publicKey; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var null|AuthorizationValidatorInterface |
32
|
|
|
*/ |
33
|
|
|
private $authorizationValidator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* New server instance. |
37
|
|
|
* |
38
|
|
|
* @param AccessTokenRepositoryInterface $accessTokenRepository |
39
|
|
|
* @param CryptKey|string $publicKey |
40
|
|
|
* @param null|AuthorizationValidatorInterface $authorizationValidator |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository, |
44
|
|
|
$publicKey, |
45
|
|
|
AuthorizationValidatorInterface $authorizationValidator = null |
46
|
|
|
) { |
47
|
|
|
$this->accessTokenRepository = $accessTokenRepository; |
48
|
|
|
|
49
|
|
|
if ($publicKey instanceof CryptKey === false) { |
50
|
|
|
$publicKey = new CryptKey($publicKey); |
51
|
|
|
} |
52
|
|
|
$this->publicKey = $publicKey; |
53
|
|
|
|
54
|
|
|
$this->authorizationValidator = $authorizationValidator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return AuthorizationValidatorInterface |
59
|
|
|
*/ |
60
|
|
|
protected function getAuthorizationValidator() |
61
|
|
|
{ |
62
|
|
|
if ($this->authorizationValidator instanceof AuthorizationValidatorInterface === false) { |
63
|
|
|
$this->authorizationValidator = new BearerTokenValidator($this->accessTokenRepository); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->authorizationValidator instanceof BearerTokenValidator === true) { |
67
|
|
|
$this->authorizationValidator->setPublicKey($this->publicKey); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->authorizationValidator; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Determine the access token validity. |
75
|
|
|
* |
76
|
|
|
* @param ServerRequestInterface $request |
77
|
|
|
* |
78
|
|
|
* @throws OAuthServerException |
79
|
|
|
* |
80
|
|
|
* @return ServerRequestInterface |
81
|
|
|
*/ |
82
|
|
|
public function validateAuthenticatedRequest(ServerRequestInterface $request) |
83
|
|
|
{ |
84
|
|
|
return $this->getAuthorizationValidator()->validateAuthorization($request); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|