Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block the request to issue new tokens if an invalid provider is given. #50

Merged
merged 3 commits into from
Sep 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/Http/Middleware/AddCustomProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Http\Request;
use League\OAuth2\Server\Exception\OAuthServerException;

class AddCustomProvider
{
Expand All @@ -20,21 +21,25 @@ class AddCustomProvider
* is used by Laravel Passport to get the correct model on access token
* creation.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*
* @throws OAuthServerException
*/
public function handle(Request $request, Closure $next)
{
$this->defaultApiProvider = config('auth.guards.api.provider');

$params = $request->all();
if (array_key_exists('provider', $params)) {
if (! is_null($params['provider'])) {
config(['auth.guards.api.provider' => $params['provider']]);
}
$provider = $request->get('provider');

if ($this->invalidProvider($provider)) {
throw OAuthServerException::invalidRequest('provider');
}

config(['auth.guards.api.provider' => $provider]);

return $next($request);
}

Expand All @@ -51,4 +56,26 @@ public function terminate()
{
config(['auth.guards.api.provider' => $this->defaultApiProvider]);
}

/**
* Check if the given provider is not registered in the auth configuration file.
*
* @param $provider
*
* @return bool
*/
protected function invalidProvider($provider)
{
if (is_null($provider)) {
return true;
sfelix-martins marked this conversation as resolved.
Show resolved Hide resolved
}

foreach (config('auth.guards') as $guardsConfiguration) {
if ($guardsConfiguration['provider'] === $provider) {
return false;
}
}

return true;
}
}
34 changes: 31 additions & 3 deletions tests/Unit/AddCustomProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Mockery;
use Illuminate\Http\Request;
use SMartins\PassportMultiauth\Tests\TestCase;
use League\OAuth2\Server\Exception\OAuthServerException;
use SMartins\PassportMultiauth\Http\Middleware\AddCustomProvider;

class AddCustomProviderTest extends TestCase
Expand All @@ -24,10 +25,11 @@ public function tearDown()

public function testIfApiProviderOnAuthWasSetCorrectly()
sfelix-martins marked this conversation as resolved.
Show resolved Hide resolved
{
// We'll push a fake testing provider in the auth config registered in the app..
config(['auth.guards.testing.provider' => 'companies']);

$request = Mockery::mock(Request::class);
$request->shouldReceive('all')->andReturn([
'provider' => 'companies',
]);
$request->shouldReceive('get')->andReturn('companies')->with('provider');

$middleware = new AddCustomProvider();
$middleware->handle($request, function () {
sfelix-martins marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -40,4 +42,30 @@ public function testIfApiProviderOnAuthWasSetCorrectly()
$middleware->terminate();
$this->assertEquals(config('auth.guards.api.provider'), 'users');
}

public function testPassNotExistentProvider()
{
$this->expectException(OAuthServerException::class);

$request = Mockery::mock(Request::class);
$request->shouldReceive('get')->andReturn('not_found')->with('provider');

$middleware = new AddCustomProvider();
$middleware->handle($request, function () {
return 'response';
});
}

public function testDoNotPassProviderToRequest()
{
$this->expectException(OAuthServerException::class);

$request = Mockery::mock(Request::class);
$request->shouldReceive('get')->andReturn(null)->with('provider');

$middleware = new AddCustomProvider();
$middleware->handle($request, function () {
return 'response';
});
}
}