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\OpenIdConnect\UserInfo\Claim; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccount; |
17
|
|
|
|
18
|
|
|
class ClaimSourceManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ClaimSource[] |
22
|
|
|
*/ |
23
|
|
|
private $claimSources = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param ClaimSource $claimSource |
27
|
|
|
* |
28
|
|
|
* @return ClaimSourceManager |
29
|
|
|
*/ |
30
|
|
|
public function add(ClaimSource $claimSource): self |
31
|
|
|
{ |
32
|
|
|
$this->claimSources[] = $claimSource; |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return ClaimSource[] |
39
|
|
|
*/ |
40
|
|
|
public function all(): array |
41
|
|
|
{ |
42
|
|
|
return $this->claimSources; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param UserAccount $userAccount |
47
|
|
|
* @param string $scope |
48
|
|
|
* @param array $previousClaims |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getUserInfo(UserAccount $userAccount, string $scope, array $previousClaims): array |
53
|
|
|
{ |
54
|
|
|
$scopes = empty($scope) ? [] : explode(' ', $scope); |
55
|
|
|
$claims = [ |
56
|
|
|
'_claim_names' => [], |
57
|
|
|
'_claim_sources' => [], |
58
|
|
|
]; |
59
|
|
|
$i = 0; |
60
|
|
|
|
61
|
|
|
foreach ($this->all() as $claimSource) { |
62
|
|
|
$result = $claimSource->getUserInfo($userAccount, $scopes, $previousClaims); |
63
|
|
|
if (null !== $result) { |
64
|
|
|
++$i; |
65
|
|
|
$src = sprintf('src%d', $i); |
66
|
|
|
$_claim_names = []; |
67
|
|
|
foreach ($result->getAvailableClaims() as $claim) { |
68
|
|
|
if ('sub' !== $claim) { |
69
|
|
|
$_claim_names[$claim] = $src; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
$claims['_claim_names'] = array_merge( |
73
|
|
|
$claims['_claim_names'], |
74
|
|
|
$_claim_names |
75
|
|
|
); |
76
|
|
|
$claims['_claim_sources'][$src] = $result->getSource(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return empty($claims['_claim_names']) ? [] : $previousClaims; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|