-
Notifications
You must be signed in to change notification settings - Fork 314
Add support for password confirmation middleware. #6
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
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Laravel\Fortify\Contracts; | ||
|
||
use Illuminate\Contracts\Support\Responsable; | ||
|
||
interface FailedPasswordVerifyResponse extends Responsable | ||
{ | ||
// | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Laravel\Fortify\Contracts; | ||
|
||
use Illuminate\Contracts\Support\Responsable; | ||
|
||
interface PasswordVerifiedResponse extends Responsable | ||
{ | ||
// | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Laravel\Fortify\Contracts; | ||
|
||
use Illuminate\Contracts\Support\Responsable; | ||
|
||
interface VerifyPasswordViewResponse extends Responsable | ||
{ | ||
// | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Laravel\Fortify\Http\Controllers; | ||
|
||
use Illuminate\Contracts\Auth\StatefulGuard; | ||
use Illuminate\Contracts\Support\Responsable; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Controller; | ||
use Laravel\Fortify\Contracts\FailedPasswordVerifyResponse; | ||
use Laravel\Fortify\Contracts\PasswordVerifiedResponse; | ||
use Laravel\Fortify\Contracts\VerifyPasswordViewResponse; | ||
|
||
class VerifyPasswordController extends Controller | ||
{ | ||
/** | ||
* The guard implementation. | ||
* | ||
* @var \Illuminate\Contracts\Auth\StatefulGuard | ||
*/ | ||
protected $guard; | ||
|
||
/** | ||
* Create a new controller instance. | ||
* | ||
* @param \Illuminate\Contracts\Auth\StatefulGuard | ||
* @return void | ||
*/ | ||
public function __construct(StatefulGuard $guard) | ||
{ | ||
$this->guard = $guard; | ||
} | ||
|
||
/** | ||
* Show the verify password view. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Laravel\Fortify\Contracts\VerifyPasswordViewResponse | ||
*/ | ||
public function show(Request $request, VerifyPasswordViewResponse $response): VerifyPasswordViewResponse | ||
{ | ||
return $response; | ||
} | ||
|
||
/** | ||
* Verify the user's password. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Contracts\Support\Responsable | ||
*/ | ||
public function store(Request $request): Responsable | ||
{ | ||
$username = config('fortify.username'); | ||
if ($status = $this->guard->validate([ | ||
$username => $request->user()->{$username}, | ||
'password' => $request->input('password'), | ||
])) { | ||
$request->session()->put('auth.password_confirmed_at', time()); | ||
} | ||
|
||
return $status | ||
? app(PasswordVerifiedResponse::class) | ||
: app(FailedPasswordVerifyResponse::class); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Laravel\Fortify\Http\Responses; | ||
|
||
use Illuminate\Http\Response; | ||
use Illuminate\Validation\ValidationException; | ||
use Laravel\Fortify\Contracts\FailedPasswordVerifyResponse as FailedPasswordVerifyResponseContract; | ||
|
||
class FailedPasswordVerifyResponse implements FailedPasswordVerifyResponseContract | ||
{ | ||
/** | ||
* Create an HTTP response that represents the object. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function toResponse($request) | ||
{ | ||
$message = __('The provided password was incorrect.'); | ||
|
||
if ($request->wantsJson()) { | ||
throw ValidationException::withMessages([ | ||
'password' => [$message], | ||
]); | ||
} | ||
|
||
return redirect()->back()->withErrors(['password' => $message]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Laravel\Fortify\Http\Responses; | ||
|
||
use Illuminate\Http\Response; | ||
use Laravel\Fortify\Contracts\PasswordVerifiedResponse as PasswordVerifiedResponseContract; | ||
|
||
class PasswordVerifiedResponse implements PasswordVerifiedResponseContract | ||
{ | ||
/** | ||
* Create an HTTP response that represents the object. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function toResponse($request) | ||
{ | ||
return $request->wantsJson() | ||
? new Response('', 201) | ||
: redirect()->intended(config('fortify.home')); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Laravel\Fortify\Tests; | ||
|
||
use Illuminate\Foundation\Auth\User; | ||
use Laravel\Fortify\Contracts\VerifyPasswordViewResponse; | ||
|
||
class VerifyPasswordControllerTest extends OrchestraTestCase | ||
{ | ||
protected $user; | ||
|
||
public function test_the_verify_password_view_is_returned() | ||
{ | ||
$this->mock(VerifyPasswordViewResponse::class) | ||
->shouldReceive('toResponse') | ||
->andReturn(response('hello world')); | ||
|
||
$response = $this->withoutExceptionHandling()->actingAs($this->user)->get( | ||
'/user/password/verify' | ||
); | ||
|
||
$response->assertStatus(200); | ||
$response->assertSeeText('hello world'); | ||
} | ||
|
||
public function test_password_can_be_verified() | ||
{ | ||
$response = $this->withoutExceptionHandling() | ||
->actingAs($this->user) | ||
->withSession(['url.intended' => 'http://foo.com/bar']) | ||
->post( | ||
'/user/password/verify', | ||
['password' => 'secret'] | ||
); | ||
|
||
$response->assertSessionHas('auth.password_confirmed_at'); | ||
$response->assertRedirect('http://foo.com/bar'); | ||
} | ||
|
||
public function test_password_verification_can_fail() | ||
{ | ||
$response = $this->withoutExceptionHandling() | ||
->actingAs($this->user) | ||
->withSession(['url.intended' => 'http://foo.com/bar']) | ||
->post( | ||
'/user/password/verify', | ||
['password' => 'invalid'] | ||
); | ||
|
||
$response->assertSessionHasErrors(['password']); | ||
$response->assertSessionMissing('auth.password_confirmed_at'); | ||
$response->assertRedirect(); | ||
$this->assertNotEquals($response->getTargetUrl(), 'http://foo.com/bar'); | ||
} | ||
|
||
public function test_password_can_be_verified_with_json() | ||
{ | ||
$response = $this->actingAs($this->user) | ||
->postJson( | ||
'/user/password/verify', | ||
['password' => 'secret'] | ||
); | ||
$response->assertStatus(201); | ||
} | ||
|
||
public function test_password_verification_can_fail_with_json() | ||
{ | ||
$response = $this->actingAs($this->user) | ||
->postJson( | ||
'/user/password/verify', | ||
['password' => 'invalid'] | ||
); | ||
|
||
$response->assertJsonValidationErrors('password'); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->loadLaravelMigrations(['--database' => 'testbench']); | ||
$this->artisan('migrate', ['--database' => 'testbench'])->run(); | ||
|
||
$this->user = TestVerifyPasswordUser::forceCreate([ | ||
'name' => 'Taylor Otwell', | ||
'email' => '[email protected]', | ||
'password' => bcrypt('secret'), | ||
]); | ||
} | ||
|
||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['migrator']->path(__DIR__.'/../database/migrations'); | ||
|
||
$app['config']->set('auth.providers.users.model', TestVerifyPasswordUser::class); | ||
|
||
$app['config']->set('database.default', 'testbench'); | ||
|
||
$app['config']->set('database.connections.testbench', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
} | ||
} | ||
|
||
class TestVerifyPasswordUser extends User | ||
{ | ||
protected $table = 'users'; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.