Skip to content

Added case-sensitivity option for usernames #485

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
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
1 change: 1 addition & 0 deletions config/fortify.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'home' => '/home',
'prefix' => '',
'domain' => null,
'lowercase_usernames' => false,
'limiters' => [
'login' => null,
],
Expand Down
25 changes: 25 additions & 0 deletions src/Actions/CanonicalizeUsername.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Laravel\Fortify\Actions;

use Illuminate\Support\Str;
use Laravel\Fortify\Fortify;

class CanonicalizeUsername
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param callable $next
* @return mixed
*/
public function handle($request, $next)
{
$request->merge([
Fortify::username() => Str::lower($request->{Fortify::username()}),
]);

return $next($request);
}
}
2 changes: 2 additions & 0 deletions src/Http/Controllers/AuthenticatedSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Routing\Controller;
use Illuminate\Routing\Pipeline;
use Laravel\Fortify\Actions\AttemptToAuthenticate;
use Laravel\Fortify\Actions\CanonicalizeUsername;
use Laravel\Fortify\Actions\EnsureLoginIsNotThrottled;
use Laravel\Fortify\Actions\PrepareAuthenticatedSession;
use Laravel\Fortify\Actions\RedirectIfTwoFactorAuthenticatable;
Expand Down Expand Up @@ -83,6 +84,7 @@ protected function loginPipeline(LoginRequest $request)

return (new Pipeline(app()))->send($request)->through(array_filter([
config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
config('fortify.lowercase_usernames') ? CanonicalizeUsername::class : null,
Features::enabled(Features::twoFactorAuthentication()) ? RedirectIfTwoFactorAuthenticatable::class : null,
AttemptToAuthenticate::class,
PrepareAuthenticatedSession::class,
Expand Down
8 changes: 8 additions & 0 deletions src/Http/Controllers/ProfileInformationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Str;
use Laravel\Fortify\Contracts\ProfileInformationUpdatedResponse;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
use Laravel\Fortify\Fortify;

class ProfileInformationController extends Controller
{
Expand All @@ -19,6 +21,12 @@ class ProfileInformationController extends Controller
public function update(Request $request,
UpdatesUserProfileInformation $updater)
{
if (config('fortify.lowercase_usernames')) {
$request->merge([
Fortify::username() => Str::lower($request->{Fortify::username()}),
]);
}

$updater->update($request->user(), $request->all());

return app(ProfileInformationUpdatedResponse::class);
Expand Down
8 changes: 8 additions & 0 deletions src/Http/Controllers/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Str;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Fortify\Contracts\RegisterResponse;
use Laravel\Fortify\Contracts\RegisterViewResponse;
use Laravel\Fortify\Fortify;

class RegisteredUserController extends Controller
{
Expand Down Expand Up @@ -51,6 +53,12 @@ public function create(Request $request): RegisterViewResponse
public function store(Request $request,
CreatesNewUsers $creator): RegisterResponse
{
if (config('fortify.lowercase_usernames')) {
$request->merge([
Fortify::username() => Str::lower($request->{Fortify::username()}),
]);
}

event(new Registered($user = $creator->create($request->all())));

$this->guard->login($user);
Expand Down
13 changes: 13 additions & 0 deletions stubs/fortify.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@

'email' => 'email',

/*
|--------------------------------------------------------------------------
| Lowercase Usernames
|--------------------------------------------------------------------------
|
| This value defines whether usernames should be lowercased before saving
| them in the database, as some database system string fields are case
| sensitive. You may disable this for your application if necessary.
|
*/

'lowercase_usernames' => true,

/*
|--------------------------------------------------------------------------
| Home Path
Expand Down
20 changes: 20 additions & 0 deletions tests/AuthenticatedSessionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,26 @@ public function test_two_factor_challenge_requires_a_challenged_user()
$this->assertNull(Auth::getUser());
}

public function test_case_insensitive_usernames_can_be_used()
{
app('config')->set('fortify.lowercase_usernames', true);

$this->loadLaravelMigrations(['--database' => 'testbench']);

TestAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
]);

$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
]);

$response->assertRedirect('/home');
}

protected function getPackageProviders($app)
{
return [FortifyServiceProvider::class];
Expand Down
22 changes: 22 additions & 0 deletions tests/ProfileInformationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,26 @@ public function test_contact_information_can_be_updated()

$response->assertStatus(200);
}

public function test_email_address_will_be_updated_case_insensitive()
{
app('config')->set('fortify.lowercase_usernames', true);

$user = Mockery::mock(Authenticatable::class);

$this->mock(UpdatesUserProfileInformation::class)
->shouldReceive('update')
->with($user, [
'name' => 'Taylor Otwell',
'email' => '[email protected]',
])
->once();

$response = $this->withoutExceptionHandling()->actingAs($user)->putJson('/user/profile-information', [
'name' => 'Taylor Otwell',
'email' => '[email protected]',
]);

$response->assertStatus(200);
}
}
25 changes: 25 additions & 0 deletions tests/RegisteredUserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,29 @@ public function test_users_can_be_created_and_redirected_to_intended_url()

$response->assertRedirect('http://foo.com/bar');
}

public function test_usernames_will_be_stored_case_insensitive()
{
app('config')->set('fortify.lowercase_usernames', true);

$this->mock(CreatesNewUsers::class)
->shouldReceive('create')
->with([
'email' => '[email protected]',
'password' => 'password',
])
->once()
->andReturn(Mockery::mock(Authenticatable::class));

$this->mock(StatefulGuard::class)
->shouldReceive('login')
->once();

$response = $this->post('/register', [
'email' => '[email protected]',
'password' => 'password',
]);

$response->assertRedirect('/home');
}
}