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

[7.0] Allow multiple redirects when creating clients #928

Merged
merged 1 commit into from
Jan 11, 2019
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
22 changes: 17 additions & 5 deletions src/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Http\Rules\RedirectRule;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;

class ClientController
Expand All @@ -23,18 +24,29 @@ class ClientController
*/
protected $validation;

/**
* The redirect validation rule.
*
* @var \Laravel\Passport\Http\Rules\RedirectRule
*/
protected $redirectRule;

/**
* Create a client controller instance.
*
* @param \Laravel\Passport\ClientRepository $clients
* @param \Illuminate\Contracts\Validation\Factory $validation
* @param \Laravel\Passport\Http\Rules\RedirectRule $redirectRule
* @return void
*/
public function __construct(ClientRepository $clients,
ValidationFactory $validation)
{
public function __construct(
ClientRepository $clients,
ValidationFactory $validation,
RedirectRule $redirectRule
) {
$this->clients = $clients;
$this->validation = $validation;
$this->redirectRule = $redirectRule;
}

/**
Expand All @@ -60,7 +72,7 @@ public function store(Request $request)
{
$this->validation->make($request->all(), [
'name' => 'required|max:255',
'redirect' => 'required|url',
'redirect' => ['required', $this->redirectRule],
])->validate();

return $this->clients->create(
Expand All @@ -85,7 +97,7 @@ public function update(Request $request, $clientId)

$this->validation->make($request->all(), [
'name' => 'required|max:255',
'redirect' => 'required|url',
'redirect' => ['required', $this->redirectRule],
])->validate();

return $this->clients->update(
Expand Down
43 changes: 43 additions & 0 deletions src/Http/Rules/RedirectRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Laravel\Passport\Http\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\Factory;

class RedirectRule implements Rule
{
/**
* @var \Illuminate\Contracts\Validation\Factory
*/
private $validator;

public function __construct(Factory $validator)
{
$this->validator = $validator;
}

/**
* {@inheritdoc}
*/
public function passes($attribute, $value)
{
foreach (explode(',', $value) as $redirect) {
$validator = $this->validator->make(['redirect' => $redirect], ['redirect' => 'url']);

if ($validator->fails()) {
return false;
}
}

return true;
}

/**
* {@inheritdoc}
*/
public function message()
{
return 'One or more redirects have an invalid url format.';
}
}
37 changes: 27 additions & 10 deletions tests/ClientControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Laravel\Passport\Tests;

use Laravel\Passport\Client;
use Laravel\Passport\Http\Controllers\ClientController;
use Mockery as m;
use Illuminate\Http\Request;
use Laravel\Passport\Client;
use PHPUnit\Framework\TestCase;
use Laravel\Passport\Http\Rules\RedirectRule;
use Laravel\Passport\Http\Controllers\ClientController;

class ClientControllerTest extends TestCase
{
Expand All @@ -25,7 +26,9 @@ public function test_all_the_clients_for_the_current_user_can_be_retrieved()
$request->shouldReceive('user')->andReturn(new ClientControllerFakeUser);

$controller = new ClientController(
$clients, m::mock('Illuminate\Contracts\Validation\Factory')
$clients,
m::mock('Illuminate\Contracts\Validation\Factory'),
m::mock(RedirectRule::class)
);

$this->assertEquals($client, $controller->forUser($request));
Expand All @@ -45,17 +48,21 @@ public function test_clients_can_be_stored()
->with(1, 'client name', 'http://localhost')
->andReturn($client = new Client);

$redirectRule = m::mock(RedirectRule::class);

$validator = m::mock('Illuminate\Contracts\Validation\Factory');
$validator->shouldReceive('make')->once()->with([
'name' => 'client name',
'redirect' => 'http://localhost',
], [
'name' => 'required|max:255',
'redirect' => 'required|url',
'redirect' => ['required', $redirectRule],
])->andReturn($validator);
$validator->shouldReceive('validate')->once();

$controller = new ClientController($clients, $validator);
$controller = new ClientController(
$clients, $validator, $redirectRule
);

$this->assertEquals($client, $controller->store($request));
}
Expand All @@ -79,17 +86,21 @@ public function test_clients_can_be_updated()
m::type('Laravel\Passport\Client'), 'client name', 'http://localhost'
)->andReturn('response');

$redirectRule = m::mock(RedirectRule::class);

$validator = m::mock('Illuminate\Contracts\Validation\Factory');
$validator->shouldReceive('make')->once()->with([
'name' => 'client name',
'redirect' => 'http://localhost',
], [
'name' => 'required|max:255',
'redirect' => 'required|url',
'redirect' => ['required', $redirectRule],
])->andReturn($validator);
$validator->shouldReceive('validate')->once();

$controller = new ClientController($clients, $validator);
$controller = new ClientController(
$clients, $validator, $redirectRule
);

$this->assertEquals('response', $controller->update($request, 1));
}
Expand All @@ -112,7 +123,9 @@ public function test_404_response_if_client_doesnt_belong_to_user()

$validator = m::mock('Illuminate\Contracts\Validation\Factory');

$controller = new ClientController($clients, $validator);
$controller = new ClientController(
$clients, $validator, m::mock(RedirectRule::class)
);

$this->assertEquals(404, $controller->update($request, 1)->status());
}
Expand All @@ -138,7 +151,9 @@ public function test_clients_can_be_deleted()

$validator = m::mock('Illuminate\Contracts\Validation\Factory');

$controller = new ClientController($clients, $validator);
$controller = new ClientController(
$clients, $validator, m::mock(RedirectRule::class)
);

$controller->destroy($request, 1);
}
Expand All @@ -161,7 +176,9 @@ public function test_404_response_if_client_doesnt_belong_to_user_on_delete()

$validator = m::mock('Illuminate\Contracts\Validation\Factory');

$controller = new ClientController($clients, $validator);
$controller = new ClientController(
$clients, $validator, m::mock(RedirectRule::class)
);

$this->assertEquals(404, $controller->destroy($request, 1)->status());
}
Expand Down
49 changes: 49 additions & 0 deletions tests/RedirectRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Laravel\Passport\Tests;

use Illuminate\Contracts\Validation\Factory;
use Illuminate\Contracts\Validation\Validator;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Laravel\Passport\Http\Rules\RedirectRule;

class RedirectRuleTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function test_it_passes_with_a_single_valid_url()
{
$rule = $this->rule($fails = false);

$this->assertTrue($rule->passes('redirect', 'https://example.com'));
}

public function test_it_passes_with_multiple_valid_urls()
{
$rule = $this->rule($fails = false);

$this->assertTrue($rule->passes('redirect', 'https://example.com,https://example2.com'));
}

public function test_it_fails_with_a_single_invalid_url()
{
$rule = $this->rule($fails = true);

$this->assertFalse($rule->passes('redirect', 'https://example.com,invalid'));
}

private function rule(bool $fails): RedirectRule
{
$validator = m::mock(Validator::class);
$validator->shouldReceive('fails')->andReturn($fails);

$factory = m::mock(Factory::class);
$factory->shouldReceive('make')->andReturn($validator);

return new RedirectRule($factory);
}
}