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

Add support for password confirmation middleware. #6

Merged
merged 3 commits into from
Sep 11, 2020
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
8 changes: 8 additions & 0 deletions routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
->middleware(['auth']);
}

// Password Verification...
Route::get('/user/password/verify', 'VerifyPasswordController@show')
->middleware(['auth'])
->name('password.confirm');

Route::post('/user/password/verify', 'VerifyPasswordController@store')
->middleware(['auth']);

// Two Factor Authentication...
if (Features::enabled(Features::twoFactorAuthentication())) {
Route::post('/user/two-factor-authentication', 'TwoFactorAuthenticationController@store')
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/FailedPasswordVerifyResponse.php
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
{
//
}
10 changes: 10 additions & 0 deletions src/Contracts/PasswordVerifiedResponse.php
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
{
//
}
10 changes: 10 additions & 0 deletions src/Contracts/VerifyPasswordViewResponse.php
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
{
//
}
13 changes: 13 additions & 0 deletions src/Fortify.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ public static function verifyEmailView($view)
});
}

/**
* Specify which view should be used as the password verification prompt.
*
* @param string $view
* @return void
*/
public static function verifyPasswordView($view)
{
app()->singleton(VerifyPasswordViewResponse::class, function () use ($view) {
return new SimpleViewResponse($view);
});
}

/**
* Specify which view should be used as the request password reset link view.
*
Expand Down
6 changes: 6 additions & 0 deletions src/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Contracts\FailedPasswordResetLinkRequestResponse as FailedPasswordResetLinkRequestResponseContract;
use Laravel\Fortify\Contracts\FailedPasswordResetResponse as FailedPasswordResetResponseContract;
use Laravel\Fortify\Contracts\FailedPasswordVerifyResponse as FailedPasswordVerifyResponseContract;
use Laravel\Fortify\Contracts\LockoutResponse as LockoutResponseContract;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Laravel\Fortify\Contracts\LogoutResponse as LogoutResponseContract;
use Laravel\Fortify\Contracts\PasswordResetResponse as PasswordResetResponseContract;
use Laravel\Fortify\Contracts\PasswordVerifiedResponse as PasswordVerifiedResponseContract;
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
use Laravel\Fortify\Contracts\SuccessfulPasswordResetLinkRequestResponse as SuccessfulPasswordResetLinkRequestResponseContract;
use Laravel\Fortify\Contracts\TwoFactorAuthenticationProvider as TwoFactorAuthenticationProviderContract;
use Laravel\Fortify\Http\Responses\FailedPasswordResetLinkRequestResponse;
use Laravel\Fortify\Http\Responses\FailedPasswordResetResponse;
use Laravel\Fortify\Http\Responses\FailedPasswordVerifyResponse;
use Laravel\Fortify\Http\Responses\LockoutResponse;
use Laravel\Fortify\Http\Responses\LoginResponse;
use Laravel\Fortify\Http\Responses\LogoutResponse;
use Laravel\Fortify\Http\Responses\PasswordResetResponse;
use Laravel\Fortify\Http\Responses\PasswordVerifiedResponse;
use Laravel\Fortify\Http\Responses\RegisterResponse;
use Laravel\Fortify\Http\Responses\SuccessfulPasswordResetLinkRequestResponse;

Expand Down Expand Up @@ -62,6 +66,8 @@ protected function registerResponseBindings()
$this->app->singleton(FailedPasswordResetLinkRequestResponseContract::class, FailedPasswordResetLinkRequestResponse::class);
$this->app->singleton(PasswordResetResponseContract::class, PasswordResetResponse::class);
$this->app->singleton(FailedPasswordResetResponseContract::class, FailedPasswordResetResponse::class);
$this->app->singleton(PasswordVerifiedResponseContract::class, PasswordVerifiedResponse::class);
$this->app->singleton(FailedPasswordVerifyResponseContract::class, FailedPasswordVerifyResponse::class);
}

/**
Expand Down
65 changes: 65 additions & 0 deletions src/Http/Controllers/VerifyPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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);
}
}
29 changes: 29 additions & 0 deletions src/Http/Responses/FailedPasswordVerifyResponse.php
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]);
}
}
22 changes: 22 additions & 0 deletions src/Http/Responses/PasswordVerifiedResponse.php
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'));
}
}
4 changes: 3 additions & 1 deletion src/Http/Responses/SimpleViewResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
use Laravel\Fortify\Contracts\ResetPasswordViewResponse;
use Laravel\Fortify\Contracts\TwoFactorChallengeViewResponse;
use Laravel\Fortify\Contracts\VerifyEmailViewResponse;
use Laravel\Fortify\Contracts\VerifyPasswordViewResponse;

class SimpleViewResponse implements
LoginViewResponse,
ResetPasswordViewResponse,
RegisterViewResponse,
RequestPasswordResetLinkViewResponse,
TwoFactorChallengeViewResponse,
VerifyEmailViewResponse
VerifyEmailViewResponse,
VerifyPasswordViewResponse
{
/**
* The name of the view.
Expand Down
109 changes: 109 additions & 0 deletions tests/VerifyPasswordControllerTest.php
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';
}