Skip to content

Commit 662c3fd

Browse files
committed
Add option to require username to be an email address
1 parent efab8e1 commit 662c3fd

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

src/Authorizer/CredentialFieldsCheckTrait.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@ trait CredentialFieldsCheckTrait
2121
*/
2222
private function verifyRequiredFields(array $credentials, array $fields)
2323
{
24-
$is_empty = function ($credentials, $field) {
25-
return isset($credentials[$field]) ? $credentials[$field] === '' : true;
26-
};
24+
foreach ($fields as $field) {
25+
if (empty($credentials[$field])) {
26+
throw new InvalidAuthenticationRequestException();
27+
}
28+
}
29+
}
2730

31+
/**
32+
* @param array $credentials
33+
* @param array $fields
34+
*/
35+
private function verifyEmailFields(array $credentials, array $fields)
36+
{
2837
foreach ($fields as $field) {
29-
if ($is_empty($credentials, $field)) {
38+
if (empty($credentials[$field]) || !filter_var($credentials[$field], FILTER_VALIDATE_EMAIL)) {
3039
throw new InvalidAuthenticationRequestException();
3140
}
3241
}

src/Authorizer/LocalAuthorizer.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ class LocalAuthorizer extends Authorizer
2525
*/
2626
private $user_repository;
2727

28+
/**
29+
* @var bool
30+
*/
31+
private $username_is_email = false;
32+
2833
/**
2934
* @param RepositoryInterface $user_repository
35+
* @param bool $username_is_email
3036
*/
31-
public function __construct(RepositoryInterface $user_repository)
37+
public function __construct(RepositoryInterface $user_repository, $username_is_email = false)
3238
{
3339
$this->user_repository = $user_repository;
40+
$this->username_is_email = (bool) $username_is_email;
3441
}
3542

3643
/**
@@ -43,6 +50,10 @@ public function verifyCredentials(array $credentials)
4350
{
4451
$this->verifyRequiredFields($credentials, ['username', 'password']);
4552

53+
if ($this->username_is_email) {
54+
$this->verifyEmailFields($credentials, ['username']);
55+
}
56+
4657
$user = $this->user_repository->findByUsername($credentials['username']);
4758

4859
$this->verifyUser($user, $credentials['password']);

test/src/LocalAuthorizerTest.php

+39-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ public function providerInvalidCredentials()
4141
];
4242
}
4343

44+
/**
45+
* @param array $username
46+
* @dataProvider providerInvalidUsername
47+
* @expectedException \ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException
48+
* @expectedExceptionMessage Authentication request data not valid
49+
*/
50+
public function testInvalidUsernameThrowsException($username)
51+
{
52+
$local_authorizer = new LocalAuthorizer(new Repository(), true);
53+
54+
$local_authorizer->verifyCredentials([
55+
'username' => $username,
56+
'password' => 'Easy to remember, Hard to guess',
57+
]);
58+
}
59+
60+
public function providerInvalidUsername()
61+
{
62+
return [
63+
['username' => null],
64+
['username' => ''],
65+
['username' => 'Invalid Username'],
66+
['username' => 'Not a valid Username'],
67+
['username' => 'not_a_username'],
68+
];
69+
}
70+
4471
/**
4572
* @expectedException \ActiveCollab\Authentication\Exception\UserNotFoundException
4673
* @expectedExceptionMessage User not found
@@ -81,9 +108,20 @@ public function testUserCanNotAuthenticateThrowsException()
81108
public function testUserIsAuthenticated()
82109
{
83110
$local_authorizer = new LocalAuthorizer(new Repository([
84-
'[email protected]' => new AuthenticatedUser(1, '[email protected]', 'John', 'password', true),
111+
'[email protected]' => new AuthenticatedUser(1, 'johndoe', 'John', 'password', true),
85112
]));
86113

114+
$user = $local_authorizer->verifyCredentials(['username' => 'johndoe', 'password' => 'password']);
115+
116+
$this->assertSame(1, $user->getId());
117+
}
118+
119+
public function testUserWithEmailUsernameIsAuthenticated()
120+
{
121+
$local_authorizer = new LocalAuthorizer(new Repository([
122+
'[email protected]' => new AuthenticatedUser(1, '[email protected]', 'John', 'password', true),
123+
]), true);
124+
87125
$user = $local_authorizer->verifyCredentials(['username' => '[email protected]', 'password' => 'password']);
88126

89127
$this->assertSame(1, $user->getId());

0 commit comments

Comments
 (0)