Skip to content

Commit

Permalink
- Fix log api key when ip is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
daycry committed Feb 14, 2023
1 parent 2f658a5 commit 37df851
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/RestServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ class RestServer extends ResourceController
*/
protected ?object $apiUser = null;

/**
* Information about the current API user.
*
* @var string
*/
protected ?string $key = null;

/**
* Information about the current AUTH user.
*
Expand Down Expand Up @@ -275,7 +282,7 @@ protected function _logRequest($authorized = false)
'uri' => $this->request->uri,
'method' => $this->request->getMethod(),
'params' => $params,
'api_key' => isset($this->apiUser->key) ? $this->apiUser->key : '',
'api_key' => isset($this->key) ? $this->key : '',
'ip_address' => $this->request->getIPAddress(),
'duration' => $this->_benchmark->getElapsedTime('petition'),
'response_code' => $this->response->getStatusCode(),
Expand Down Expand Up @@ -340,7 +347,7 @@ public function _remap($method, ...$params)
}
}

$this->apiUser = \Daycry\RestServer\Validators\ApiKey::check($this->request, $this->_petition, $this->args);
$this->apiUser = \Daycry\RestServer\Validators\ApiKey::check($this->request, $this->args, $this->_petition, $this->key);

if ($this->_restConfig->strictApiAndAuth && $this->apiUser instanceof \Exception) {
throw $this->apiUser;
Expand Down
4 changes: 2 additions & 2 deletions src/Validators/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class ApiKey
{
public static function check(RequestInterface $request, object $petition = null, array $args): ?object
public static function check(RequestInterface $request, array $args, object $petition = null, string &$key = null): ?object
{
$row = null;
$usekey = config('RestServer')->restEnableKeys;
Expand Down Expand Up @@ -54,7 +54,7 @@ public static function check(RequestInterface $request, object $petition = null,
}
}

if (!$found_address) {
if ($found_address !== true) {
return UnauthorizedException::forIpDenied();
}
} else {
Expand Down

0 comments on commit 37df851

Please sign in to comment.