Skip to content

Commit 8f72571

Browse files
committed
Fix IP access middleware
1 parent 9b93629 commit 8f72571

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

app/Http/Middleware/Api/AuthenticateIPAccess.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public function handle(Request $request, Closure $next)
2929
}
3030

3131
$find = new IP($request->ip());
32-
foreach ($model->allowed_ips as $ip) {
32+
foreach (json_decode($model->allowed_ips) as $ip) {
3333
if (Range::parse($ip)->contains($find)) {
3434
return $next($request);
3535
}
3636
}
3737

38-
throw new AccessDeniedHttpException('This IP address does not have permission to access the API using these credentials.');
38+
throw new AccessDeniedHttpException('This IP address (' . $request->ip() . ') does not have permission to access the API using these credentials.');
3939
}
4040
}

tests/Unit/Http/Middleware/API/AuthenticateIPAccessTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testWithNoIPRestrictions()
2525
*/
2626
public function testWithValidIP()
2727
{
28-
$model = factory(ApiKey::class)->make(['allowed_ips' => ['127.0.0.1']]);
28+
$model = factory(ApiKey::class)->make(['allowed_ips' => '["127.0.0.1"]']);
2929
$this->setRequestAttribute('api_key', $model);
3030

3131
$this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('127.0.0.1');
@@ -38,7 +38,7 @@ public function testWithValidIP()
3838
*/
3939
public function testValidIPAganistCIDRRange()
4040
{
41-
$model = factory(ApiKey::class)->make(['allowed_ips' => ['192.168.1.1/28']]);
41+
$model = factory(ApiKey::class)->make(['allowed_ips' => '["192.168.1.1/28"]']);
4242
$this->setRequestAttribute('api_key', $model);
4343

4444
$this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('192.168.1.15');
@@ -54,10 +54,10 @@ public function testValidIPAganistCIDRRange()
5454
*/
5555
public function testWithInvalidIP()
5656
{
57-
$model = factory(ApiKey::class)->make(['allowed_ips' => ['127.0.0.1']]);
57+
$model = factory(ApiKey::class)->make(['allowed_ips' => '["127.0.0.1"]']);
5858
$this->setRequestAttribute('api_key', $model);
5959

60-
$this->request->shouldReceive('ip')->withNoArgs()->once()->andReturn('127.0.0.2');
60+
$this->request->shouldReceive('ip')->withNoArgs()->twice()->andReturn('127.0.0.2');
6161

6262
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
6363
}

0 commit comments

Comments
 (0)