Failed Conditions
Push — ng ( ca946d...fcb055 )
by Florent
10:43
created

AuthenticationMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 16 4
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\TokenIntrospectionEndpoint;
15
16
use Psr\Http\Server\RequestHandlerInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerRepository;
19
use OAuth2Framework\Component\Core\Exception\OAuth2Exception;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
23
class AuthenticationMiddleware implements MiddlewareInterface
24
{
25
    /**
26
     * @var ResourceServerRepository
27
     */
28
    private $resourceServerRepository;
29
30
    /**
31
     * @var ResourceServerAuthenticationMethodManager
32
     */
33
    private $resourceServerAuthenticationMethodManager;
34
35
    /**
36
     * ResourceServerAuthenticationMiddleware constructor.
37
     *
38
     * @param ResourceServerRepository                  $resourceServerRepository
39
     * @param ResourceServerAuthenticationMethodManager $resourceServerAuthenticationMethodManager
40
     */
41
    public function __construct(ResourceServerRepository $resourceServerRepository, ResourceServerAuthenticationMethodManager $resourceServerAuthenticationMethodManager)
42
    {
43
        $this->resourceServerRepository = $resourceServerRepository;
44
        $this->resourceServerAuthenticationMethodManager = $resourceServerAuthenticationMethodManager;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
51
    {
52
        $resourceServerId = $this->resourceServerAuthenticationMethodManager->findResourceServerInformationInTheRequest($request, $authenticationMethod, $resourceServerCredentials);
0 ignored issues
show
Bug introduced by
The variable $authenticationMethod does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $resourceServerCredentials does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
53
        if (null === $resourceServerId) {
54
            throw new OAuth2Exception(401, OAuth2Exception::ERROR_INVALID_RESOURCE_SERVER, 'Resource Server authentication failed.');
55
        }
56
        $resourceServer = $this->resourceServerRepository->find($resourceServerId);
57
58
        if (null === $resourceServer || false === $this->resourceServerAuthenticationMethodManager->isResourceServerAuthenticated($request, $resourceServer, $authenticationMethod, $resourceServerCredentials)) {
59
            throw new OAuth2Exception(401, OAuth2Exception::ERROR_INVALID_RESOURCE_SERVER, 'Resource Server authentication failed.');
60
        }
61
62
        $request = $request->withAttribute('resource_server', $resourceServer);
63
64
        return $handler->handle($request);
65
    }
66
}
67