Completed
Push — master ( 64cd9f...411f65 )
by Alexandre
02:13
created

ScopePolicyManager::getScopes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: GCC-MED
5
 * Date: 15/01/2018
6
 * Time: 09:54
7
 */
8
9
namespace OAuth2\ScopePolicy;
10
11
12
use OAuth2\Config;
13
use OAuth2\Exceptions\OAuthException;
14
use OAuth2\Roles\ClientInterface;
15
use OAuth2\Roles\Clients\RegisteredClient;
16
17
18
class ScopePolicyManager
19
{
20
    /**
21
     * @var Config
22
     */
23
    private $config;
24
25
    /**
26
     * ScopePolicyManager constructor.
27
     * @param Config $config
28
     * @throws \Exception
29
     */
30
    public function __construct(Config $config)
31
    {
32
        $this->config = $config;
33
    }
34
35
    /**
36
     * @param ClientInterface $client
37
     * @param string|null $scope
38
     * @return array|null
39
     */
40
    public function getScopes(ClientInterface $client, ?string $scope): array
41
    {
42
        return $this->config->getScopePolicy()->getScopes($client, $scope);
43
    }
44
45
    /**
46
     * @param ClientInterface $client
47
     * @param array $scopes
48
     * @throws OAuthException
49
     */
50
    public function verifyScopes(ClientInterface $client, array $scopes): void
51
    {
52
        if(empty($scopes)) {
53
            throw new OAuthException('invalid_scope',
54
                'The request scope is unknown.',
55
                'https://tools.ietf.org/html/rfc6749#section-4.1');
56
        }
57
58
        if ($client instanceof RegisteredClient && is_array($client->getMetadata()->getScope())) {
0 ignored issues
show
introduced by
The condition $client instanceof OAuth...Metadata()->getScope()) can never be true.
Loading history...
59
            $supportedScopes = explode(' ', $client->getMetadata()->getScope());
60
            if(!empty(array_diff($scopes, $supportedScopes))) {
61
                throw new OAuthException('invalid_scope',
62
                    'The request scope is invalid. Supported scopes : '.$client->getMetadata()->getScope(),
63
                    'https://tools.ietf.org/html/rfc6749#section-4.1');
64
            }
65
        }
66
    }
67
68
}