Completed
Push — master ( d9a404...9e6750 )
by Alexandre
02:12
created

ScopePolicyManager::verifyScopes()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 3
nop 2
dl 0
loc 8
rs 8.8571
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
     * @return array|null
38
     * @throws OAuthException
39
     */
40
    public function getDefaultScopes(ClientInterface $client): ?array
41
    {
42
        return $this->config->getScopePolicy()->getDefaultScopes($client);
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 ($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...
53
            $supportedScopes = explode(' ', $client->getMetadata()->getScope());
54
            if(empty($scopes) || !empty(array_diff($scopes, $supportedScopes))) {
55
                throw new OAuthException('invalid_scope',
56
                    'The request scope is invalid. Supported scopes : '.$client->getMetadata()->getScope(),
57
                    'https://tools.ietf.org/html/rfc6749#section-4.1');
58
            }
59
        }
60
    }
61
62
}