Completed
Pull Request — master (#925)
by Steve
01:48
created

Introspector::validateIntrospectionRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace League\OAuth2\Server;
4
5
use League\OAuth2\Server\Exception\OAuthServerException;
6
use League\OAuth2\Server\IntrospectionValidators\BearerTokenValidator;
7
use League\OAuth2\Server\IntrospectionValidators\IntrospectionValidatorInterface;
8
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
9
use League\OAuth2\Server\ResponseTypes\IntrospectionResponse;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
class Introspector
13
{
14
    /**
15
     * @var AccessTokenRepositoryInterface
16
     */
17
    private $accessTokenRepository;
18
19
    /**
20
     * @var CryptKey
21
     */
22
    private $privateKey;
23
24
    /**
25
     * @var null|IntrospectionValidatorInterface
26
     */
27
    private $introspectionValidator;
28
29
    /**
30
     * New Introspector instance.
31
     *
32
     * @param AccessTokenRepositoryInterface  $accessTokenRepository
33
     * @param CryptKey                        $privateKey
34
     * @param IntrospectionValidatorInterface $introspectionValidator
35
     */
36 4
    public function __construct(
37
        AccessTokenRepositoryInterface $accessTokenRepository,
38
        CryptKey $privateKey,
39
        IntrospectionValidatorInterface $introspectionValidator = null
40
    ) {
41 4
        $this->accessTokenRepository = $accessTokenRepository;
42 4
        $this->privateKey = $privateKey;
43 4
        $this->introspectionValidator = $introspectionValidator;
44 4
    }
45
46
    /**
47
     * Validate the introspection request.
48
     *
49
     * @param ServerRequestInterface $request
50
     *
51
     * @throws OAuthServerException
52
     */
53 2
    public function validateIntrospectionRequest(ServerRequestInterface $request)
54
    {
55 2
        if ($request->getMethod() !== 'POST') {
56 1
            throw OAuthServerException::accessDenied('Invalid request method');
57
        }
58 1
    }
59
60
    /**
61
     * Return an introspection response.
62
     *
63
     * @param ServerRequestInterface $request
64
     * @param IntrospectionResponse  $responseType
65
     *
66
     * @return IntrospectionResponse
67
     */
68 2
    public function respondToIntrospectionRequest(
69
        ServerRequestInterface $request,
70
        IntrospectionResponse $responseType
71
    ) {
72 2
        $validator = $this->getIntrospectionValidator();
73
74 2
        if ($validator->validateIntrospection($request)) {
75 1
            $responseType->setRequest($request);
76 1
            $responseType->setValidity(true);
77
        }
78
79 2
        return $responseType;
80
    }
81
82
    /**
83
     * Get the introspection validator, falling back to the bearer token validator if not set.
84
     *
85
     * @return IntrospectionValidatorInterface
86
     */
87 2
    protected function getIntrospectionValidator()
88
    {
89 2
        if ($this->introspectionValidator instanceof IntrospectionValidatorInterface === false) {
90
            $this->introspectionValidator = new BearerTokenValidator($this->accessTokenRepository);
91
            $this->introspectionValidator->setPrivateKey($this->privateKey);
92
        }
93
94 2
        return $this->introspectionValidator;
95
    }
96
}
97