|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\AuthorizationEndpoint\UserAccountDiscovery; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\Authorization; |
|
17
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\RedirectToLoginPageException; |
|
18
|
|
|
|
|
19
|
|
|
class UserAccountDiscoveryManager |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var UserAccountDiscovery[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $extensions = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param UserAccountDiscovery $extension |
|
28
|
|
|
*/ |
|
29
|
|
|
public function add(UserAccountDiscovery $extension) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->extensions[] = $extension; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param Authorization $authorization |
|
36
|
|
|
* |
|
37
|
|
|
* @return Authorization |
|
38
|
|
|
* |
|
39
|
|
|
* @throws RedirectToLoginPageException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function find(Authorization $authorization): Authorization |
|
42
|
|
|
{ |
|
43
|
|
|
$userAccount = null; |
|
44
|
|
|
$isFullyAuthenticated = null; |
|
45
|
|
|
foreach ($this->extensions as $extension) { |
|
46
|
|
|
$tmpIsFullyAuthenticated = null; |
|
47
|
|
|
$tmpUserAccount = $extension->find($authorization, $tmpIsFullyAuthenticated); |
|
48
|
|
|
if (null !== $tmpUserAccount) { |
|
49
|
|
|
if (null === $userAccount) { |
|
50
|
|
|
$userAccount = $tmpUserAccount; |
|
51
|
|
|
$isFullyAuthenticated = $tmpIsFullyAuthenticated; |
|
52
|
|
|
} else { |
|
53
|
|
|
if ($tmpUserAccount->getPublicId()->getValue() !== $userAccount->getPublicId()->getValue()) { |
|
54
|
|
|
throw new RedirectToLoginPageException($authorization); |
|
55
|
|
|
} |
|
56
|
|
|
if (true === $tmpIsFullyAuthenticated) { |
|
57
|
|
|
$isFullyAuthenticated = $tmpIsFullyAuthenticated; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (null !== $userAccount) { |
|
64
|
|
|
$authorization = $authorization->withUserAccount($userAccount, $isFullyAuthenticated); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $authorization; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param Authorization $authorization |
|
72
|
|
|
*/ |
|
73
|
|
|
public function check(Authorization $authorization) |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($this->extensions as $extension) { |
|
76
|
|
|
$authorization = $extension->check($authorization); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.