|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2016 SURFnet B.V. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace OpenConext\EngineBlockApiClientBundle\Service; |
|
20
|
|
|
|
|
21
|
|
|
use OpenConext\EngineBlockApiClientBundle\Exception\InvalidResponseException; |
|
22
|
|
|
use OpenConext\EngineBlockApiClientBundle\Http\JsonApiClient; |
|
23
|
|
|
use OpenConext\Profile\Value\Consent; |
|
24
|
|
|
use OpenConext\Profile\Value\ConsentList; |
|
25
|
|
|
use OpenConext\Profile\Value\SpecifiedConsent; |
|
26
|
|
|
use OpenConext\Profile\Value\SpecifiedConsentList; |
|
27
|
|
|
use OpenConext\ProfileBundle\Attribute\AttributeSetWithFallbacks; |
|
28
|
|
|
use stdClass; |
|
29
|
|
|
use Surfnet\SamlBundle\Exception\UnknownUrnException; |
|
30
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\Attribute; |
|
31
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDefinition; |
|
32
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDictionary; |
|
33
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\AttributeSetInterface; |
|
34
|
|
|
|
|
35
|
|
|
final class AttributeReleasePolicyService |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var JsonApiClient |
|
39
|
|
|
*/ |
|
40
|
|
|
private $jsonApiClient; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var AttributeDictionary |
|
44
|
|
|
*/ |
|
45
|
|
|
private $attributeDictionary; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct(JsonApiClient $jsonApiClient, AttributeDictionary $attributeDictionary) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->jsonApiClient = $jsonApiClient; |
|
50
|
|
|
$this->attributeDictionary = $attributeDictionary; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param ConsentList $consentList |
|
55
|
|
|
* @param AttributeSetInterface $attributeSet |
|
56
|
|
|
* @return SpecifiedConsentList |
|
57
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity, PHPMD.CouplingBetweenObjects) Build and mapping logic causes complexity |
|
58
|
|
|
*/ |
|
59
|
|
|
public function applyAttributeReleasePolicies(ConsentList $consentList, AttributeSetInterface $attributeSet) |
|
60
|
|
|
{ |
|
61
|
|
|
$entityIds = $consentList->map(function (Consent $consent) { |
|
62
|
|
|
return $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId(); |
|
63
|
|
|
}); |
|
64
|
|
|
|
|
65
|
|
|
$mappedAttributes = []; |
|
66
|
|
|
foreach ($attributeSet as $attribute) { |
|
67
|
|
|
$mace = $attribute->getAttributeDefinition()->getUrnMace(); |
|
68
|
|
|
$oid = $attribute->getAttributeDefinition()->getUrnOid(); |
|
69
|
|
|
|
|
70
|
|
|
if ($mace !== null) { |
|
71
|
|
|
$mappedAttributes[$mace] = $attribute->getValue(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($oid !== null) { |
|
75
|
|
|
$mappedAttributes[$oid] = $attribute->getValue(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$data = [ |
|
80
|
|
|
'entityIds' => $entityIds, |
|
81
|
|
|
'attributes' => !empty($mappedAttributes) ? $mappedAttributes : new stdClass() |
|
82
|
|
|
]; |
|
83
|
|
|
$response = $this->jsonApiClient->post($data, '/arp'); |
|
84
|
|
|
|
|
85
|
|
|
$specifiedConsents = $consentList->map( |
|
86
|
|
|
function (Consent $consent) use ($response) { |
|
87
|
|
|
$entityId = $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId(); |
|
88
|
|
|
|
|
89
|
|
|
if (!isset($response[$entityId])) { |
|
90
|
|
|
throw new InvalidResponseException( |
|
91
|
|
|
sprintf( |
|
92
|
|
|
'EntityID "%s" was not found in the ARP response (entityIDs: %s)', |
|
93
|
|
|
$entityId, |
|
94
|
|
|
join(', ', array_keys($response)) |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$attributes = []; |
|
100
|
|
|
foreach ($response[$entityId] as $attributeName => $attributeValue) { |
|
101
|
|
|
try { |
|
102
|
|
|
$attributeDefinition = $this->attributeDictionary->getAttributeDefinitionByUrn($attributeName); |
|
103
|
|
|
} catch (UnknownUrnException $exception) { |
|
104
|
|
|
$attributeDefinition = new AttributeDefinition($attributeName, $attributeName, $attributeName); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$attribute = new Attribute($attributeDefinition, $attributeValue); |
|
108
|
|
|
if (!in_array($attribute, $attributes)) { |
|
109
|
|
|
$attributes[] = $attribute; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return SpecifiedConsent::specifies($consent, AttributeSetWithFallbacks::create($attributes)); |
|
114
|
|
|
} |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
return SpecifiedConsentList::createWith($specifiedConsents); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|