Skip to content

Commit 9387c6c

Browse files
committed
Support #447
1 parent bfb7740 commit 9387c6c

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

api.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3019,6 +3019,9 @@ private function writePasswords(String $passwordFile, array $passwords): bool
30193019

30203020
private function getAuthorizationCredentials(Request $request): String
30213021
{
3022+
if (isset($_SERVER['PHP_AUTH_USER'])) {
3023+
return $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'];
3024+
}
30223025
$parts = explode(' ', trim($request->getHeader('Authorization')), 2);
30233026
if (count($parts) != 2) {
30243027
return '';
@@ -3044,7 +3047,16 @@ public function handle(Request $request): Response
30443047
$validUser = $this->getValidUsername($username, $password, $passwordFile);
30453048
$_SESSION['username'] = $validUser;
30463049
if (!$validUser) {
3047-
return $this->responder->error(ErrorCode::ACCESS_DENIED, $username);
3050+
return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
3051+
}
3052+
}
3053+
if (!isset($_SESSION['username']) || !$_SESSION['username']) {
3054+
$authenticationMode = $this->getProperty('mode', 'required');
3055+
if ($authenticationMode == 'required') {
3056+
$response = $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
3057+
$realm = $this->getProperty('realm', 'Username and password required');
3058+
$response->addHeader('WWW-Authenticate', "Basic realm=\"$realm\"");
3059+
return $response;
30483060
}
30493061
}
30503062
return $this->next->handle($request);
@@ -3170,7 +3182,7 @@ public function handle(Request $request): Response
31703182
}
31713183
$allowedIpAddresses = $this->getProperty('allowedIpAddresses', '');
31723184
if (!$this->isIpAllowed($ipAddress, $allowedIpAddresses)) {
3173-
$response = $this->responder->error(ErrorCode::ACCESS_DENIED, $ipAddress);
3185+
$response = $this->responder->error(ErrorCode::TEMPORARY_OR_PERMANENTLY_BLOCKED, '');
31743186
} else {
31753187
$response = $this->next->handle($request);
31763188
}
@@ -3260,7 +3272,13 @@ public function handle(Request $request): Response
32603272
$claims = $this->getClaims($token);
32613273
$_SESSION['claims'] = $claims;
32623274
if (empty($claims)) {
3263-
return $this->responder->error(ErrorCode::ACCESS_DENIED, 'JWT');
3275+
return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, 'JWT');
3276+
}
3277+
}
3278+
if (empty($_SESSION['claims'])) {
3279+
$authenticationMode = $this->getProperty('mode', 'required');
3280+
if ($authenticationMode == 'required') {
3281+
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
32643282
}
32653283
}
32663284
return $this->next->handle($request);
@@ -4127,11 +4145,12 @@ class ErrorCode
41274145
const HTTP_MESSAGE_NOT_READABLE = 1008;
41284146
const DUPLICATE_KEY_EXCEPTION = 1009;
41294147
const DATA_INTEGRITY_VIOLATION = 1010;
4130-
const AUTHORIZATION_REQUIRED = 1011;
4131-
const ACCESS_DENIED = 1012;
4148+
const AUTHENTICATION_REQUIRED = 1011;
4149+
const AUTHENTICATION_FAILED = 1012;
41324150
const INPUT_VALIDATION_FAILED = 1013;
41334151
const OPERATION_FORBIDDEN = 1014;
41344152
const OPERATION_NOT_SUPPORTED = 1015;
4153+
const TEMPORARY_OR_PERMANENTLY_BLOCKED = 1016;
41354154

41364155
private $values = [
41374156
9999 => ["%s", Response::INTERNAL_SERVER_ERROR],
@@ -4146,11 +4165,12 @@ class ErrorCode
41464165
1008 => ["Cannot read HTTP message", Response::UNPROCESSABLE_ENTITY],
41474166
1009 => ["Duplicate key exception", Response::CONFLICT],
41484167
1010 => ["Data integrity violation", Response::CONFLICT],
4149-
1011 => ["Authorization required", Response::UNAUTHORIZED],
4150-
1012 => ["Access denied for '%s'", Response::FORBIDDEN],
4168+
1011 => ["Authentication required", Response::UNAUTHORIZED],
4169+
1012 => ["Authentication failed for '%s'", Response::FORBIDDEN],
41514170
1013 => ["Input validation failed for '%s'", Response::UNPROCESSABLE_ENTITY],
41524171
1014 => ["Operation forbidden", Response::FORBIDDEN],
41534172
1015 => ["Operation '%s' not supported", Response::METHOD_NOT_ALLOWED],
4173+
1016 => ["Temporary or permanently blocked", Response::FORBIDDEN],
41544174
];
41554175

41564176
public function __construct(int $code)
@@ -5412,6 +5432,7 @@ public function __toString(): String
54125432
'username' => 'php-crud-api',
54135433
'password' => 'php-crud-api',
54145434
'database' => 'php-crud-api',
5435+
'middlewares' => 'basicAuth',
54155436
]);
54165437
$request = new Request();
54175438
$api = new Api($config);

src/Tqdev/PhpCrudApi/Middleware/BasicAuthMiddleware.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ private function writePasswords(String $passwordFile, array $passwords): bool
6060

6161
private function getAuthorizationCredentials(Request $request): String
6262
{
63+
if (isset($_SERVER['PHP_AUTH_USER'])) {
64+
return $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'];
65+
}
6366
$parts = explode(' ', trim($request->getHeader('Authorization')), 2);
6467
if (count($parts) != 2) {
6568
return '';

src/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'username' => 'php-crud-api',
1212
'password' => 'php-crud-api',
1313
'database' => 'php-crud-api',
14+
'middlewares' => 'basicAuth',
1415
]);
1516
$request = new Request();
1617
$api = new Api($config);

0 commit comments

Comments
 (0)