Failed Conditions
Push — master ( 819484...23fc45 )
by Florent
03:33
created

AuthenticationMiddleware::checkResourceServer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\ResourceServerAuthentication;
15
16
use Assert\Assertion;
17
use OAuth2Framework\Component\Core\Message\OAuth2Error;
18
use OAuth2Framework\Component\Core\ResourceServer\ResourceServer;
19
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerRepository;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\MiddlewareInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
25
final class AuthenticationMiddleware implements MiddlewareInterface
26
{
27
    /**
28
     * @var AuthenticationMethodManager
29
     */
30
    private $authenticationMethodManager;
31
32
    /**
33
     * @var ResourceServerRepository
34
     */
35
    private $resourceServerRepository;
36
37
    public function __construct(ResourceServerRepository $resourceServerRepository, AuthenticationMethodManager $authenticationMethodManager)
38
    {
39
        $this->resourceServerRepository = $resourceServerRepository;
40
        $this->authenticationMethodManager = $authenticationMethodManager;
41
    }
42
43
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
44
    {
45
        try {
46
            $authentication_method = null;
47
            $resourceServer_credentials = null;
48
            $resourceServerId = $this->authenticationMethodManager->findResourceServerIdAndCredentials($request, $authentication_method, $resourceServer_credentials);
49
            if (null !== $resourceServerId && $authentication_method instanceof AuthenticationMethod) {
50
                $resourceServer = $this->resourceServerRepository->find($resourceServerId);
51
                Assertion::notNull($resourceServer, 'Unknown resource server or resource server not authenticated.');
52
                $this->checkAuthenticationMethod($request, $resourceServer, $authentication_method, $resourceServer_credentials);
53
                $request = $request->withAttribute('resource_server', $resourceServer);
54
                $request = $request->withAttribute('resource_server_authentication_method', $authentication_method);
55
                $request = $request->withAttribute('resource_server_credentials', $resourceServer_credentials);
56
            }
57
        } catch (\Throwable $e) {
58
            throw new OAuth2Error(401, OAuth2Error::ERROR_INVALID_RESOURCE_SERVER, $e->getMessage(), [], $e);
59
        }
60
61
        return $handler->handle($request);
62
    }
63
64
    /**
65
     * @param mixed $resourceServerCredentials
66
     */
67
    private function checkAuthenticationMethod(ServerRequestInterface $request, ResourceServer $resourceServer, AuthenticationMethod $authenticationMethod, $resourceServerCredentials): void
68
    {
69
        if (!\in_array($resourceServer->getAuthenticationMethod(), $authenticationMethod->getSupportedMethods(), true)) {
70
            throw new \InvalidArgumentException('Unknown resource server or resource server not authenticated.');
71
        }
72
        if (!$authenticationMethod->isResourceServerAuthenticated($resourceServer, $resourceServerCredentials, $request)) {
73
            throw new \InvalidArgumentException('Unknown resource server or resource server not authenticated.');
74
        }
75
    }
76
}
77