Skip to content

Commit 430454e

Browse files
authored
Merge pull request #565 from MolbioUnige/feature/environmentAuthenticator
EnvironmentAuthenticator
2 parents f2c57de + fdce4d2 commit 430454e

File tree

2 files changed

+708
-0
lines changed

2 files changed

+708
-0
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+
* - `optionalFields` 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+
'optionalFields' => [],
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 `optionalFields`.
77+
*
78+
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
79+
* @return array server params defined by optionalFields.
80+
*/
81+
protected function _getOptionalData(ServerRequestInterface $request): array
82+
{
83+
$fields = $this->_config['optionalFields'];
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+
}

0 commit comments

Comments
 (0)