Skip to content

Commit df91a7b

Browse files
committed
Added Environment authenticator
Removed field mapping, added required and optional fields Corrections from remarks Removed unecessary annotations, cs-fix
1 parent 017ad3b commit df91a7b

27 files changed

+827
-119
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* For full copyright and license information, please see the LICENSE.txt
10+
* Redistributions of files must retain the above copyright notice.
11+
*
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @since 2.10.0
15+
* @license https://opensource.org/licenses/mit-license.php MIT License
16+
*/
17+
namespace Authentication\Authenticator;
18+
19+
use Authentication\UrlChecker\UrlCheckerTrait;
20+
use Psr\Http\Message\ServerRequestInterface;
21+
22+
/**
23+
* Environment Authenticator
24+
*
25+
* Authenticates an identity based on the POST data of the request.
26+
*/
27+
class EnvironmentAuthenticator extends AbstractAuthenticator
28+
{
29+
use UrlCheckerTrait;
30+
31+
/**
32+
* Default config for this object.
33+
* - `loginUrl` Login URL or an array of URLs.
34+
* - `urlChecker` Url checker config.
35+
* - `fields` array of required fields to get from the environment
36+
* - `optional_fields` array of optional fields to get from the environment
37+
*
38+
* @var array
39+
*/
40+
protected $_defaultConfig = [
41+
'loginUrl' => null,
42+
'urlChecker' => 'Authentication.Default',
43+
'fields' => [],
44+
'optional_fields' => [],
45+
];
46+
47+
/**
48+
* Get values from the environment variables configured by `fields`.
49+
*
50+
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
51+
* @return array|null server params defined by `fields` or null if a field is missing.
52+
*/
53+
protected function _getData(ServerRequestInterface $request): ?array
54+
{
55+
$fields = $this->_config['fields'];
56+
$params = $request->getServerParams();
57+
58+
$data = [];
59+
foreach ($fields as $field) {
60+
if (!isset($params[$field])) {
61+
return null;
62+
}
63+
64+
$value = $params[$field];
65+
if (!is_string($value) || !strlen($value)) {
66+
return null;
67+
}
68+
69+
$data[$field] = $value;
70+
}
71+
72+
return $data;
73+
}
74+
75+
/**
76+
* Get values from the environment variables configured by `optional_fields`.
77+
*
78+
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
79+
* @return array server params defined by optional_fields.
80+
*/
81+
protected function _getOptionalData(ServerRequestInterface $request): array
82+
{
83+
$fields = $this->_config['optional_fields'];
84+
$params = $request->getServerParams();
85+
86+
$data = [];
87+
foreach ($fields as $field) {
88+
if (isset($params[$field])) {
89+
$data[$field] = $params[$field];
90+
}
91+
}
92+
93+
return $data;
94+
}
95+
96+
/**
97+
* Prepares the error object for a login URL error
98+
*
99+
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
100+
* @return \Authentication\Authenticator\ResultInterface
101+
*/
102+
protected function _buildLoginUrlErrorResult(ServerRequestInterface $request): ResultInterface
103+
{
104+
$uri = $request->getUri();
105+
$base = $request->getAttribute('base');
106+
if ($base !== null) {
107+
$uri = $uri->withPath((string)$base . $uri->getPath());
108+
}
109+
110+
$checkFullUrl = $this->getConfig('urlChecker.checkFullUrl', false);
111+
if ($checkFullUrl) {
112+
$uri = (string)$uri;
113+
} else {
114+
$uri = $uri->getPath();
115+
}
116+
117+
$errors = [
118+
sprintf(
119+
'Login URL `%s` did not match `%s`.',
120+
$uri,
121+
implode('` or `', (array)$this->getConfig('loginUrl'))
122+
),
123+
];
124+
125+
return new Result(null, Result::FAILURE_OTHER, $errors);
126+
}
127+
128+
/**
129+
* Authenticates the identity contained in a request.
130+
*
131+
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
132+
* @return \Authentication\Authenticator\ResultInterface
133+
*/
134+
public function authenticate(ServerRequestInterface $request): ResultInterface
135+
{
136+
if (!$this->_checkUrl($request)) {
137+
return $this->_buildLoginUrlErrorResult($request);
138+
}
139+
$data = $this->_getData($request);
140+
if (empty($data)) {
141+
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING, [
142+
'Environment credentials not found',
143+
]);
144+
}
145+
146+
$data = array_merge($this->_getOptionalData($request), $data);
147+
148+
$user = $this->_identifier->identify($data);
149+
150+
if (empty($user)) {
151+
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identifier->getErrors());
152+
}
153+
154+
return new Result($user, Result::SUCCESS);
155+
}
156+
}

src/Authenticator/HttpBasicAuthenticator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
declare(strict_types=1);
33

44
/**
5-
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6-
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
77
*
88
* Licensed under The MIT License
99
* For full copyright and license information, please see the LICENSE.txt
1010
* Redistributions of files must retain the above copyright notice.
1111
*
12-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13-
* @link http://cakephp.org CakePHP(tm) Project
14-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
1515
*/
1616
namespace Authentication\Authenticator;
1717

src/Authenticator/HttpDigestAuthenticator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
declare(strict_types=1);
33

44
/**
5-
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6-
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
77
*
88
* Licensed under The MIT License
99
* For full copyright and license information, please see the LICENSE.txt
1010
* Redistributions of files must retain the above copyright notice.
1111
*
12-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13-
* @link http://cakephp.org CakePHP(tm) Project
14-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
1515
*/
1616
namespace Authentication\Authenticator;
1717

src/Authenticator/SessionAuthenticator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
declare(strict_types=1);
33

44
/**
5-
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6-
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
77
*
88
* Licensed under The MIT License
99
* For full copyright and license information, please see the LICENSE.txt
1010
* Redistributions of files must retain the above copyright notice.
1111
*
12-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13-
* @link http://cakephp.org CakePHP(tm) Project
14-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
1515
*/
1616
namespace Authentication\Authenticator;
1717

src/Identity.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function get($field)
124124
/**
125125
* Whether a offset exists
126126
*
127-
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
127+
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
128128
* @param mixed $offset Offset
129129
* @return bool
130130
*/
@@ -136,7 +136,7 @@ public function offsetExists($offset): bool
136136
/**
137137
* Offset to retrieve
138138
*
139-
* @link http://php.net/manual/en/arrayaccess.offsetget.php
139+
* @link https://php.net/manual/en/arrayaccess.offsetget.php
140140
* @param mixed $offset Offset
141141
* @return mixed
142142
*/
@@ -149,7 +149,7 @@ public function offsetGet($offset)
149149
/**
150150
* Offset to set
151151
*
152-
* @link http://php.net/manual/en/arrayaccess.offsetset.php
152+
* @link https://php.net/manual/en/arrayaccess.offsetset.php
153153
* @param mixed $offset The offset to assign the value to.
154154
* @param mixed $value Value
155155
* @throws \BadMethodCallException
@@ -164,7 +164,7 @@ public function offsetSet($offset, $value)
164164
/**
165165
* Offset to unset
166166
*
167-
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
167+
* @link https://php.net/manual/en/arrayaccess.offsetunset.php
168168
* @param mixed $offset Offset
169169
* @throws \BadMethodCallException
170170
* @return void

src/PasswordHasher/AbstractPasswordHasher.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
declare(strict_types=1);
33

44
/**
5-
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6-
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
77
*
88
* Licensed under The MIT License
99
* For full copyright and license information, please see the LICENSE.txt
1010
* Redistributions of files must retain the above copyright notice.
1111
*
12-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13-
* @link http://cakephp.org CakePHP(tm) Project
14-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
1515
*/
1616
namespace Authentication\PasswordHasher;
1717

src/PasswordHasher/DefaultPasswordHasher.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
declare(strict_types=1);
33

44
/**
5-
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6-
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
77
*
88
* Licensed under The MIT License
99
* For full copyright and license information, please see the LICENSE.txt
1010
* Redistributions of files must retain the above copyright notice.
1111
*
12-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13-
* @link http://cakephp.org CakePHP(tm) Project
14-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
1515
*/
1616
namespace Authentication\PasswordHasher;
1717

src/PasswordHasher/FallbackPasswordHasher.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
declare(strict_types=1);
33

44
/**
5-
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6-
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
77
*
88
* Licensed under The MIT License
99
* For full copyright and license information, please see the LICENSE.txt
1010
* Redistributions of files must retain the above copyright notice.
1111
*
12-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13-
* @link http://cakephp.org CakePHP(tm) Project
14-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
1515
*/
1616
namespace Authentication\PasswordHasher;
1717

src/PasswordHasher/LegacyPasswordHasher.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
declare(strict_types=1);
33

44
/**
5-
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6-
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
77
*
88
* Licensed under The MIT License
99
* For full copyright and license information, please see the LICENSE.txt
1010
* Redistributions of files must retain the above copyright notice.
1111
*
12-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13-
* @link http://cakephp.org CakePHP(tm) Project
14-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
1515
*/
1616
namespace Authentication\PasswordHasher;
1717

src/PasswordHasher/PasswordHasherFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
declare(strict_types=1);
33

44
/**
5-
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6-
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
77
*
88
* Licensed under The MIT License
99
* For full copyright and license information, please see the LICENSE.txt
1010
* Redistributions of files must retain the above copyright notice.
1111
*
12-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13-
* @link http://cakephp.org CakePHP(tm) Project
14-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
1515
*/
1616
namespace Authentication\PasswordHasher;
1717

0 commit comments

Comments
 (0)