Passed
Pull Request — master (#1122)
by Sebastian
02:02
created

ResourceServer::getAuthorizationValidator()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 12
rs 10
c 1
b 0
f 0
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