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\Support\Str; |
15
|
|
|
use Illuminate\Http\Request; |
16
|
|
|
use Illuminate\Contracts\Http\Parser as ParserContract; |
17
|
|
|
|
18
|
|
|
class AuthHeaders implements ParserContract |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The header name |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $header = 'authorization'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The header prefix |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $prefix = 'bearer'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Attempt to parse the token from some other possible headers |
37
|
|
|
* |
38
|
|
|
* @return null|string |
39
|
|
|
*/ |
40
|
|
|
protected function fromAltHeaders(Request $request) |
41
|
|
|
{ |
42
|
|
|
return $request->server->get('HTTP_AUTHORIZATION', |
43
|
|
|
$request->server->get('REDIRECT_HTTP_AUTHORIZATION') |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Try to parse the token from the request header |
49
|
|
|
* |
50
|
|
|
* @param \Illuminate\Http\Request $request |
51
|
|
|
* |
52
|
|
|
* @return null|string |
53
|
|
|
*/ |
54
|
|
|
public function parse(Request $request) |
55
|
|
|
{ |
56
|
|
|
$header = $request->headers->get($this->header, $this->fromAltHeaders($request)); |
57
|
|
|
|
58
|
|
|
if (! $header || ! Str::startsWith(strtolower($header), $this->prefix)) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return trim(str_ireplace($this->prefix, '', $header)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the header name |
67
|
|
|
* |
68
|
|
|
* @param string $headerName |
69
|
|
|
* |
70
|
|
|
* @return TokenParser |
71
|
|
|
*/ |
72
|
|
|
public function setHeaderName($headerName) |
73
|
|
|
{ |
74
|
|
|
$this->header = $headerName; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the header prefix |
81
|
|
|
* |
82
|
|
|
* @param string $headerPrefix |
83
|
|
|
* |
84
|
|
|
* @return TokenParser |
85
|
|
|
*/ |
86
|
|
|
public function setHeaderPrefix($headerPrefix) |
87
|
|
|
{ |
88
|
|
|
$this->prefix = $headerPrefix; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|