Failed Conditions
Push — master ( 516359...a8b39a )
by Florent
04:00
created

ResourceServerAuthMethodByIpAddress::isResourceServerAuthenticated()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 5
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Server\Tests\Stub;
15
16
use OAuth2Framework\Component\Server\Model\ResourceServer\ResourceServerId;
17
use OAuth2Framework\Component\Server\Model\ResourceServer\ResourceServerInterface;
18
use OAuth2Framework\Component\Server\TokenIntrospectionEndpointAuthMethod\TokenIntrospectionEndpointAuthMethodInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
21
final class ResourceServerAuthMethodByIpAddress implements TokenIntrospectionEndpointAuthMethodInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function findResourceServerId(ServerRequestInterface $request, &$resourceServerCredentials = null): ?ResourceServerId
27
    {
28
        if ($request->hasHeader('X-Resource-Server-Id')) {
29
            $id = $request->getHeader('X-Resource-Server-Id');
30
            if (1 === count($id)) {
31
                return ResourceServerId::create($id[0]);
32
            }
33
        }
34
35
        return null;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function isResourceServerAuthenticated(ResourceServerInterface $resourceServer, $resourceServerCredentials, ServerRequestInterface $request): bool
42
    {
43
        if (!$resourceServer instanceof ResourceServer) {
44
            return false;
45
        }
46
        if ($resourceServer->isDeleted()) {
47
            return false;
48
        }
49
        if (!$resourceServer->has('ips') || !is_array($resourceServer->get('ips')) || empty($resourceServer->get('ips'))) {
50
            return false;
51
        }
52
        $params = $request->getServerParams();
53
        if (!array_key_exists('REMOTE_ADDR', $params)) {
54
            return false;
55
        }
56
57
        return in_array($params['REMOTE_ADDR'], $resourceServer->get('ips'));
58
    }
59
}
60