1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tequilarapido\RestrictAccess\Restricter; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
6
|
|
|
|
7
|
|
|
class BasicAuthRestricter extends Restricter |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Do we need to restric. |
11
|
|
|
*/ |
12
|
|
|
public function isRestrictionEnabled() |
13
|
|
|
{ |
14
|
|
|
return config('restrict_access.by_basic_auth.enabled'); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Protection method. |
19
|
|
|
* |
20
|
|
|
* @return Response|bool |
21
|
|
|
*/ |
22
|
|
|
public function restrict() |
23
|
|
|
{ |
24
|
|
|
if (! $this->isRestrictionEnabled()) { |
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (! $this->attempt()) { |
29
|
|
|
return $this->getBasicAuthenticationResponse(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Attempt login. |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
* |
40
|
|
|
* @throws \Exception |
41
|
|
|
*/ |
42
|
|
|
protected function attempt() |
43
|
|
|
{ |
44
|
|
|
$username = config('restrict_access.by_basic_auth.username'); |
45
|
|
|
$password = config('restrict_access.by_basic_auth.password'); |
46
|
|
|
|
47
|
|
|
if (empty($username) || empty($password)) { |
48
|
|
|
throw new \Exception('restrict_access.by_basic_auth username and password are required'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->ensurePhpFpmCompatibility(); |
52
|
|
|
|
53
|
|
|
return $username === $this->request->getUser() && $password === $this->request->getPassword(); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns www-authenticate header. |
58
|
|
|
* |
59
|
|
|
* @return Response |
60
|
|
|
*/ |
61
|
|
|
protected function getBasicAuthenticationResponse() |
62
|
|
|
{ |
63
|
|
|
$headers = ['WWW-Authenticate' => 'Basic realm="Authentication System"']; |
64
|
|
|
|
65
|
|
|
return new Response('Invalid credentials.', 401, $headers); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* PHP FPM / FastCGI Compatibility |
70
|
|
|
* use custom headers. |
71
|
|
|
*/ |
72
|
|
|
protected function ensurePhpFpmCompatibility() |
73
|
|
|
{ |
74
|
|
|
$this->tryToSetAuthPHPFrom('Authorization'); |
75
|
|
|
$this->tryToSetAuthPHPFrom('REDIRECT_HTTP_AUTHORIZATION'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set headers. |
80
|
|
|
* |
81
|
|
|
* @param $header |
82
|
|
|
*/ |
83
|
|
|
protected function tryToSetAuthPHPFrom($header) |
84
|
|
|
{ |
85
|
|
|
if ( |
86
|
|
|
! empty($_SERVER[$header]) |
87
|
|
|
&& preg_match('/Basic\s+(.*)$/i', $_SERVER[$header], $auth_matches) |
88
|
|
|
) { |
89
|
|
|
$auth_items = isset($auth_matches[1]) ? explode(':', base64_decode($auth_matches[1])) : []; |
90
|
|
|
if (isset($auth_items[0]) && isset($auth_items[1])) { |
91
|
|
|
$_SERVER['PHP_AUTH_USER'] = strip_tags($auth_items[0]); |
92
|
|
|
$_SERVER['PHP_AUTH_PW'] = strip_tags($auth_items[1]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: