Skip to content

Commit

Permalink
add multiple auth key management
Browse files Browse the repository at this point in the history
  • Loading branch information
iNem0o committed Aug 31, 2021
1 parent 3e2bd32 commit 1f08b84
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/Webservice/Authenticator/SimpleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

class SimpleAuthenticator implements AuthenticatorInterface
{
protected $key = "-------------------------------";
/**
* @var string[]
*/
protected $key = [];

/** @var Response */
protected $response;
Expand All @@ -23,7 +26,7 @@ class SimpleAuthenticator implements AuthenticatorInterface
*/
public function __construct($key)
{
$this->key=$key;
$this->setKey($key);
}

/**
Expand All @@ -33,20 +36,26 @@ public function tryAuth()
{
// controle auth
//set http auth headers for apache+php-cgi work around
if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'],
$matches)
if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match(
'/Basic\s+(.*)$/i',
$_SERVER['HTTP_AUTHORIZATION'],
$matches
)
) {
if(isset($matches[1])){
if (isset($matches[1])) {
list($name, $password) = explode(':', base64_decode($matches[1]));
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
}
}

//set http auth headers for apache+php-cgi work around if variable gets renamed by apache
if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i',
$_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches)
if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && preg_match(
'/Basic\s+(.*)$/i',
$_SERVER['REDIRECT_HTTP_AUTHORIZATION'],
$matches
)
) {
if(isset($matches[1])) {
if (isset($matches[1])) {
list($name, $password) = explode(':', base64_decode($matches[1]));
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
}
Expand All @@ -57,30 +66,36 @@ public function tryAuth()
} elseif (isset($_GET['ws_key'])) {
$key = $_GET['ws_key'];
} else {
$this->response = new Response("Unauthorized",401,array(
"WWW-Authenticate" => 'Basic realm="Welcome to PrestaShop Webservice, please enter the authentication key as the login. No password required."'
$this->response = new Response("Unauthorized", 401, array(
"WWW-Authenticate" => 'Basic realm="Welcome to PrestaShop Webservice, please enter the authentication key as the login. No password required."',
));

return false;
}

if ($key != $this->key) {
$this->response = new Response("Unauthorized",401,array(
"WWW-Authenticate" => 'Basic realm="Welcome to PrestaShop Webservice, please enter the authentication key as the login. No password required."'
if (!in_array($key, $this->key, true)) {
$this->response = new Response("Unauthorized", 401, array(
"WWW-Authenticate" => 'Basic realm="Welcome to PrestaShop Webservice, please enter the authentication key as the login. No password required."',
));

return false;
}

return true;

}

/**
* @param string $key
* @param string|string[] $key
* @return SimpleAuthenticator
*/
public function setKey($key)
{
if (!is_array($key)) {
$key = [$key];
}

$this->key = $key;

return $this;
}

Expand Down

0 comments on commit 1f08b84

Please sign in to comment.