|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of jwt-auth |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sean Tymon <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tymon\JWTAuth\Http; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
|
|
16
|
|
|
class Parser |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $chain; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Illuminate\Http\Request |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $request; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param \Illuminate\Http\Request $request |
|
30
|
|
|
* @param array $chain |
|
31
|
|
|
*/ |
|
32
|
18 |
|
public function __construct(Request $request, array $chain = []) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
18 |
|
$this->request = $request; |
|
35
|
18 |
|
$this->chain = $chain; |
|
36
|
18 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get the parser chain |
|
40
|
|
|
* |
|
41
|
|
|
* @return array The chain of ParserContracts that the parser evaluates. |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function getChain() |
|
44
|
|
|
{ |
|
45
|
2 |
|
return $this->chain; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Set the order of the parser chain |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $chain |
|
52
|
|
|
*/ |
|
53
|
16 |
|
public function setChain(array $chain) |
|
54
|
|
|
{ |
|
55
|
16 |
|
$this->chain = $chain; |
|
56
|
|
|
|
|
57
|
16 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Alias for setting the order of the chain |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $chain |
|
64
|
|
|
*/ |
|
65
|
14 |
|
public function setChainOrder(array $chain) |
|
66
|
|
|
{ |
|
67
|
14 |
|
$this->setChain($chain); |
|
68
|
|
|
|
|
69
|
14 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Iterate throught the parsers and attempt to retrieve |
|
74
|
|
|
* a value, otherwise return null |
|
75
|
|
|
* |
|
76
|
|
|
* @return string|null |
|
77
|
|
|
*/ |
|
78
|
16 |
|
public function parseToken() |
|
79
|
|
|
{ |
|
80
|
16 |
|
foreach ($this->chain as $parser) { |
|
81
|
16 |
|
$response = $parser->parse($this->request); |
|
82
|
|
|
|
|
83
|
16 |
|
if ($response !== null) { |
|
84
|
10 |
|
return $response; |
|
85
|
|
|
} |
|
86
|
14 |
|
} |
|
87
|
|
|
|
|
88
|
6 |
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check whether a token exists in the chain |
|
93
|
|
|
* |
|
94
|
|
|
* @return boolean |
|
95
|
|
|
*/ |
|
96
|
16 |
|
public function hasToken() |
|
97
|
|
|
{ |
|
98
|
16 |
|
return $this->parseToken() !== null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Set the request instance. |
|
103
|
|
|
* |
|
104
|
|
|
* @param \Illuminate\Http\Request $request |
|
105
|
|
|
* |
|
106
|
|
|
* @return Parser |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function setRequest(Request $request) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
2 |
|
$this->request = $request; |
|
111
|
|
|
|
|
112
|
2 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|