Skip to content

Commit 5cd070d

Browse files
author
Florian Krämer
committed
Continue working on the base implementation
1 parent c74cda0 commit 5cd070d

File tree

16 files changed

+527
-194
lines changed

16 files changed

+527
-194
lines changed

Diff for: .editorconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 4
9+
end_of_line = lf
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
13+
[*.bat]
14+
end_of_line = crlf
15+
16+
[*.yml]
17+
indent_style = space
18+
indent_size = 2

Diff for: composer.json

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
{
2-
"name": "cakephp/middleware-authentication",
3-
"description": "Authentication for the middleware stack",
4-
"type": "cakephp-plugin",
5-
"require": {
6-
"cakephp/cakephp": "^3.3"
7-
},
8-
"require-dev": {
9-
"cakephp/cakephp-codesniffer": "^2.1",
10-
"phpunit/phpunit": "^5.5"
11-
},
12-
"license": "MIT",
13-
"minimum-stability": "dev",
14-
"autoload": {
15-
"psr-4": {
16-
"MiddlewareAuth\\": "src"
17-
}
18-
},
19-
"autoload-dev": {
20-
"psr-4": {
21-
"MiddlewareAuthTest\\": "tests/test_app/Plugin/MiddlewareAuthTest/src",
22-
"MiddlewareAuth\\Test\\": "tests",
23-
"MiddlewareAuth\\Test\\App\\": "tests/test_app/App",
24-
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests",
25-
"TestApp\\": "./vendor/cakephp/cakephp/tests/test_app/TestApp"
26-
}
27-
},
28-
"prefer-source": true
2+
"name": "cakephp/middleware-authentication",
3+
"description": "Authentication for the middleware stack",
4+
"type": "cakephp-plugin",
5+
"require": {
6+
"cakephp/cakephp": "^3.3"
7+
},
8+
"require-dev": {
9+
"cakephp/cakephp-codesniffer": "^2.1",
10+
"phpunit/phpunit": "^5.5"
11+
},
12+
"license": "MIT",
13+
"minimum-stability": "dev",
14+
"autoload": {
15+
"psr-4": {
16+
"MiddlewareAuth\\": "src"
17+
}
18+
},
19+
"autoload-dev": {
20+
"psr-4": {
21+
"MiddlewareAuthTest\\": "tests/test_app/Plugin/MiddlewareAuthTest/src",
22+
"MiddlewareAuth\\Test\\": "tests",
23+
"MiddlewareAuth\\Test\\App\\": "tests/test_app/App",
24+
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests",
25+
"TestApp\\": "./vendor/cakephp/cakephp/tests/test_app/TestApp"
26+
}
27+
},
28+
"prefer-source": true
2929
}

Diff for: src/Routing/Middleware/Authentication/AbstractAuthenticator.php renamed to src/Auth/Authentication/AbstractAuthenticator.php

+6-54
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@
88
*
99
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
1010
* @link http://cakephp.org CakePHP(tm) Project
11-
* @since 2.0.0
11+
* @since 4.0.0
1212
* @license http://www.opensource.org/licenses/mit-license.php MIT License
1313
*/
14-
namespace MiddlewareAuth\Routing\Middleware\Authentication;
14+
namespace MiddlewareAuth\Auth\Authentication;
1515

16-
use Cake\Auth\PasswordHasherFactory;
1716
use Cake\Core\InstanceConfigTrait;
1817
use Cake\ORM\TableRegistry;
18+
use MiddlewareAuth\Auth\PasswordHasherTrait;
1919
use Psr\Http\Message\ResponseInterface;
2020
use Psr\Http\Message\ServerRequestInterface;
2121

22-
abstract class AbstractAuthenticator {
22+
abstract class AbstractAuthenticator implements AuthenticateInterface {
2323

2424
use InstanceConfigTrait;
25+
use PasswordHasherTrait;
2526

2627
/**
2728
* Default config for this object.
@@ -51,28 +52,6 @@ abstract class AbstractAuthenticator {
5152
'passwordHasher' => 'Default'
5253
];
5354

54-
/**
55-
* A Component registry, used to get more components.
56-
*
57-
* @var \Cake\Controller\ComponentRegistry
58-
*/
59-
protected $_registry;
60-
61-
/**
62-
* Password hasher instance.
63-
*
64-
* @var \Cake\Auth\AbstractPasswordHasher
65-
*/
66-
protected $_passwordHasher;
67-
68-
/**
69-
* Whether or not the user authenticated by this class
70-
* requires their password to be rehashed with another algorithm.
71-
*
72-
* @var bool
73-
*/
74-
protected $_needsPasswordRehash = false;
75-
7655
/**
7756
* Constructor
7857
*
@@ -149,33 +128,6 @@ protected function _query($username) {
149128
return $query;
150129
}
151130

152-
/**
153-
* Return password hasher object
154-
*
155-
* @return \Cake\Auth\AbstractPasswordHasher Password hasher instance
156-
* @throws \RuntimeException If password hasher class not found or
157-
* it does not extend AbstractPasswordHasher
158-
*/
159-
public function passwordHasher() {
160-
if ($this->_passwordHasher) {
161-
return $this->_passwordHasher;
162-
}
163-
164-
$passwordHasher = $this->_config['passwordHasher'];
165-
166-
return $this->_passwordHasher = PasswordHasherFactory::build($passwordHasher);
167-
}
168-
169-
/**
170-
* Returns whether or not the password stored in the repository for the logged in user
171-
* requires to be rehashed with another algorithm
172-
*
173-
* @return bool
174-
*/
175-
public function needsPasswordRehash() {
176-
return $this->_needsPasswordRehash;
177-
}
178-
179131
/**
180132
* Authenticate a user based on the request information.
181133
*
@@ -226,4 +178,4 @@ public function unauthenticated(ResponseInterface $request, ServerRequestInterfa
226178
public function implementedEvents() {
227179
return [];
228180
}
229-
}
181+
}

Diff for: src/Auth/Authentication/AuthenticateInterface.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
10+
* @link http://cakephp.org CakePHP(tm) Project
11+
* @since 4.0.0
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
namespace MiddlewareAuth\Auth\Authentication;
15+
16+
use Psr\Http\Message\ResponseInterface;
17+
use Psr\Http\Message\ServerRequestInterface;
18+
19+
interface AuthenticateInterface
20+
{
21+
/**
22+
* Authenticate user.
23+
*
24+
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
25+
* @param \Psr\Http\Message\ResponseInterface $response The response.
26+
* @return \Cake\Auth\Result
27+
*/
28+
public function authenticate(ServerRequestInterface $request, ResponseInterface $response);
29+
}

Diff for: src/Routing/Middleware/Authentication/FormAuthenticator.php renamed to src/Auth/Authentication/FormAuthenticator.php

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
11
<?php
22
/**
3-
*
43
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
54
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
6-
*
75
* Licensed under The MIT License
86
* For full copyright and license information, please see the LICENSE.txt
97
* Redistributions of files must retain the above copyright notice.
108
*
119
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
1210
* @link http://cakephp.org CakePHP(tm) Project
13-
* @since 2.0.0
11+
* @since 4.0.0
1412
* @license http://www.opensource.org/licenses/mit-license.php MIT License
1513
*/
16-
namespace MiddlewareAuth\Routing\Middleware\Authentication;
14+
namespace MiddlewareAuth\Auth\Authentication;
1715

1816
use Psr\Http\Message\ResponseInterface;
1917
use Psr\Http\Message\ServerRequestInterface;
2018

2119
/**
22-
* An authentication adapter for AuthComponent. Provides the ability to authenticate using POST
23-
* data. Can be used by configuring AuthComponent to use it via the AuthComponent::$authenticate config.
24-
*
25-
* ```
26-
* $this->Auth->authenticate = [
27-
* 'Form' => [
28-
* 'finder' => ['auth' => ['some_finder_option' => 'some_value']]
29-
* ]
30-
* ]
31-
* ```
32-
*
33-
* When configuring FormAuthenticate you can pass in config to which fields, model and additional conditions
34-
* are used. See FormAuthenticate::$_config for more information.
3520
*
36-
* @see \Cake\Controller\Component\AuthComponent::$authenticate
3721
*/
3822
class FormAuthenticator extends AbstractAuthenticator
3923
{
@@ -77,9 +61,15 @@ public function authenticate(ServerRequestInterface $request, ResponseInterface
7761

7862
$body = $request->getParsedBody();
7963

80-
return $this->_findUser(
64+
$user = $this->_findUser(
8165
$body[$fields['username']],
8266
$body[$fields['password']]
8367
);
68+
69+
if (empty($user)) {
70+
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND);
71+
}
72+
73+
return new Result($user, Result::SUCCESS);
8474
}
85-
}
75+
}

Diff for: src/Auth/Authentication/Result.php

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
10+
* @link http://cakephp.org CakePHP(tm) Project
11+
* @since 4.0.0
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
namespace MiddlewareAuth\Auth\Authentication;
15+
16+
/**
17+
* Authentication result object
18+
*/
19+
class Result
20+
{
21+
/**
22+
* General Failure
23+
*/
24+
const FAILURE = 0;
25+
26+
/**
27+
* Failure due to identity not being found.
28+
*/
29+
const FAILURE_IDENTITY_NOT_FOUND = -1;
30+
31+
/**
32+
* Failure due to invalid credential being supplied.
33+
*/
34+
const FAILURE_CREDENTIAL_INVALID = -2;
35+
36+
/**
37+
* Failure due to other circumstances.
38+
*/
39+
const FAILURE_OTHER = -3;
40+
41+
/**
42+
* Authentication success.
43+
*/
44+
const SUCCESS = 1;
45+
46+
/**
47+
* Authentication result code
48+
*
49+
* @var int
50+
*/
51+
protected $code;
52+
53+
/**
54+
* The identity used in the authentication attempt
55+
*
56+
* @var mixed
57+
*/
58+
protected $identity;
59+
60+
/**
61+
* An array of string reasons why the authentication attempt was unsuccessful
62+
*
63+
* If authentication was successful, this should be an empty array.
64+
*
65+
* @var array
66+
*/
67+
protected $messages;
68+
69+
/**
70+
* Sets the result code, identity, and failure messages
71+
*
72+
* @param mixed $identity
73+
* @param int $code
74+
* @param array $messages
75+
*/
76+
public function __construct($identity, $code, array $messages = [])
77+
{
78+
$this->code = (int)$code;
79+
$this->identity = $identity;
80+
$this->messages = $messages;
81+
}
82+
83+
/**
84+
* Returns whether the result represents a successful authentication attempt.
85+
*
86+
* @return bool
87+
*/
88+
public function isValid()
89+
{
90+
return $this->code > 0;
91+
}
92+
93+
/**
94+
* Get the result code for this authentication attempt.
95+
*
96+
* @return int
97+
*/
98+
public function getCode()
99+
{
100+
return $this->code;
101+
}
102+
103+
/**
104+
* Returns the identity used in the authentication attempt.
105+
*
106+
* @return mixed
107+
*/
108+
public function getIdentity()
109+
{
110+
return $this->identity;
111+
}
112+
113+
/**
114+
* Returns an array of string reasons why the authentication attempt was unsuccessful.
115+
*
116+
* If authentication was successful, this method returns an empty array.
117+
*
118+
* @return array
119+
*/
120+
public function getMessages()
121+
{
122+
return $this->messages;
123+
}
124+
}

Diff for: src/Routing/Middleware/Authentication/Storage/MemoryStorage.php renamed to src/Auth/Authentication/Storage/MemoryStorage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @since 3.1.0
1313
* @license http://www.opensource.org/licenses/mit-license.php MIT License
1414
*/
15-
namespace MiddlewareAuth\Routing\Middleware\Authentication\Storage;
15+
namespace MiddlewareAuth\Auth\Authentication\Storage;
1616

1717
/**
1818
* Memory based non-persistent storage for authenticated user record.

0 commit comments

Comments
 (0)