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