Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
orkhanahmadov committed Sep 18, 2019
1 parent f5ec856 commit 75ff029
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
Expand Down
6 changes: 5 additions & 1 deletion src/IpMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class IpMiddleware

/**
* IpMiddleware constructor.
*
* @param Application $application
* @param Repository $config
*/
Expand All @@ -37,12 +38,15 @@ public function __construct(Application $application, Repository $config)
* @param Request $request
* @param Closure $next
* @param array $allowedIp
*
* @return mixed
*/
public function handle($request, Closure $next, ...$allowedIp)
{
$clientIp = $request->server('HTTP_CF_CONNECTING_IP') ?? $request->ip();

if (! $this->application->environment($this->config->get('ip-middleware.ignore_environments')) &&
!in_array($request->ip(), Arr::flatten($allowedIp))
! in_array($clientIp, Arr::flatten($allowedIp))
) {
abort(Response::HTTP_FORBIDDEN);
}
Expand Down
52 changes: 43 additions & 9 deletions tests/IpMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,58 @@

class IpMiddlewareTest extends TestCase
{
public function testFails()
/**
* @var IpMiddleware
*/
private $middleware;

protected function setUp(): void
{
parent::setUp();

$this->middleware = $this->app->make(IpMiddleware::class);
}

public function testBlocksIfIpIsNotWhitelisted()
{
$this->withoutExceptionHandling();
$this->expectException(HttpException::class);
$middleware = $this->app->make(IpMiddleware::class);
$request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '1.1.1.0']);

$middleware->handle($request, function () {
}, '1.1.1.1');
$this->middleware->handle($request, function () {
}, '1.1.1.1,2.2.2.2');
}

public function testPasses()
public function testAllowsWithCloudflareIpAddress()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_CF_CONNECTING_IP' => '2.1.1.1']);

$result = $this->middleware->handle($request, function () {
return true;
}, '2.1.1.1');

$this->assertTrue($result);
}

public function testAllowsIfIpIsWhitelisted()
{
$this->withoutExceptionHandling();
$middleware = new IpMiddleware();
$request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '1.1.1.1']);

$middleware->handle($request, function () {
$result = $this->middleware->handle($request, function () {
return true;
}, '1.1.1.1');

$this->assertTrue($result);
}

public function testAllowsIfEnvironmentIsIgnored()
{
app()['config']->set('ip-middleware.ignore_environments', ['testing']);
$request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '1.1.1.0']);

$result = $this->middleware->handle($request, function () {
return true;
}, '1.1.1.1');

$this->assertTrue($result);
}
}
10 changes: 0 additions & 10 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,4 @@ protected function getPackageProviders($app)
LaravelIpMiddlewareServiceProvider::class,
];
}

/**
* Set up the environment.
*
* @param Application $app
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testing');
}
}

0 comments on commit 75ff029

Please sign in to comment.