| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\HttpAuth; |
| 4 |
|
| 5 |
class HttpAuth |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Authentication type |
| 9 |
* |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
protected static $type = 'basic'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Name of authentication realm |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected static $realm = 'Secured Resource'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Username |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected static $username = 'admin'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Password |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected static $password = 'secret'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Static factory method |
| 37 |
* |
| 38 |
* @param array $config |
| 39 |
* @return HttpAuth |
| 40 |
*/ |
| 41 |
public static function make(array $config = []): HttpAuth |
| 42 |
{ |
| 43 |
return (new self())->configure($config); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Configure current instance by array |
| 48 |
* |
| 49 |
* @param array $config |
| 50 |
* @return HttpAuth |
| 51 |
*/ |
| 52 |
private function configure(array $config = []): HttpAuth |
| 53 |
{ |
| 54 |
foreach ($config as $key => $value) { |
| 55 |
if (isset(static::${$key})) { |
| 56 |
static::${$key} = $value; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Create vault by current parameters and secure it |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function secure(): void |
| 69 |
{ |
| 70 |
$this->getVault()->secure(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Create HTTP basic auth instance |
| 75 |
* |
| 76 |
* @return HttpAuth |
| 77 |
*/ |
| 78 |
public function basic(): HttpAuth |
| 79 |
{ |
| 80 |
static::$type = 'basic'; |
| 81 |
|
| 82 |
return $this; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Create HTTP digest auth instance |
| 87 |
* |
| 88 |
* @return HttpAuth |
| 89 |
*/ |
| 90 |
public function digest(): HttpAuth |
| 91 |
{ |
| 92 |
static::$type = 'digest'; |
| 93 |
|
| 94 |
return $this; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Set type of configured vault |
| 99 |
* |
| 100 |
* @param string $value |
| 101 |
* @return HttpAuth |
| 102 |
*/ |
| 103 |
public function type($value): HttpAuth |
| 104 |
{ |
| 105 |
static::$type = $value; |
| 106 |
|
| 107 |
return $this; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Set realm name of configured vault |
| 112 |
* |
| 113 |
* @param string $value |
| 114 |
* @return HttpAuth |
| 115 |
*/ |
| 116 |
public function realm($value): HttpAuth |
| 117 |
{ |
| 118 |
static::$realm = $value; |
| 119 |
|
| 120 |
return $this; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Set username of configured vault |
| 125 |
* |
| 126 |
* @param string $value |
| 127 |
* @return HttpAuth |
| 128 |
*/ |
| 129 |
public function username($value): HttpAuth |
| 130 |
{ |
| 131 |
static::$username = $value; |
| 132 |
|
| 133 |
return $this; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Set password of configured vault |
| 138 |
* |
| 139 |
* @param string $value |
| 140 |
* @return HttpAuth |
| 141 |
*/ |
| 142 |
public function password($value): HttpAuth |
| 143 |
{ |
| 144 |
static::$password = $value; |
| 145 |
|
| 146 |
return $this; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Set credentials for configured vault |
| 151 |
* |
| 152 |
* @param string $username |
| 153 |
* @param string $password |
| 154 |
* @return HttpAuth |
| 155 |
*/ |
| 156 |
public function credentials($username, $password): HttpAuth |
| 157 |
{ |
| 158 |
return $this->username($username)->password($password); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get type of current instance |
| 163 |
* |
| 164 |
* @return mixed |
| 165 |
*/ |
| 166 |
public function getType() |
| 167 |
{ |
| 168 |
return static::$type; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get realm of current instance |
| 173 |
* |
| 174 |
* @return mixed |
| 175 |
*/ |
| 176 |
public function getRealm() |
| 177 |
{ |
| 178 |
return static::$realm; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Get username of current instance |
| 183 |
* |
| 184 |
* @return mixed |
| 185 |
*/ |
| 186 |
public function getUsername() |
| 187 |
{ |
| 188 |
return static::$username; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get password of current instance |
| 193 |
* |
| 194 |
* @return mixed |
| 195 |
*/ |
| 196 |
public function getPassword() |
| 197 |
{ |
| 198 |
return static::$password; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Return ready configured vault |
| 203 |
* |
| 204 |
* @return AbstractVault |
| 205 |
*/ |
| 206 |
protected function getVault(): AbstractVault |
| 207 |
{ |
| 208 |
$classname = sprintf('%s\Vault\%sVault', __NAMESPACE__, ucfirst(strtolower(static::$type))); |
| 209 |
|
| 210 |
if (! class_exists($classname)) { |
| 211 |
throw new Exception\NotSupportedException( |
| 212 |
'Unable to create HTTP authentication vault of type "' . static::$type . '".' |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
return new $classname(static::$realm, static::$username, static::$password); |
| 217 |
} |
| 218 |
} |
| 219 |
|