for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Uauth;
class Basic
{
/**
* @var string
*/
protected $user;
protected $password;
protected $realm;
* @var callable
protected $verify;
protected $deny;
* Constructor.
public function __construct($realm = "Secured Area", array $allowedUsers = array())
$this->realm = $realm;
$this->verify = function ($user, $password) use ($allowedUsers) {
return isset($allowedUsers[$user]) && $allowedUsers[$user] == $password;
};
}
* Set realm
* @param string $realm
*
* @return Basic The current instance
public function realm($realm)
return $this;
* Set the user verification system
* @param callable $verify
* @return Basic
public function verify(callable $verify)
$this->verify = $verify;
* Set the callable executed on deny by the verification
* @param callable $deny
public function deny(callable $deny)
$this->deny = $deny;
* Process the basic auth
public function auth()
$user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null;
$password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null;
if (is_null($user) || !(bool) call_user_func($this->verify, $user, $password)) {
header(sprintf('WWW-Authenticate: Basic realm="%s"', $this->realm));
header('HTTP/1.0 401 Unauthorized');
if ($this->deny) {
call_user_func($this->deny, $user);
exit;
$this->user = $user;
$this->password = $password;
* Get the verified user
* Only available after the auth method
* @return string
public function getUser()
return $this->user;
* Get the password of the verified user
public function getPassword()
return $this->password;