Failed Conditions
Push — master ( 2eee97...42d85f )
by Florent
09:02
created

AudienceChecker::checkHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Checker;
15
16
use Jose\Component\Checker\ClaimCheckerInterface;
17
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
use Symfony\Component\Routing\RouterInterface;
19
20
final class AudienceChecker implements ClaimCheckerInterface
21
{
22
    private const CLAIM_NAME = 'aud';
23
24
    /**
25
     * @var bool
26
     */
27
    private $protectedHeader = false;
28
    /**
29
     * @var string
30
     */
31
    private $audience;
32
33
    /**
34
     * AudienceChecker constructor.
35
     *
36
     * @param RouterInterface $router
37
     * @param string          $routeName
38
     * @param array           $routeParameters
39
     */
40
    public function __construct(RouterInterface $router, string $routeName, array $routeParameters)
41
    {
42
        $this->audience = $router->generate($routeName, $routeParameters, UrlGeneratorInterface::ABSOLUTE_URL);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function checkClaim($value)
49
    {
50
        return $this->checkValue($value);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function checkHeader($value)
57
    {
58
        return $this->checkValue($value);
59
    }
60
61
    /**
62
     * @param $value
63
     *
64
     * @throws \InvalidArgumentException
65
     */
66
    private function checkValue($value)
67
    {
68
        if (is_string($value) && $value !== $this->audience) {
69
            throw new \InvalidArgumentException('Bad audience.');
70
        } elseif (!is_array($value) || !in_array($this->audience, $value)) {
71
            throw new \InvalidArgumentException('Bad audience.');
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function supportedClaim(): string
79
    {
80
        return self::CLAIM_NAME;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function supportedHeader(): string
87
    {
88
        return self::CLAIM_NAME;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function protectedHeaderOnly(): bool
95
    {
96
        return $this->protectedHeader;
97
    }
98
}
99