Skip to content

Commit 11a71ed

Browse files
committed
Merge pull request #6 from activecollab/dev
Refactor component for better usage in new applications
2 parents 6254ce5 + 331cffc commit 11a71ed

31 files changed

+1343
-481
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"php": ">=5.6.0",
2020
"activecollab/user": "~1.0",
2121
"activecollab/cookies": "~0.1",
22-
"guzzlehttp/psr7": "~1.2"
22+
"guzzlehttp/psr7": "~1.2",
23+
"google/apiclient": "^1.1"
2324
},
2425
"require-dev": {
2526
"fabpot/php-cs-fixer": "^1.0",

composer.lock

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

src/Adapter/Adapter.php

-65
This file was deleted.

src/Adapter/AdapterInterface.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ 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|null
23+
* @param ServerRequestInterface $request
24+
* @return array|null Example:['authenticated_user' => AuthenticatedUserInterface, 'authenticated_with' => AuthenticationResultInterface];
2625
*/
27-
public function initialize(ServerRequestInterface $request, &$authenticated_with = null);
26+
public function initialize(ServerRequestInterface $request);
2827

2928
/**
30-
* Authenticate with given credential against authentication source.
29+
* Authenticate user against authentication source.
3130
*
32-
* @param ServerRequestInterface $request
33-
* @param bool $check_password
31+
* @param AuthenticatedUserInterface $authenticated_user
3432
* @return AuthenticationResultInterface
3533
*/
36-
public function authenticate(ServerRequestInterface $request, $check_password = true);
34+
public function authenticate(AuthenticatedUserInterface $authenticated_user);
3735

3836
/**
3937
* Terminate an instance that was used to authenticate a user.

src/Adapter/BrowserSession.php

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

99
namespace ActiveCollab\Authentication\Adapter;
1010

11+
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
1112
use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface as UserRepositoryInterface;
1213
use ActiveCollab\Authentication\AuthenticationResult\AuthenticationResultInterface;
1314
use ActiveCollab\Authentication\Exception\InvalidSessionException;
@@ -20,7 +21,7 @@
2021
/**
2122
* @package ActiveCollab\Authentication\Adapter
2223
*/
23-
class BrowserSession extends Adapter
24+
class BrowserSession implements AdapterInterface
2425
{
2526
/**
2627
* @var UserRepositoryInterface
@@ -63,7 +64,7 @@ public function __construct(UserRepositoryInterface $user_repository, SessionRep
6364
/**
6465
* {@inheritdoc}
6566
*/
66-
public function initialize(ServerRequestInterface $request, &$authenticated_with = null)
67+
public function initialize(ServerRequestInterface $request)
6768
{
6869
$session_id = $this->cookies->get($request, $this->session_cookie_name);
6970

@@ -76,9 +77,8 @@ public function initialize(ServerRequestInterface $request, &$authenticated_with
7677
if ($session instanceof SessionInterface) {
7778
if ($user = $session->getAuthenticatedUser($this->user_repository)) {
7879
$this->session_repository->recordUsageBySession($session);
79-
$authenticated_with = $session;
8080

81-
return $user;
81+
return ['authenticated_user' => $user, 'authenticated_with' => $session];
8282
}
8383
}
8484

@@ -88,15 +88,9 @@ public function initialize(ServerRequestInterface $request, &$authenticated_with
8888
/**
8989
* {@inheritdoc}
9090
*/
91-
public function authenticate(ServerRequestInterface $request, $check_password = true)
91+
public function authenticate(AuthenticatedUserInterface $authenticated_user)
9292
{
93-
return $this->session_repository->createSession(
94-
$this->getUserFromCredentials(
95-
$this->user_repository,
96-
$this->getAuthenticationCredentialsFromRequest($request, $check_password),
97-
$check_password
98-
)
99-
);
93+
return $this->session_repository->createSession($authenticated_user);
10094
}
10195

10296
/**

src/Adapter/TokenBearer.php

+20-30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace ActiveCollab\Authentication\Adapter;
1010

11+
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
1112
use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface as UserRepositoryInterface;
1213
use ActiveCollab\Authentication\AuthenticationResult\AuthenticationResultInterface;
1314
use ActiveCollab\Authentication\Exception\InvalidTokenException;
@@ -19,7 +20,7 @@
1920
/**
2021
* @package ActiveCollab\Authentication\Adapter
2122
*/
22-
class TokenBearer extends Adapter
23+
class TokenBearer implements AdapterInterface
2324
{
2425
/**
2526
* @var UserRepositoryInterface
@@ -44,52 +45,41 @@ public function __construct(UserRepositoryInterface $user_repository, TokenRepos
4445
/**
4546
* {@inheritdoc}
4647
*/
47-
public function initialize(ServerRequestInterface $request, &$authenticated_with = null)
48+
public function initialize(ServerRequestInterface $request)
4849
{
4950
$authorization = $request->getHeaderLine('Authorization');
5051

51-
if (!empty($authorization) && substr($authorization, 0, 7) === 'Bearer ') {
52-
$token_id = trim(substr($authorization, 7));
52+
if (empty($authorization) || substr($authorization, 0, 7) !== 'Bearer ') {
53+
return null;
54+
}
5355

54-
if ($token_id === null || $token_id === '') {
55-
throw new InvalidTokenException();
56-
}
56+
$token_id = trim(substr($authorization, 7));
5757

58-
if ($token = $this->token_repository->getById($token_id)) {
59-
if ($user = $token->getAuthenticatedUser($this->user_repository)) {
60-
$this->token_repository->recordUsageByToken($token);
61-
$authenticated_with = $token;
58+
if ($token_id === null || $token_id === '') {
59+
throw new InvalidTokenException();
60+
}
6261

63-
return $user;
64-
}
65-
}
62+
if ($token = $this->token_repository->getById($token_id)) {
63+
if ($user = $token->getAuthenticatedUser($this->user_repository)) {
64+
$this->token_repository->recordUsageByToken($token);
6665

67-
throw new InvalidTokenException();
66+
return ['authenticated_user' => $user, 'authenticated_with' => $token];
67+
}
6868
}
6969

70-
return null;
70+
throw new InvalidTokenException();
7171
}
7272

7373
/**
74-
* Authenticate with given credential agains authentication source.
75-
*
76-
* @param ServerRequestInterface $request
77-
* @param bool $check_password
78-
* @return AuthenticationResultInterface
74+
* {@inheritdoc}
7975
*/
80-
public function authenticate(ServerRequestInterface $request, $check_password = true)
76+
public function authenticate(AuthenticatedUserInterface $authenticated_user)
8177
{
82-
return $this->token_repository->issueToken($this->getUserFromCredentials(
83-
$this->user_repository,
84-
$this->getAuthenticationCredentialsFromRequest($request),
85-
$check_password
86-
));
78+
return $this->token_repository->issueToken($authenticated_user);
8779
}
8880

8981
/**
90-
* Terminate an instance that was used to authenticate a user.
91-
*
92-
* @param AuthenticationResultInterface $authenticated_with
82+
* {@inheritdoc}
9383
*/
9484
public function terminate(AuthenticationResultInterface $authenticated_with)
9585
{

src/Authentication.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Active Collab Authentication project.
5+
*
6+
* (c) A51 doo <[email protected]>. All rights reserved.
7+
*/
8+
9+
namespace ActiveCollab\Authentication;
10+
11+
use ActiveCollab\Authentication\Adapter\AdapterInterface;
12+
use ActiveCollab\Authentication\Authorizer\AuthorizerInterface;
13+
use ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException;
14+
use Exception;
15+
use Psr\Http\Message\RequestInterface;
16+
use RuntimeException;
17+
18+
class Authentication implements AuthenticationInterface
19+
{
20+
/**
21+
* @var array
22+
*/
23+
private $adapters;
24+
25+
/**
26+
* @param array $adapters
27+
*/
28+
public function __construct(array $adapters)
29+
{
30+
foreach ($adapters as $adapter) {
31+
if (!($adapter instanceof AdapterInterface)) {
32+
throw new RuntimeException('Invalid object type provided');
33+
}
34+
}
35+
36+
$this->adapters = $adapters;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function initialize(RequestInterface $request)
43+
{
44+
$exception = null;
45+
$results = ['authenticated_user' => [], 'authenticated_with' => []];
46+
47+
foreach ($this->adapters as $adapter) {
48+
try {
49+
$result = $adapter->initialize($request);
50+
if ($result) {
51+
$results['authenticated_user'][] = $result['authenticated_user'];
52+
$results['authenticated_with'][] = $result['authenticated_with'];
53+
}
54+
} catch (Exception $e) {
55+
$exception = $e;
56+
}
57+
}
58+
59+
if (empty($results['authenticated_user'])) {
60+
if ($exception) {
61+
throw $exception;
62+
}
63+
64+
return $request;
65+
}
66+
67+
if (count($results['authenticated_user']) > 1) {
68+
throw new InvalidAuthenticationRequestException('You can not be authenticated with more than one authentication method');
69+
}
70+
71+
return $request
72+
->withAttribute('authenticated_user', $results['authenticated_user'][0])
73+
->withAttribute('authenticated_with', $results['authenticated_with'][0]);
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function authorize(AuthorizerInterface $authorizer, AdapterInterface $adapter, array $credentials)
80+
{
81+
$user = $authorizer->verifyCredentials($credentials);
82+
83+
return $adapter->authenticate($user);
84+
}
85+
}

src/AuthenticationInterface.php

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

99
namespace ActiveCollab\Authentication;
1010

11+
use ActiveCollab\Authentication\Adapter\AdapterInterface;
1112
use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface;
13+
use ActiveCollab\Authentication\Authorizer\AuthorizerInterface;
1214
use Psr\Http\Message\RequestInterface;
1315

1416
/**
@@ -19,16 +21,18 @@ interface AuthenticationInterface
1921
/**
2022
* Initialize authentication layer and see if we have a user who's already logged in.
2123
*
22-
* @param RequestInterface $request
23-
* @return AuthenticatedUserInterface|null
24+
* @param RequestInterface $request
25+
* @return RequestInterface
2426
*/
2527
public function initialize(RequestInterface $request);
2628

2729
/**
28-
* Authenticate with given credential agains authentication source.
30+
* Authorize and authenticate with given credentials against authorization/authentication source.
2931
*
30-
* @param RequestInterface $request
32+
* @param AuthorizerInterface $authorizer
33+
* @param AdapterInterface $adapter
34+
* @param array $credentials
3135
* @return AuthenticatedUserInterface
3236
*/
33-
public function authenticate(RequestInterface $request);
37+
public function authorize(AuthorizerInterface $authorizer, AdapterInterface $adapter, array $credentials);
3438
}

0 commit comments

Comments
 (0)