Failed Conditions
Push — master ( 349866...67c1d1 )
by Florent
10:56 queued 06:08
created

ScopeChecker::check()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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\SecurityBundle\Annotation\Checker;
15
16
use OAuth2Framework\SecurityBundle\Annotation\OAuth2;
17
use OAuth2Framework\SecurityBundle\Security\Authentication\Token\OAuth2Token;
18
19
final class ScopeChecker implements Checker
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function check(OAuth2Token $token, OAuth2 $configuration): void
25
    {
26
        $scope = $configuration->getScope();
27
        if (null === $scope) {
28
            return;
29
        }
30
31
        $scopes = explode(' ', $scope);
32
        $tokenScope = $token->getAccessToken()->hasParameter('scope') ? explode(' ', $token->getAccessToken()->getParameter('scope')) : [];
33
        $diff = array_diff($scopes, $tokenScope);
34
35
        if (!empty($diff)) {
36
            throw new \Exception(sprintf('Insufficient scope. The required scope is "%s"', $configuration->getScope()));
37
        }
38
    }
39
}
40