1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 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 Surfnet\StepupGateway\GatewayBundle\Pdp\Dto; |
20
|
|
|
|
21
|
|
|
use JsonSerializable; |
22
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Request\AccessSubject; |
23
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Request\Resource; |
24
|
|
|
use Webmozart\Assert\Assert; |
25
|
|
|
|
26
|
|
|
final class Request implements JsonSerializable |
27
|
|
|
{ |
28
|
|
|
const NAMEIDFORMAT_UNSPECIFIED = 'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified'; |
29
|
|
|
const XACML_ATTRIBUTE_IP_ADDRESS = 'urn:mace:surfnet.nl:collab:xacml-attribute:ip-address'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AccessSubject |
33
|
|
|
*/ |
34
|
|
|
public $accessSubject; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Request\Resource |
38
|
|
|
*/ |
39
|
|
|
public $resource; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $clientId |
43
|
|
|
* @param string $subjectId |
44
|
|
|
* @param string $idpEntityId |
45
|
|
|
* @param string $spEntityId |
46
|
|
|
* @param array $responseAttributes |
47
|
|
|
* @param string $requestIpAddress |
48
|
|
|
* @return Request $request |
49
|
|
|
*/ |
50
|
|
|
public static function from($clientId, $subjectId, $idpEntityId, $spEntityId, array $responseAttributes, $requestIpAddress) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
Assert::string($subjectId, 'The SubjectId must be a string, received "%s"'); |
53
|
|
|
Assert::string($idpEntityId, 'The IDPentityID must be a string, received "%s"'); |
54
|
|
|
Assert::string($spEntityId, 'The SPentityID must be a string, received "%s"'); |
55
|
|
|
Assert::allString( |
56
|
|
|
array_keys($responseAttributes), |
57
|
|
|
'The keys of the Response attributes must be strings' |
58
|
|
|
); |
59
|
|
|
Assert::allIsArray($responseAttributes, 'The values of the Response attributes must be arrays'); |
60
|
|
|
Assert::string($clientId, 'The client ID must be a string, received "%s"'); |
61
|
|
|
Assert::string($requestIpAddress, 'The request IP address must be a string, received "%s"'); |
62
|
|
|
|
63
|
|
|
$request = new self; |
64
|
|
|
|
65
|
|
|
$subjectIdAttribute = new Attribute; |
66
|
|
|
$subjectIdAttribute->attributeId = self::NAMEIDFORMAT_UNSPECIFIED; |
67
|
|
|
$subjectIdAttribute->value = $subjectId; |
68
|
|
|
|
69
|
|
|
$request->accessSubject = new AccessSubject; |
70
|
|
|
$request->accessSubject->attributes = [$subjectIdAttribute]; |
71
|
|
|
|
72
|
|
|
$spEntityIdAttribute = new Attribute; |
73
|
|
|
$spEntityIdAttribute->attributeId = 'SPentityID'; |
74
|
|
|
$spEntityIdAttribute->value = $spEntityId; |
75
|
|
|
|
76
|
|
|
$idpEntityIdAttribute = new Attribute; |
77
|
|
|
$idpEntityIdAttribute->attributeId = 'IDPentityID'; |
78
|
|
|
$idpEntityIdAttribute->value = $idpEntityId; |
79
|
|
|
|
80
|
|
|
$clientIdAttribute = new Attribute; |
81
|
|
|
$clientIdAttribute->attributeId = 'ClientID'; |
82
|
|
|
$clientIdAttribute->value = $clientId; |
83
|
|
|
|
84
|
|
|
$request->resource = new Resource; |
85
|
|
|
$request->resource->attributes = [$clientIdAttribute, $spEntityIdAttribute, $idpEntityIdAttribute]; |
86
|
|
|
|
87
|
|
|
foreach ($responseAttributes as $id => $values) { |
88
|
|
|
foreach ($values as $value) { |
89
|
|
|
$attribute = new Attribute; |
90
|
|
|
$attribute->attributeId = $id; |
|
|
|
|
91
|
|
|
$attribute->value = $value; |
92
|
|
|
|
93
|
|
|
$request->accessSubject->attributes[] = $attribute; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$ipAddressAttribute = new Attribute; |
98
|
|
|
$ipAddressAttribute->attributeId = self::XACML_ATTRIBUTE_IP_ADDRESS; |
99
|
|
|
$ipAddressAttribute->value = $requestIpAddress; |
100
|
|
|
|
101
|
|
|
$request->accessSubject->attributes[] = $ipAddressAttribute; |
102
|
|
|
|
103
|
|
|
return $request; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function jsonSerialize() |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
|
|
'Request' => [ |
110
|
|
|
'AccessSubject' => $this->accessSubject, |
111
|
|
|
'Resource' => $this->resource, |
112
|
|
|
] |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.