Failed Conditions
Pull Request — master (#31)
by Florent
03:43
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
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
20
21
final class ScopeChecker implements CheckerInterface
22
{
23
    /**
24
     * @var ScopeRepositoryInterface
25
     */
26
    private $scopeRepository;
27
28
    /**
29
     * ScopeChecker constructor.
30
     *
31
     * @param ScopeRepositoryInterface $scopeRepository
32
     */
33
    public function __construct(ScopeRepositoryInterface $scopeRepository)
34
    {
35
        $this->scopeRepository = $scopeRepository;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function check(OAuth2Token $token, OAuth2 $configuration): ?string
42
    {
43
        $scope = $configuration->getScope();
44
        if (null === $scope) {
45
            return null;
46
        }
47
48
        $scopes = explode(' ', $scope);
49
        $diff = array_diff($scopes, $token->getAccessToken()->getScopes());
50
51
        if (!empty($diff)) {
52
            return sprintf('Insufficient scope. The scope rule is: %s', $configuration->getScope());
53
        }
54
55
        return  null;
56
    }
57
}
58