Completed
Push — master ( 1de13c...bf55ce )
by Alex
33:38
created

ResourceServer::isValidRequest()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 3 Features 0
Metric Value
c 6
b 3
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 10
nc 6
nop 2
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
namespace League\OAuth2\Server;
10
11
use League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface;
12
use League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator;
13
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
class ResourceServer
17
{
18
    /**
19
     * @var \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface
20
     */
21
    private $accessTokenRepository;
22
    /**
23
     * @var \League\OAuth2\Server\CryptKey|string
24
     */
25
    private $publicKey;
26
    /**
27
     * @var \League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface|null
28
     */
29
    private $authorizationValidator;
30
31
    /**
32
     * New server instance.
33
     *
34
     * @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface                  $accessTokenRepository
35
     * @param \League\OAuth2\Server\CryptKey|string                                              $publicKey
36
     * @param null|\League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface $authorizationValidator
37
     */
38
    public function __construct(
39
        AccessTokenRepositoryInterface $accessTokenRepository,
40
        $publicKey,
41
        AuthorizationValidatorInterface $authorizationValidator = null
42
    ) {
43
        $this->accessTokenRepository = $accessTokenRepository;
44
45
        if (!$publicKey instanceof CryptKey) {
46
            $publicKey = new CryptKey($publicKey);
47
        }
48
        $this->publicKey = $publicKey;
49
50
        $this->authorizationValidator = $authorizationValidator;
51
    }
52
53
    /**
54
     * @return \League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface
55
     */
56
    protected function getAuthorizationValidator()
57
    {
58
        if (!$this->authorizationValidator instanceof AuthorizationValidatorInterface) {
59
            $this->authorizationValidator = new BearerTokenValidator($this->accessTokenRepository);
60
        }
61
62
        $this->authorizationValidator->setPublicKey($this->publicKey);
63
64
        return $this->authorizationValidator;
65
    }
66
67
    /**
68
     * Determine the access token validity.
69
     *
70
     * @param \Psr\Http\Message\ServerRequestInterface $request
71
     *
72
     * @throws \League\OAuth2\Server\Exception\OAuthServerException
73
     *
74
     * @return \Psr\Http\Message\ServerRequestInterface
75
     */
76
    public function validateAuthenticatedRequest(ServerRequestInterface $request)
77
    {
78
        return $this->getAuthorizationValidator()->validateAuthorization($request);
79
    }
80
}
81