| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\HttpAuth; |
| 4 |
|
| 5 |
use Intervention\HttpAuth\Configurator\ArrayConfigurator; |
| 6 |
|
| 7 |
abstract class AbstractVault |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Environment |
| 11 |
* |
| 12 |
* @var Environment |
| 13 |
*/ |
| 14 |
protected $environment; |
| 15 |
|
| 16 |
/** |
| 17 |
* Name of realm for vault |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $realm; |
| 22 |
|
| 23 |
/** |
| 24 |
* Username for vault |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $username; |
| 28 |
|
| 29 |
/** |
| 30 |
* Password for vault |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $password; |
| 35 |
|
| 36 |
/** |
| 37 |
* Build directive for current vault |
| 38 |
* |
| 39 |
* @return Directive |
| 40 |
*/ |
| 41 |
abstract public function getDirective(): Directive; |
| 42 |
|
| 43 |
/** |
| 44 |
* Determine if vault is accessible by given key |
| 45 |
* |
| 46 |
* @param Key $key |
| 47 |
* @return bool |
| 48 |
*/ |
| 49 |
abstract public function unlocksWithKey(Key $key): bool; |
| 50 |
|
| 51 |
/** |
| 52 |
* Create new instance |
| 53 |
* |
| 54 |
* @param mixed $realm |
| 55 |
* @param mixed $username |
| 56 |
* @param mixed $password |
| 57 |
*/ |
| 58 |
public function __construct($realm, $username, $password) |
| 59 |
{ |
| 60 |
$this->checkParameterValidity([ |
| 61 |
'realm' => $realm, |
| 62 |
'username' => $username, |
| 63 |
'password' => $password, |
| 64 |
]); |
| 65 |
|
| 66 |
$this->environment = new Environment(); |
| 67 |
|
| 68 |
$this->realm = $realm; |
| 69 |
$this->username = $username; |
| 70 |
$this->password = $password; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Throw exception if any of the given parameters are empty |
| 75 |
* |
| 76 |
* @param array $parameters |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
private function checkParameterValidity(array $parameters): void |
| 80 |
{ |
| 81 |
foreach ($parameters as $key => $value) { |
| 82 |
if (empty($value)) { |
| 83 |
throw new Exception\InvalidParameterException( |
| 84 |
'Cannot create HTTP authentication vault. Parameter "' . $key . '" cannot be empty.' |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Return key from current token |
| 92 |
* |
| 93 |
* @return Key |
| 94 |
*/ |
| 95 |
public function getKey(): Key |
| 96 |
{ |
| 97 |
return $this->environment->getToken()->toKey(); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Denies access for non-authenticated users |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function secure(): void |
| 106 |
{ |
| 107 |
if (! $this->unlocksWithKey($this->getKey())) { |
| 108 |
$this->denyAccess(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Set name of realm |
| 114 |
* |
| 115 |
* @param string $realm |
| 116 |
* @return AbstractVault |
| 117 |
*/ |
| 118 |
public function setRealm($realm): AbstractVault |
| 119 |
{ |
| 120 |
$this->realm = $realm; |
| 121 |
|
| 122 |
return $this; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Alias for setRealm() |
| 127 |
* |
| 128 |
* @param string $realm |
| 129 |
* @return AbstractVault |
| 130 |
*/ |
| 131 |
public function realm($realm): AbstractVault |
| 132 |
{ |
| 133 |
return $this->setRealm($realm); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Return current realm name |
| 138 |
* |
| 139 |
* @return string |
| 140 |
*/ |
| 141 |
public function getRealm() |
| 142 |
{ |
| 143 |
return $this->realm; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Set username for current vault |
| 148 |
* |
| 149 |
* @param string $username |
| 150 |
*/ |
| 151 |
public function setUsername($username): AbstractVault |
| 152 |
{ |
| 153 |
$this->username = $username; |
| 154 |
|
| 155 |
return $this; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Alias for setUsername() |
| 160 |
* |
| 161 |
* @param string $username |
| 162 |
*/ |
| 163 |
public function username($username): AbstractVault |
| 164 |
{ |
| 165 |
return $this->setUsername($username); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Return current username |
| 170 |
* |
| 171 |
* @return string |
| 172 |
*/ |
| 173 |
public function getUsername() |
| 174 |
{ |
| 175 |
return $this->username; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Set password for current vault |
| 180 |
* |
| 181 |
* @param string $password |
| 182 |
* @return AbstractVault |
| 183 |
*/ |
| 184 |
public function setPassword($password): AbstractVault |
| 185 |
{ |
| 186 |
$this->password = $password; |
| 187 |
|
| 188 |
return $this; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Alias for setPassword() |
| 193 |
* |
| 194 |
* @param string $password |
| 195 |
* @return AbstractVault |
| 196 |
*/ |
| 197 |
public function password($password): AbstractVault |
| 198 |
{ |
| 199 |
return $this->setPassword($password); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Return current password |
| 204 |
* |
| 205 |
* @return string |
| 206 |
*/ |
| 207 |
public function getPassword() |
| 208 |
{ |
| 209 |
return $this->password; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Set username and password at once |
| 214 |
* |
| 215 |
* @param string $username |
| 216 |
* @param string $password |
| 217 |
* @return AbstractVault |
| 218 |
*/ |
| 219 |
public function credentials($username, $password): AbstractVault |
| 220 |
{ |
| 221 |
return $this->setUsername($username)->setPassword($password); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Sends HTTP 401 Header |
| 226 |
* |
| 227 |
* @return void |
| 228 |
*/ |
| 229 |
protected function denyAccess(): void |
| 230 |
{ |
| 231 |
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
| 232 |
header($protocol . ' Unauthorized'); |
| 233 |
header('WWW-Authenticate: ' . (string) $this->getDirective()); |
| 234 |
exit('<strong>' . $protocol . ' 401 Unauthorized</strong>'); |
| 235 |
} |
| 236 |
} |
| 237 |
|