|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
|
5
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
|
6
|
|
|
* |
|
7
|
|
|
* Licensed under The MIT License |
|
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
|
12
|
|
|
* @link https://cakephp.org CakePHP(tm) Project |
|
13
|
|
|
* @since 1.0.0 |
|
14
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
declare(strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace Phauthentic\Authentication\Authenticator; |
|
20
|
|
|
|
|
21
|
|
|
use Phauthentic\Authentication\UrlChecker\UrlCheckerInterface; |
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
23
|
|
|
|
|
24
|
|
|
trait UrlAwareTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Url Checker |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Phauthentic\Authentication\UrlChecker\UrlCheckerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected UrlCheckerInterface $urlChecker; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Login URLs |
|
35
|
|
|
* |
|
36
|
|
|
* @var string[] |
|
37
|
|
|
*/ |
|
38
|
|
|
protected array $loginUrls = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Sets multiple login URLs. |
|
42
|
|
|
* |
|
43
|
8 |
|
* @param array<int, string> $urls An array of URLs. |
|
44
|
|
|
* @return $this |
|
45
|
8 |
|
*/ |
|
46
|
|
|
public function setLoginUrls(array $urls): self |
|
47
|
8 |
|
{ |
|
48
|
|
|
$this->loginUrls = $urls; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Adds a login URL. |
|
55
|
|
|
* |
|
56
|
32 |
|
* @param string $url Login URL. |
|
57
|
|
|
* @return $this |
|
58
|
32 |
|
*/ |
|
59
|
|
|
public function addLoginUrl(string $url): self |
|
60
|
32 |
|
{ |
|
61
|
|
|
$this->loginUrls[] = $url; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* URL checker wrapper for multiple URLs. |
|
68
|
|
|
* Returns true if there are no URLs configured. |
|
69
|
|
|
* |
|
70
|
84 |
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request. |
|
71
|
|
|
* @return bool |
|
72
|
84 |
|
*/ |
|
73
|
44 |
|
protected function checkUrl(ServerRequestInterface $request): bool |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$this->loginUrls) { |
|
|
|
|
|
|
76
|
40 |
|
return true; |
|
77
|
40 |
|
} |
|
78
|
36 |
|
|
|
79
|
|
|
foreach ($this->loginUrls as $url) { |
|
80
|
|
|
if ($this->urlChecker->check($request, $url)) { |
|
81
|
|
|
return true; |
|
82
|
16 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.