Fix IP access middleware

This commit is contained in:
Dane Everitt 2018-02-28 23:39:59 -06:00
parent 9b93629f45
commit 8f72571895
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 6 additions and 6 deletions

View File

@ -29,12 +29,12 @@ class AuthenticateIPAccess
} }
$find = new IP($request->ip()); $find = new IP($request->ip());
foreach ($model->allowed_ips as $ip) { foreach (json_decode($model->allowed_ips) as $ip) {
if (Range::parse($ip)->contains($find)) { if (Range::parse($ip)->contains($find)) {
return $next($request); return $next($request);
} }
} }
throw new AccessDeniedHttpException('This IP address does not have permission to access the API using these credentials.'); throw new AccessDeniedHttpException('This IP address (' . $request->ip() . ') does not have permission to access the API using these credentials.');
} }
} }

View File

@ -25,7 +25,7 @@ class AuthenticateIPAccessTest extends MiddlewareTestCase
*/ */
public function testWithValidIP() public function testWithValidIP()
{ {
$model = factory(ApiKey::class)->make(['allowed_ips' => ['127.0.0.1']]); $model = factory(ApiKey::class)->make(['allowed_ips' => '["127.0.0.1"]']);
$this->setRequestAttribute('api_key', $model); $this->setRequestAttribute('api_key', $model);
$this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('127.0.0.1'); $this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('127.0.0.1');
@ -38,7 +38,7 @@ class AuthenticateIPAccessTest extends MiddlewareTestCase
*/ */
public function testValidIPAganistCIDRRange() public function testValidIPAganistCIDRRange()
{ {
$model = factory(ApiKey::class)->make(['allowed_ips' => ['192.168.1.1/28']]); $model = factory(ApiKey::class)->make(['allowed_ips' => '["192.168.1.1/28"]']);
$this->setRequestAttribute('api_key', $model); $this->setRequestAttribute('api_key', $model);
$this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('192.168.1.15'); $this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('192.168.1.15');
@ -54,10 +54,10 @@ class AuthenticateIPAccessTest extends MiddlewareTestCase
*/ */
public function testWithInvalidIP() public function testWithInvalidIP()
{ {
$model = factory(ApiKey::class)->make(['allowed_ips' => ['127.0.0.1']]); $model = factory(ApiKey::class)->make(['allowed_ips' => '["127.0.0.1"]']);
$this->setRequestAttribute('api_key', $model); $this->setRequestAttribute('api_key', $model);
$this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('127.0.0.2'); $this->request->shouldReceive('ip')->withNoArgs()->twice()->andReturn('127.0.0.2');
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
} }