1 | <?php |
||
2 | |||
3 | namespace Raptor\Request\Components; |
||
4 | |||
5 | class Authorization |
||
6 | { |
||
7 | /** |
||
8 | * Authorization type. |
||
9 | * |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $type; |
||
13 | |||
14 | /** |
||
15 | * Authorization username. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $username; |
||
20 | |||
21 | /** |
||
22 | * Authorization password. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $password; |
||
27 | |||
28 | /** |
||
29 | * Authorization token. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $token; |
||
34 | |||
35 | /** |
||
36 | * Authorization header-field. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $field; |
||
41 | |||
42 | /** |
||
43 | * Get authorization type. |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | public function type() |
||
48 | { |
||
49 | if ($this->type !== null) { |
||
50 | return $this->type; |
||
51 | } |
||
52 | if ($this->field() === false || count($string = explode(' ', $this->field)) < 2) { |
||
53 | return $this->type = false; |
||
0 ignored issues
–
show
|
|||
54 | } |
||
55 | return $this->type = $string[0]; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Get authorization username. |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public function username() |
||
64 | { |
||
65 | if ($this->username !== null) { |
||
66 | return $this->username; |
||
67 | } |
||
68 | |||
69 | if (isset($_SERVER['PHP_AUTH_USER'])) { |
||
70 | return $this->username = $_SERVER['PHP_AUTH_USER']; |
||
71 | } |
||
72 | |||
73 | if (strtolower($this->type()) == 'basic') { |
||
74 | // Decode AUTHORIZATION header into PHP_AUTH_USER when authorization header is basic |
||
75 | $token = explode(':', base64_decode($this->token()), 1); |
||
76 | if (count($token) == 1) { |
||
77 | return $this->username = $_SERVER['PHP_AUTH_USER'] = $token[0]; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return $this->username = false; |
||
0 ignored issues
–
show
The property
$username was declared of type string , but false is of type false . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
![]() |
|||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Get authorization password. |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | public function password() |
||
90 | { |
||
91 | if ($this->password !== null) { |
||
92 | $this->password; |
||
93 | } |
||
94 | |||
95 | if (isset($_SERVER['PHP_AUTH_USER'])) { |
||
96 | return $this->password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : ''; |
||
97 | } |
||
98 | |||
99 | if (strtolower($this->type()) == 'basic') { |
||
100 | // Decode AUTHORIZATION header into PHP_AUTH_PW when authorization header is basic |
||
101 | $token = explode(':', base64_decode($this->token()), 2); |
||
102 | if (count($token) == 2) { |
||
103 | return $this->password = $_SERVER['PHP_AUTH_PW'] = $token[1]; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | return $this->password = false; |
||
0 ignored issues
–
show
The property
$password was declared of type string , but false is of type false . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
![]() |
|||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get authorization token. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | public function token() |
||
116 | { |
||
117 | if ($this->token !== null) { |
||
118 | return $this->token; |
||
119 | } |
||
120 | if ($this->field() === false || count($string = explode(' ', $this->field)) != 2) { |
||
121 | return $this->token = false; |
||
0 ignored issues
–
show
The property
$token was declared of type string , but false is of type false . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
![]() |
|||
122 | } |
||
123 | return $this->token = $string[1]; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Get authorization header-field. |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | public function field() |
||
132 | { |
||
133 | if ($this->field !== null) { |
||
134 | return $this->field; |
||
135 | } |
||
136 | |||
137 | // HTTP_AUTHORIZATION |
||
138 | if (isset($_SERVER['HTTP_AUTHORIZATION'])) { |
||
139 | $this->field = $_SERVER['HTTP_AUTHORIZATION']; |
||
140 | } |
||
141 | // REDIRECT_HTTP_AUTHORIZATION |
||
142 | elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) { |
||
143 | $this->field = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; |
||
144 | } |
||
145 | if ($this->field) { |
||
146 | if (empty($_SERVER['PHP_AUTH_DIGEST']) && (0 === stripos($this->field, 'digest '))) { |
||
147 | // In some circumstances PHP_AUTH_DIGEST needs to be set. |
||
148 | return $_SERVER['PHP_AUTH_DIGEST'] = $this->field; |
||
149 | } |
||
150 | } |
||
151 | // PHP_AUTH_USER |
||
152 | if (isset($_SERVER['PHP_AUTH_USER'])) { |
||
153 | return $this->field = 'Basic '.base64_encode($_SERVER['PHP_AUTH_USER'].':'.$_SERVER['PHP_AUTH_PW']); |
||
154 | } |
||
155 | // PHP_AUTH_DIGEST |
||
156 | if (isset($_SERVER['PHP_AUTH_DIGEST'])) { |
||
157 | return $this->field = $_SERVER['PHP_AUTH_DIGEST']; |
||
158 | } |
||
159 | |||
160 | return $this->field = false; |
||
0 ignored issues
–
show
The property
$field was declared of type string , but false is of type false . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
![]() |
|||
161 | } |
||
162 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.