44 if (is_null($realm)) {
47 if (! is_string($realm)) {
48 throw new \InvalidArgumentException();
52 throw new \InvalidArgumentException();
55 if (! is_string($table) || empty($table)) {
56 throw new \InvalidArgumentException();
59 $this->realm = $realm;
61 $this->table = $table;
70 if (! is_string($name) || empty($name)) {
71 throw new \InvalidArgumentException();
75 assert(isset($this->account));
76 return @$this->account->{$name};
86 if (! is_string($password)) {
87 throw new \InvalidArgumentException();
90 return password_hash($password, PASSWORD_BCRYPT);
98 $this->account = null;
99 $www_authenticate =
"WWW-Authenticate: Basic";
100 if (! empty($this->realm))
101 $www_authenticate .=
"realm=\"{$this->realm}\"";
102 header($www_authenticate);
105 http_response_code(401);
115 if (isset($this->account))
return true;
116 if ($this->processAuthentication())
return true;
121 private function processAuthentication()
124 if (! isset($_SERVER[
"PHP_AUTH_USER"]))
126 $username = $_SERVER[
'PHP_AUTH_USER'];
127 if (empty($username))
129 $acc_sql =
"select * from {$this->table} where username = ";
130 $acc_sql .= $this->pdo->quote($username);
131 $account = $this->pdo->query($acc_sql)->fetchObject();
134 $hash = $account->password;
136 throw new \UnexpectedValueException(
137 _(
"Empty password") .
": {$account->username}");
139 $password = $_SERVER[
'PHP_AUTH_PW'];
140 if (! password_verify($password, $hash))
142 $this->account = $account;
145 private $account = null;
authenticate()
Performs authentication.
Performs basic HTTP authentication.
getPasswordHash($password)
Creates a new password hash.
getAccountAttribute($name)
Returns an account attribute.
__construct($realm,\PDO $pdo, $table)
Creates a new instance.
An abstract authentication.