@@ -3019,6 +3019,9 @@ private function writePasswords(String $passwordFile, array $passwords): bool
3019
3019
3020
3020
private function getAuthorizationCredentials (Request $ request ): String
3021
3021
{
3022
+ if (isset ($ _SERVER ['PHP_AUTH_USER ' ])) {
3023
+ return $ _SERVER ['PHP_AUTH_USER ' ] . ': ' . $ _SERVER ['PHP_AUTH_PW ' ];
3024
+ }
3022
3025
$ parts = explode (' ' , trim ($ request ->getHeader ('Authorization ' )), 2 );
3023
3026
if (count ($ parts ) != 2 ) {
3024
3027
return '' ;
@@ -3044,7 +3047,16 @@ public function handle(Request $request): Response
3044
3047
$ validUser = $ this ->getValidUsername ($ username , $ password , $ passwordFile );
3045
3048
$ _SESSION ['username ' ] = $ validUser ;
3046
3049
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 ;
3048
3060
}
3049
3061
}
3050
3062
return $ this ->next ->handle ($ request );
@@ -3170,7 +3182,7 @@ public function handle(Request $request): Response
3170
3182
}
3171
3183
$ allowedIpAddresses = $ this ->getProperty ('allowedIpAddresses ' , '' );
3172
3184
if (!$ this ->isIpAllowed ($ ipAddress , $ allowedIpAddresses )) {
3173
- $ response = $ this ->responder ->error (ErrorCode::ACCESS_DENIED , $ ipAddress );
3185
+ $ response = $ this ->responder ->error (ErrorCode::TEMPORARY_OR_PERMANENTLY_BLOCKED , '' );
3174
3186
} else {
3175
3187
$ response = $ this ->next ->handle ($ request );
3176
3188
}
@@ -3260,7 +3272,13 @@ public function handle(Request $request): Response
3260
3272
$ claims = $ this ->getClaims ($ token );
3261
3273
$ _SESSION ['claims ' ] = $ claims ;
3262
3274
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 , '' );
3264
3282
}
3265
3283
}
3266
3284
return $ this ->next ->handle ($ request );
@@ -4127,11 +4145,12 @@ class ErrorCode
4127
4145
const HTTP_MESSAGE_NOT_READABLE = 1008 ;
4128
4146
const DUPLICATE_KEY_EXCEPTION = 1009 ;
4129
4147
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 ;
4132
4150
const INPUT_VALIDATION_FAILED = 1013 ;
4133
4151
const OPERATION_FORBIDDEN = 1014 ;
4134
4152
const OPERATION_NOT_SUPPORTED = 1015 ;
4153
+ const TEMPORARY_OR_PERMANENTLY_BLOCKED = 1016 ;
4135
4154
4136
4155
private $ values = [
4137
4156
9999 => ["%s " , Response::INTERNAL_SERVER_ERROR ],
@@ -4146,11 +4165,12 @@ class ErrorCode
4146
4165
1008 => ["Cannot read HTTP message " , Response::UNPROCESSABLE_ENTITY ],
4147
4166
1009 => ["Duplicate key exception " , Response::CONFLICT ],
4148
4167
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 ],
4151
4170
1013 => ["Input validation failed for '%s' " , Response::UNPROCESSABLE_ENTITY ],
4152
4171
1014 => ["Operation forbidden " , Response::FORBIDDEN ],
4153
4172
1015 => ["Operation '%s' not supported " , Response::METHOD_NOT_ALLOWED ],
4173
+ 1016 => ["Temporary or permanently blocked " , Response::FORBIDDEN ],
4154
4174
];
4155
4175
4156
4176
public function __construct (int $ code )
@@ -5412,6 +5432,7 @@ public function __toString(): String
5412
5432
'username ' => 'php-crud-api ' ,
5413
5433
'password ' => 'php-crud-api ' ,
5414
5434
'database ' => 'php-crud-api ' ,
5435
+ 'middlewares ' => 'basicAuth ' ,
5415
5436
]);
5416
5437
$ request = new Request ();
5417
5438
$ api = new Api ($ config );
0 commit comments