|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Canis.io |
|
4
|
|
|
* @license MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Canis\Lumen\Jwt; |
|
8
|
|
|
|
|
9
|
|
|
class Token |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private $claims; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $tokenString; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor |
|
22
|
|
|
* @param string $tokenString |
|
23
|
|
|
* @param array $claims |
|
24
|
|
|
*/ |
|
25
|
22 |
|
public function __construct($tokenString, $claims = []) |
|
26
|
|
|
{ |
|
27
|
22 |
|
$this->tokenString = $tokenString; |
|
28
|
22 |
|
$this->claims = $claims; |
|
29
|
22 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get a specific claim |
|
33
|
|
|
* @param string $claim Name of claim |
|
34
|
|
|
* @return mixed Null on not found |
|
35
|
|
|
*/ |
|
36
|
10 |
|
public function getClaim($claim) |
|
37
|
|
|
{ |
|
38
|
10 |
|
if (isset($this->claims[$claim])) { |
|
39
|
10 |
|
return $this->claims[$claim]; |
|
40
|
|
|
} |
|
41
|
1 |
|
return null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Check for a specific claim |
|
47
|
|
|
* @param string $claim Name of claim |
|
48
|
|
|
* @return mixed Null on not found |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function hasClaim($claim) |
|
51
|
|
|
{ |
|
52
|
4 |
|
return array_key_exists($claim, $this->claims); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get all claims |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function getClaims() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->claims; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get token string |
|
67
|
|
|
* @return string String variant of token |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function getTokenString() |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->tokenString; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Convert object to string |
|
76
|
|
|
* @return string String variant of token |
|
77
|
|
|
*/ |
|
78
|
14 |
|
public function __toString() |
|
79
|
|
|
{ |
|
80
|
14 |
|
return $this->tokenString; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Validate the claims of the token |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $validateClaims |
|
88
|
|
|
* @return boolean |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public function ensureClaimValues(array $validateClaims) |
|
91
|
|
|
{ |
|
92
|
4 |
|
foreach ($validateClaims as $claim => $value) { |
|
93
|
4 |
|
if (!isset($this->claims[$claim]) || $this->claims[$claim] !== $value) { |
|
94
|
1 |
|
return false; |
|
95
|
|
|
} |
|
96
|
3 |
|
} |
|
97
|
3 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|