Skip to content

Commit 46e1c44

Browse files
committed
Make username formats extensible
1 parent 662c3fd commit 46e1c44

File tree

4 files changed

+72
-11
lines changed

4 files changed

+72
-11
lines changed

src/Authorizer/AuthorizerInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
interface AuthorizerInterface
1717
{
18+
const USERNAME_FORMAT_ANY = 'any';
19+
const USERNAME_FORMAT_ALPHANUM = 'alphanum';
20+
const USERNAME_FORMAT_EMAIL = 'email';
21+
1822
/**
1923
* Perform user credentials verification against the real user database provider.
2024
*

src/Authorizer/CredentialFieldsCheckTrait.php

+13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ private function verifyRequiredFields(array $credentials, array $fields)
2828
}
2929
}
3030

31+
/**
32+
* @param array $credentials
33+
* @param array $fields
34+
*/
35+
private function verifyAlphanumFields(array $credentials, array $fields)
36+
{
37+
foreach ($fields as $field) {
38+
if (empty($credentials[$field]) || !ctype_alnum($credentials[$field])) {
39+
throw new InvalidAuthenticationRequestException();
40+
}
41+
}
42+
}
43+
3144
/**
3245
* @param array $credentials
3346
* @param array $fields

src/Authorizer/LocalAuthorizer.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ class LocalAuthorizer extends Authorizer
2828
/**
2929
* @var bool
3030
*/
31-
private $username_is_email = false;
31+
private $username_format;
3232

3333
/**
3434
* @param RepositoryInterface $user_repository
35-
* @param bool $username_is_email
35+
* @param string $username_format
3636
*/
37-
public function __construct(RepositoryInterface $user_repository, $username_is_email = false)
37+
public function __construct(RepositoryInterface $user_repository, $username_format = AuthorizerInterface::USERNAME_FORMAT_ANY)
3838
{
3939
$this->user_repository = $user_repository;
40-
$this->username_is_email = (bool) $username_is_email;
40+
$this->username_format = $username_format;
4141
}
4242

4343
/**
@@ -50,8 +50,13 @@ public function verifyCredentials(array $credentials)
5050
{
5151
$this->verifyRequiredFields($credentials, ['username', 'password']);
5252

53-
if ($this->username_is_email) {
54-
$this->verifyEmailFields($credentials, ['username']);
53+
switch ($this->username_format) {
54+
case AuthorizerInterface::USERNAME_FORMAT_ALPHANUM:
55+
$this->verifyAlphanumFields($credentials, ['username']);
56+
break;
57+
case AuthorizerInterface::USERNAME_FORMAT_EMAIL:
58+
$this->verifyEmailFields($credentials, ['username']);
59+
break;
5560
}
5661

5762
$user = $this->user_repository->findByUsername($credentials['username']);

test/src/LocalAuthorizerTest.php

+44-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace ActiveCollab\Authentication\Test;
1010

11+
use ActiveCollab\Authentication\Authorizer\AuthorizerInterface;
1112
use ActiveCollab\Authentication\Authorizer\LocalAuthorizer;
1213
use ActiveCollab\Authentication\Test\AuthenticatedUser\AuthenticatedUser;
1314
use ActiveCollab\Authentication\Test\AuthenticatedUser\Repository;
@@ -43,21 +44,48 @@ public function providerInvalidCredentials()
4344

4445
/**
4546
* @param array $username
46-
* @dataProvider providerInvalidUsername
47+
* @dataProvider providerInvalidAlphaNumUsername
4748
* @expectedException \ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException
4849
* @expectedExceptionMessage Authentication request data not valid
4950
*/
50-
public function testInvalidUsernameThrowsException($username)
51+
public function testInvalidAlphaNumUsernameThrowsException($username)
5152
{
52-
$local_authorizer = new LocalAuthorizer(new Repository(), true);
53+
$local_authorizer = new LocalAuthorizer(new Repository(), AuthorizerInterface::USERNAME_FORMAT_ALPHANUM);
5354

5455
$local_authorizer->verifyCredentials([
5556
'username' => $username,
5657
'password' => 'Easy to remember, Hard to guess',
5758
]);
5859
}
5960

60-
public function providerInvalidUsername()
61+
public function providerInvalidAlphaNumUsername()
62+
{
63+
return [
64+
['username' => null],
65+
['username' => ''],
66+
['username' => 'Invalid Username'],
67+
['username' => 'not_a_username'],
68+
['username' => '[email protected]'],
69+
];
70+
}
71+
72+
/**
73+
* @param array $username
74+
* @dataProvider providerInvalidEmailUsername
75+
* @expectedException \ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException
76+
* @expectedExceptionMessage Authentication request data not valid
77+
*/
78+
public function testInvalidEmailUsernameThrowsException($username)
79+
{
80+
$local_authorizer = new LocalAuthorizer(new Repository(), AuthorizerInterface::USERNAME_FORMAT_EMAIL);
81+
82+
$local_authorizer->verifyCredentials([
83+
'username' => $username,
84+
'password' => 'Easy to remember, Hard to guess',
85+
]);
86+
}
87+
88+
public function providerInvalidEmailUsername()
6189
{
6290
return [
6391
['username' => null],
@@ -116,11 +144,22 @@ public function testUserIsAuthenticated()
116144
$this->assertSame(1, $user->getId());
117145
}
118146

147+
public function testUserWithAlphanumUsernameIsAuthenticated()
148+
{
149+
$local_authorizer = new LocalAuthorizer(new Repository([
150+
'[email protected]' => new AuthenticatedUser(1, 'JohnDoe1983', 'John', 'password', true),
151+
]), AuthorizerInterface::USERNAME_FORMAT_ALPHANUM);
152+
153+
$user = $local_authorizer->verifyCredentials(['username' => 'JohnDoe1983', 'password' => 'password']);
154+
155+
$this->assertSame(1, $user->getId());
156+
}
157+
119158
public function testUserWithEmailUsernameIsAuthenticated()
120159
{
121160
$local_authorizer = new LocalAuthorizer(new Repository([
122161
'[email protected]' => new AuthenticatedUser(1, '[email protected]', 'John', 'password', true),
123-
]), true);
162+
]), AuthorizerInterface::USERNAME_FORMAT_EMAIL);
124163

125164
$user = $local_authorizer->verifyCredentials(['username' => '[email protected]', 'password' => 'password']);
126165

0 commit comments

Comments
 (0)