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 TokenParser |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Illuminate\Http\Request |
20
|
|
|
*/ |
21
|
|
|
protected $request; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The header name |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $header = 'authorization'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The header prefix |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $prefix = 'bearer'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The query string key |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $query = 'token'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \Illuminate\Http\Request $request |
46
|
|
|
*/ |
47
|
10 |
|
public function __construct(Request $request) |
48
|
|
|
{ |
49
|
10 |
|
$this->request = $request; |
50
|
10 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Attempt to parse the token from some other possible headers |
54
|
|
|
* |
55
|
|
|
* @return false|string |
56
|
|
|
*/ |
57
|
10 |
|
protected function fromAltHeaders() |
58
|
|
|
{ |
59
|
10 |
|
return $this->request->server->get('HTTP_AUTHORIZATION', |
60
|
10 |
|
$this->request->server->get('REDIRECT_HTTP_AUTHORIZATION', false) |
61
|
10 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Try to parse the token from the request header |
66
|
|
|
* |
67
|
|
|
* @return false|string |
68
|
|
|
*/ |
69
|
10 |
|
public function fromHeader() |
70
|
|
|
{ |
71
|
10 |
|
$header = $this->request->headers->get($this->header, $this->fromAltHeaders()); |
72
|
|
|
|
73
|
10 |
|
if (! $header || ! starts_with(strtolower($header), $this->prefix)) { |
74
|
6 |
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
return trim(str_ireplace($this->prefix, '', $header)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Try to get the token from the route |
82
|
|
|
* |
83
|
|
|
* @return false|string |
84
|
|
|
*/ |
85
|
6 |
|
public function fromRoute() |
86
|
|
|
{ |
87
|
6 |
|
return $this->request->route()->parameter($this->query, false); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Try to parse the token from the request query string |
92
|
|
|
* |
93
|
|
|
* @return false|string |
94
|
|
|
*/ |
95
|
6 |
|
public function fromQueryString() |
96
|
|
|
{ |
97
|
6 |
|
return $this->request->input($this->query, $this->fromRoute()); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Try to parse the token from either the header or query string |
102
|
|
|
* |
103
|
|
|
* @return false|string |
104
|
|
|
*/ |
105
|
10 |
|
public function parseToken() |
106
|
|
|
{ |
107
|
10 |
|
return $this->fromHeader() ?: $this->fromQueryString(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check whether a token exists in the request |
112
|
|
|
* |
113
|
|
|
* @return boolean |
114
|
|
|
*/ |
115
|
10 |
|
public function hasToken() |
116
|
|
|
{ |
117
|
10 |
|
return $this->parseToken() !== false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the request instance. |
122
|
|
|
* |
123
|
|
|
* @param \Illuminate\Http\Request $request |
124
|
|
|
* |
125
|
|
|
* @return TokenParser |
126
|
|
|
*/ |
127
|
2 |
|
public function setRequest(Request $request) |
128
|
|
|
{ |
129
|
2 |
|
$this->request = $request; |
130
|
|
|
|
131
|
2 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set the header name |
136
|
|
|
* |
137
|
|
|
* @param string $headerName |
138
|
|
|
* |
139
|
|
|
* @return TokenParser |
140
|
|
|
*/ |
141
|
|
|
public function setHeaderName($headerName) |
142
|
|
|
{ |
143
|
|
|
$this->header = $headerName; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set the header prefix |
150
|
|
|
* |
151
|
|
|
* @param string $headerPrefix |
152
|
|
|
* |
153
|
|
|
* @return TokenParser |
154
|
|
|
*/ |
155
|
|
|
public function setHeaderPrefix($headerPrefix) |
156
|
|
|
{ |
157
|
|
|
$this->prefix = $headerPrefix; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the query string |
164
|
|
|
* |
165
|
|
|
* @param string $queryString |
166
|
|
|
* |
167
|
|
|
* @return TokenParser |
168
|
|
|
*/ |
169
|
|
|
public function setQueryString($queryString) |
170
|
|
|
{ |
171
|
|
|
$this->query = $queryString; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|