Skip to content

Commit 6254ce5

Browse files
committed
Merge pull request #5 from activecollab/authentication-improvement
Authentication improvement
2 parents 90bb8fc + 4e589a0 commit 6254ce5

36 files changed

+302
-211
lines changed

.php_cs.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
'standardize_not_equal',
3838
'ternary_spaces',
3939
'trim_array_spaces',
40-
'unused_use ',
40+
'unused_use',
4141
'whitespacy_lines',
4242
'ordered_use',
4343
'short_array_syntax',

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"require-dev": {
2525
"fabpot/php-cs-fixer": "^1.0",
2626
"phpunit/phpunit": "~5",
27-
"slim/slim": "3.0.0-RC2"
27+
"slim/slim": "~3.2"
2828
},
2929
"minimum-stability": "dev",
3030
"autoload": {
@@ -37,4 +37,4 @@
3737
"ActiveCollab\\Authentication\\Test\\": "test/src"
3838
}
3939
}
40-
}
40+
}

composer.lock

+9-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Adapter/Adapter.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace ActiveCollab\Authentication\Adapter;
1010

1111
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
12-
use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface as UserRepositoryInterface;
13-
use ActiveCollab\Authentication\Exception\InvalidAuthenticateRequest;
14-
use ActiveCollab\Authentication\Exception\InvalidPassword;
15-
use ActiveCollab\Authentication\Exception\UserNotFound;
12+
use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface;
13+
use ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException;
14+
use ActiveCollab\Authentication\Exception\InvalidPasswordException;
15+
use ActiveCollab\Authentication\Exception\UserNotFoundException;
1616
use Psr\Http\Message\ServerRequestInterface;
1717

1818
/**
@@ -32,32 +32,32 @@ protected function getAuthenticationCredentialsFromRequest(ServerRequestInterfac
3232
$credentials = $request->getParsedBody();
3333

3434
if (!is_array($credentials) || empty($credentials['username']) || ($check_password && empty($credentials['password']))) {
35-
throw new InvalidAuthenticateRequest();
35+
throw new InvalidAuthenticationRequestException();
3636
}
3737

3838
return $credentials;
3939
}
4040

4141
/**
42-
* @param UserRepositoryInterface $repository
42+
* @param RepositoryInterface $repository
4343
* @param array $credentials
4444
* @param bool $check_password
4545
* @return AuthenticatedUserInterface
4646
*/
47-
protected function getUserFromCredentials(UserRepositoryInterface $repository, array $credentials, $check_password = true)
47+
protected function getUserFromCredentials(RepositoryInterface $repository, array $credentials, $check_password = true)
4848
{
4949
$user = $repository->findByUsername($credentials['username']);
5050

5151
if (!$user) {
52-
throw new UserNotFound();
52+
throw new UserNotFoundException();
5353
}
5454

5555
if ($check_password && !$user->isValidPassword($credentials['password'])) {
56-
throw new InvalidPassword();
56+
throw new InvalidPasswordException();
5757
}
5858

5959
if (!$user->canAuthenticate()) {
60-
throw new UserNotFound();
60+
throw new UserNotFoundException();
6161
}
6262

6363
return $user;

src/Adapter/AdapterInterface.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace ActiveCollab\Authentication\Adapter;
1010

1111
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
12-
use ActiveCollab\Authentication\AuthenticationResultInterface;
12+
use ActiveCollab\Authentication\AuthenticationResult\AuthenticationResultInterface;
1313
use Psr\Http\Message\ServerRequestInterface;
1414

1515
/**
@@ -20,14 +20,14 @@ interface AdapterInterface
2020
/**
2121
* Initialize authentication layer and see if we have a user who's already logged in.
2222
*
23-
* @param ServerRequestInterface $request
24-
* @param null $authenticated_with
25-
* @return AuthenticatedUserInterface
23+
* @param ServerRequestInterface $request
24+
* @param null $authenticated_with
25+
* @return AuthenticatedUserInterface|null
2626
*/
2727
public function initialize(ServerRequestInterface $request, &$authenticated_with = null);
2828

2929
/**
30-
* Authenticate with given credential agains authentication source.
30+
* Authenticate with given credential against authentication source.
3131
*
3232
* @param ServerRequestInterface $request
3333
* @param bool $check_password

src/Adapter/BrowserSession.php

+25-23
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
namespace ActiveCollab\Authentication\Adapter;
1010

1111
use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface as UserRepositoryInterface;
12-
use ActiveCollab\Authentication\AuthenticationResultInterface;
13-
use ActiveCollab\Authentication\Exception\InvalidSession;
12+
use ActiveCollab\Authentication\AuthenticationResult\AuthenticationResultInterface;
13+
use ActiveCollab\Authentication\Exception\InvalidSessionException;
1414
use ActiveCollab\Authentication\Session\RepositoryInterface as SessionRepositoryInterface;
1515
use ActiveCollab\Authentication\Session\SessionInterface;
1616
use ActiveCollab\Cookies\CookiesInterface;
@@ -25,12 +25,12 @@ class BrowserSession extends Adapter
2525
/**
2626
* @var UserRepositoryInterface
2727
*/
28-
private $users_repository;
28+
private $user_repository;
2929

3030
/**
3131
* @var SessionRepositoryInterface
3232
*/
33-
private $sessions_repository;
33+
private $session_repository;
3434

3535
/**
3636
* @var CookiesInterface
@@ -43,19 +43,19 @@ class BrowserSession extends Adapter
4343
private $session_cookie_name;
4444

4545
/**
46-
* @param UserRepositoryInterface $users_repository
47-
* @param SessionRepositoryInterface $sessions_repository
46+
* @param UserRepositoryInterface $user_repository
47+
* @param SessionRepositoryInterface $session_repository
4848
* @param CookiesInterface $cookies
4949
* @param string $session_cookie_name
5050
*/
51-
public function __construct(UserRepositoryInterface $users_repository, SessionRepositoryInterface $sessions_repository, CookiesInterface $cookies, $session_cookie_name = 'sessid')
51+
public function __construct(UserRepositoryInterface $user_repository, SessionRepositoryInterface $session_repository, CookiesInterface $cookies, $session_cookie_name = 'sessid')
5252
{
5353
if (empty($session_cookie_name)) {
5454
throw new InvalidArgumentException('Session cookie name is required');
5555
}
5656

57-
$this->users_repository = $users_repository;
58-
$this->sessions_repository = $sessions_repository;
57+
$this->user_repository = $user_repository;
58+
$this->session_repository = $session_repository;
5959
$this->cookies = $cookies;
6060
$this->session_cookie_name = $session_cookie_name;
6161
}
@@ -65,32 +65,34 @@ public function __construct(UserRepositoryInterface $users_repository, SessionRe
6565
*/
6666
public function initialize(ServerRequestInterface $request, &$authenticated_with = null)
6767
{
68-
if ($session_id = $this->cookies->get($request, $this->session_cookie_name)) {
69-
$session = $this->sessions_repository->getById($session_id);
68+
$session_id = $this->cookies->get($request, $this->session_cookie_name);
7069

71-
if ($session instanceof SessionInterface) {
72-
if ($user = $session->getAuthenticatedUser($this->users_repository)) {
73-
$this->sessions_repository->recordUsage($session);
74-
$authenticated_with = $session;
70+
if (!$session_id) {
71+
return null;
72+
}
7573

76-
return $user;
77-
}
78-
}
74+
$session = $this->session_repository->getById($session_id);
7975

80-
throw new InvalidSession();
76+
if ($session instanceof SessionInterface) {
77+
if ($user = $session->getAuthenticatedUser($this->user_repository)) {
78+
$this->session_repository->recordUsageBySession($session);
79+
$authenticated_with = $session;
80+
81+
return $user;
82+
}
8183
}
8284

83-
return null;
85+
throw new InvalidSessionException();
8486
}
8587

8688
/**
8789
* {@inheritdoc}
8890
*/
8991
public function authenticate(ServerRequestInterface $request, $check_password = true)
9092
{
91-
return $this->sessions_repository->createSession(
93+
return $this->session_repository->createSession(
9294
$this->getUserFromCredentials(
93-
$this->users_repository,
95+
$this->user_repository,
9496
$this->getAuthenticationCredentialsFromRequest($request, $check_password),
9597
$check_password
9698
)
@@ -103,7 +105,7 @@ public function authenticate(ServerRequestInterface $request, $check_password =
103105
public function terminate(AuthenticationResultInterface $authenticated_with)
104106
{
105107
if ($authenticated_with instanceof SessionInterface) {
106-
$this->sessions_repository->terminateSession($authenticated_with);
108+
$this->session_repository->terminateSession($authenticated_with);
107109
} else {
108110
throw new InvalidArgumentException('Instance is not a browser session');
109111
}

src/Adapter/TokenBearer.php

+22-18
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
namespace ActiveCollab\Authentication\Adapter;
1010

1111
use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface as UserRepositoryInterface;
12-
use ActiveCollab\Authentication\AuthenticationResultInterface;
13-
use ActiveCollab\Authentication\Exception\InvalidToken;
12+
use ActiveCollab\Authentication\AuthenticationResult\AuthenticationResultInterface;
13+
use ActiveCollab\Authentication\Exception\InvalidTokenException;
1414
use ActiveCollab\Authentication\Token\RepositoryInterface as TokenRepositoryInterface;
1515
use ActiveCollab\Authentication\Token\TokenInterface;
1616
use InvalidArgumentException;
@@ -24,21 +24,21 @@ class TokenBearer extends Adapter
2424
/**
2525
* @var UserRepositoryInterface
2626
*/
27-
private $users_repository;
27+
private $user_repository;
2828

2929
/**
3030
* @var TokenRepositoryInterface
3131
*/
32-
private $tokens_repository;
32+
private $token_repository;
3333

3434
/**
35-
* @param UserRepositoryInterface $users_repository
36-
* @param TokenRepositoryInterface $tokens_repository
35+
* @param UserRepositoryInterface $user_repository
36+
* @param TokenRepositoryInterface $token_repository
3737
*/
38-
public function __construct(UserRepositoryInterface $users_repository, TokenRepositoryInterface $tokens_repository)
38+
public function __construct(UserRepositoryInterface $user_repository, TokenRepositoryInterface $token_repository)
3939
{
40-
$this->users_repository = $users_repository;
41-
$this->tokens_repository = $tokens_repository;
40+
$this->user_repository = $user_repository;
41+
$this->token_repository = $token_repository;
4242
}
4343

4444
/**
@@ -48,23 +48,23 @@ public function initialize(ServerRequestInterface $request, &$authenticated_with
4848
{
4949
$authorization = $request->getHeaderLine('Authorization');
5050

51-
if (!empty($authorization) && substr($authorization, 0, 7) == 'Bearer ') {
51+
if (!empty($authorization) && substr($authorization, 0, 7) === 'Bearer ') {
5252
$token_id = trim(substr($authorization, 7));
5353

54-
if (empty($token_id)) {
55-
throw new InvalidToken();
54+
if ($token_id === null || $token_id === '') {
55+
throw new InvalidTokenException();
5656
}
5757

58-
if ($token = $this->tokens_repository->getById($token_id)) {
59-
if ($user = $token->getAuthenticatedUser($this->users_repository)) {
60-
$this->tokens_repository->recordUsage($token);
58+
if ($token = $this->token_repository->getById($token_id)) {
59+
if ($user = $token->getAuthenticatedUser($this->user_repository)) {
60+
$this->token_repository->recordUsageByToken($token);
6161
$authenticated_with = $token;
6262

6363
return $user;
6464
}
6565
}
6666

67-
throw new InvalidToken();
67+
throw new InvalidTokenException();
6868
}
6969

7070
return null;
@@ -79,7 +79,11 @@ public function initialize(ServerRequestInterface $request, &$authenticated_with
7979
*/
8080
public function authenticate(ServerRequestInterface $request, $check_password = true)
8181
{
82-
return $this->tokens_repository->issueToken($this->getUserFromCredentials($this->users_repository, $this->getAuthenticationCredentialsFromRequest($request), $check_password));
82+
return $this->token_repository->issueToken($this->getUserFromCredentials(
83+
$this->user_repository,
84+
$this->getAuthenticationCredentialsFromRequest($request),
85+
$check_password
86+
));
8387
}
8488

8589
/**
@@ -90,7 +94,7 @@ public function authenticate(ServerRequestInterface $request, $check_password =
9094
public function terminate(AuthenticationResultInterface $authenticated_with)
9195
{
9296
if ($authenticated_with instanceof TokenInterface) {
93-
$this->tokens_repository->terminateToken($authenticated_with);
97+
$this->token_repository->terminateToken($authenticated_with);
9498
} else {
9599
throw new InvalidArgumentException('Instance is not a token');
96100
}

src/AuthenticatedUser/AuthenticatedUserInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use ActiveCollab\User\UserInterface;
1212

1313
/**
14-
* @package ActiveCollab\Authentication
14+
* @package ActiveCollab\Authentication\AuthenticatedUser
1515
*/
1616
interface AuthenticatedUserInterface extends UserInterface
1717
{

src/AuthenticationResultInterface.php renamed to src/AuthenticationResult/AuthenticationResultInterface.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@
66
* (c) A51 doo <[email protected]>. All rights reserved.
77
*/
88

9-
namespace ActiveCollab\Authentication;
9+
namespace ActiveCollab\Authentication\AuthenticationResult;
1010

1111
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
12-
use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface as UserRepositoryInterface;
12+
use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface;
13+
use JsonSerializable;
1314
use Psr\Http\Message\ResponseInterface;
1415

1516
/**
16-
* @package ActiveCollab\Authentication
17+
* @package ActiveCollab\Authentication\AuthenticationResult
1718
*/
18-
interface AuthenticationResultInterface extends \JsonSerializable
19+
interface AuthenticationResultInterface extends JsonSerializable
1920
{
2021
/**
2122
* Get authenticated user from the repository.
2223
*
23-
* @param UserRepositoryInterface $repository
24+
* @param RepositoryInterface $repository
2425
* @return AuthenticatedUserInterface
2526
*/
26-
public function getAuthenticatedUser(UserRepositoryInterface $repository);
27+
public function getAuthenticatedUser(RepositoryInterface $repository);
2728

2829
/**
2930
* @param ResponseInterface $response

0 commit comments

Comments
 (0)