Failed Conditions
Pull Request — master (#31)
by Florent
07:03 queued 03:28
created

ScopeChecker::getExpressionLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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\Bundle\Server\Annotation\Checker;
15
16
use OAuth2Framework\Bundle\Server\Annotation\OAuth2;
17
use OAuth2Framework\Bundle\Server\Security\Authentication\Token\OAuth2Token;
18
use OAuth2Framework\Component\Server\Model\Scope\ScopeRepositoryInterface;
19
20
final class ScopeChecker implements CheckerInterface
21
{
22
    /**
23
     * @var ScopeRepositoryInterface
24
     */
25
    private $scopeRepository;
26
27
    /**
28
     * ScopeChecker constructor.
29
     *
30
     * @param ScopeRepositoryInterface $scopeRepository
31
     */
32
    public function __construct(ScopeRepositoryInterface $scopeRepository)
33
    {
34
        $this->scopeRepository = $scopeRepository;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function check(OAuth2Token $token, OAuth2 $configuration): ?string
41
    {
42
        $scope = $configuration->getScope();
43
        if (null === $scope) {
44
            return null;
45
        }
46
47
        $scopes = explode(' ', $scope);
48
        $diff = array_diff($scopes, $token->getAccessToken()->getScopes());
49
50
        if (!empty($diff)) {
51
            return sprintf('Insufficient scope. The scope rule is: %s', $configuration->getScope());
52
        }
53
54
        return  null;
55
    }
56
}
57