1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Validator; |
4
|
|
|
|
5
|
|
|
use Gandung\JWT\ValidatorInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class Validator implements ValidatorInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $constraint = []; |
16
|
|
|
|
17
|
|
|
public function addConstraint(Constraints\ConstraintInterface $constraint) |
18
|
|
|
{ |
19
|
|
|
$this->constraint[] = $constraint; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function validate($value) |
26
|
|
|
{ |
27
|
|
|
$q = explode('.', $value); |
28
|
|
|
|
29
|
|
|
if (sizeof($q) !== 3) { |
30
|
|
|
throw new \InvalidArgumentException( |
31
|
|
|
"Supply valid JWT token." |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$m = $this->mergeUnserializedPortion( |
36
|
|
|
$this->unserializePortion($q[0]), |
37
|
|
|
$this->unserializePortion($q[1]) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$this->normalizeConstraints(); |
41
|
|
|
|
42
|
|
|
foreach ($this->constraint as $c) { |
43
|
|
|
if (!array_key_exists($c, $m)) { |
44
|
|
|
throw new \RuntimeException( |
45
|
|
|
sprintf("Supplied JWT Token doesn't have '%s' constraint.", $c) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Validate constraints from given array. |
55
|
|
|
* |
56
|
|
|
* @param array $array |
57
|
|
|
* @param null|object $key |
58
|
|
|
* @return boolean |
59
|
|
|
* @throws \RuntimeException when supplied array doesn't matched given constraint. |
60
|
|
|
*/ |
61
|
|
|
public function validateFromArray($array, $key = null) |
62
|
|
|
{ |
63
|
|
|
if (is_null($key)) { |
64
|
|
|
$this->normalizeConstraints(); |
65
|
|
|
|
66
|
|
|
foreach ($this->constraint as $c) { |
67
|
|
|
if (!array_key_exists($c, $array)) { |
68
|
|
|
throw new \RuntimeException( |
69
|
|
|
sprintf("Supplied data doesn't have '%s' constraint.", $c) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} else { |
74
|
|
|
if (!array_key_exists($key->getValue(), $array)) { |
75
|
|
|
throw new \RuntimeException( |
76
|
|
|
sprintf("Supplied data doesn't have '%s' constraint.", $key->getValue()) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function unserializePortion($portion) |
85
|
|
|
{ |
86
|
|
|
return json_decode( |
87
|
|
|
\Gandung\JWT\__jwt_base64UrlDecode($portion), |
88
|
|
|
\JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function mergeUnserializedPortion() |
93
|
|
|
{ |
94
|
|
|
$res = []; |
95
|
|
|
|
96
|
|
|
foreach (func_get_args() as $v) { |
97
|
|
|
$res = array_merge_recursive($res, $v); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $res; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function normalizeConstraints() |
104
|
|
|
{ |
105
|
|
|
$this->constraint = array_map(function ($q) { |
106
|
|
|
return $q->getValue(); |
107
|
|
|
}, $this->constraint); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|