forked from apiato/apiato
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: made login attribute (email, phone, etc..) case sensitivity con…
…figurable resolves apiato#671
- Loading branch information
1 parent
2541d2e
commit 35ebd9e
Showing
6 changed files
with
81 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -64,6 +64,18 @@ | |
'email' => ['email'], | ||
], | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Case Sensitive | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This field represents if login attribute should be case-sensitive. | ||
| If false, then user can log in with both `[email protected]` and `[email protected]` | ||
| | ||
*/ | ||
|
||
'case_sensitive' => false, | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Prefix | ||
|
@@ -87,7 +99,7 @@ | |
| | ||
*/ | ||
'allowed-reset-password-urls' => [ | ||
'http://api.apiato.test/v1/password/reset', | ||
env('APP_URL', 'http://api.apiato.test/v1') . '/password/reset', | ||
], | ||
|
||
/* | ||
|
@@ -99,6 +111,6 @@ | |
| | ||
*/ | ||
'allowed-verify-email-urls' => [ | ||
'http://api.apiato.test/v1/email/verify', | ||
env('APP_URL', 'http://api.apiato.test/v1') . '/email/verify', | ||
], | ||
]; |
This file contains 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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use App\Containers\AppSection\Authentication\Tests\TestCase; | ||
use App\Containers\AppSection\Authentication\UI\WEB\Requests\LoginRequest; | ||
use App\Containers\AppSection\User\Models\User; | ||
use Illuminate\Support\Facades\Config; | ||
|
||
/** | ||
* Class WebLoginActionTest. | ||
|
@@ -28,16 +29,36 @@ public function testLogin(): void | |
$this->assertSame($user->name, $this->userDetails['name']); | ||
} | ||
|
||
public function testLoginWithInvalidCredentialsThrowsAnException(): void | ||
public function testLoginWithInvalidEmailThrowsAnException(): void | ||
{ | ||
$this->expectException(LoginFailedException::class); | ||
$this->expectExceptionMessage('Invalid Login Credentials.'); | ||
|
||
$this->request = new LoginRequest(['email' => '[email protected]', 'password' => 'wrong_password']); | ||
$this->request = new LoginRequest(['email' => '[email protected]', 'password' => $this->userDetails['password']]); | ||
|
||
$this->action->run($this->request); | ||
} | ||
|
||
public function testLoginWithInvalidPasswordThrowsAnException(): void | ||
{ | ||
$this->expectException(LoginFailedException::class); | ||
$this->expectExceptionMessage('Invalid Login Credentials.'); | ||
|
||
$this->request = new LoginRequest(['email' => $this->userDetails['email'], 'password' => 'wrong-password']); | ||
|
||
$this->action->run($this->request); | ||
} | ||
|
||
public function testLoginWithUppercaseEmail(): void | ||
{ | ||
Config::set('appSection-authentication.login.case_sensitive', false); | ||
|
||
$user = $this->action->run($this->request); | ||
|
||
$this->assertInstanceOf(User::class, $user); | ||
$this->assertSame($user->name, $this->userDetails['name']); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
15 changes: 15 additions & 0 deletions
15
app/Containers/AppSection/Authentication/Traits/LoginAttributeCaseSensitivityTrait.php
This file contains 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,15 @@ | ||
<?php | ||
|
||
namespace App\Containers\AppSection\Authentication\Traits; | ||
|
||
trait LoginAttributeCaseSensitivityTrait | ||
{ | ||
/** | ||
* @param string $username | ||
* @return string | ||
*/ | ||
private function processLoginAttributeCaseSensitivity(string $username): string | ||
{ | ||
return config('appSection-authentication.login.case_sensitive') ? $username : strtolower($username); | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -32,6 +32,24 @@ public function testClientWebAdminProxyLogin(): void | |
$this->assertResponseContainKeys(['expires_in', 'access_token']); | ||
} | ||
|
||
public function testClientWebAdminProxyLoginWithUppercaseEmail(): void | ||
{ | ||
$data = [ | ||
'email' => '[email protected]', | ||
'password' => 'testiness', | ||
]; | ||
$this->getTestingUser(['email' => '[email protected]', 'password' => $data['password'],]); | ||
Config::set('appSection-authentication.login.case_sensitive', false); | ||
|
||
$response = $this->makeCall($data); | ||
|
||
$response->assertStatus(200); | ||
$this->assertResponseContainKeyValue([ | ||
'token_type' => 'Bearer', | ||
]); | ||
$this->assertResponseContainKeys(['expires_in', 'access_token']); | ||
} | ||
|
||
public function testLoginWithNameAttribute(): void | ||
{ | ||
$data = [ | ||
|