1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
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\Saml; |
20
|
|
|
|
21
|
|
|
use SAML2\Constants; |
22
|
|
|
use SAML2\Response; |
23
|
|
|
use Surfnet\SamlBundle\Exception\LogicException; |
24
|
|
|
|
25
|
|
|
class ResponseBuilder |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
|
|
|
|
28
|
|
|
* @var Response |
29
|
|
|
*/ |
30
|
|
|
private $response; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @var \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext |
34
|
|
|
*/ |
35
|
|
|
private $responseContext; |
|
|
|
|
36
|
|
|
|
37
|
|
|
public function createNewResponse(ResponseContext $context) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if ($this->response) { |
40
|
|
|
throw new LogicException('Cannot create a new Response when still building a response.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->responseContext = $context; |
44
|
|
|
|
45
|
|
|
$response = new Response(); |
46
|
|
|
$response->setDestination($context->getDestination()); |
47
|
|
|
$response->setIssuer($context->getIssuer()); |
48
|
|
|
$response->setIssueInstant($context->getIssueInstant()); |
49
|
|
|
$response->setInResponseTo($context->getInResponseTo()); |
50
|
|
|
|
51
|
|
|
$this->response = $response; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
|
|
|
|
57
|
|
|
* @param string $status |
|
|
|
|
58
|
|
|
* @param string|null $subStatus |
|
|
|
|
59
|
|
|
* @param string|null $message |
|
|
|
|
60
|
|
|
* @return $this |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function setResponseStatus($status, $subStatus = null, $message = null) |
63
|
|
|
{ |
64
|
|
|
if (!$this->isValidResponseStatus($status)) { |
65
|
|
|
throw new LogicException(sprintf('Trying to set invalid Response Status')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($subStatus && !$this->isValidResponseSubStatus($subStatus)) { |
69
|
|
|
throw new LogicException(sprintf('Trying to set invalid Response SubStatus')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$status = ['Code' => $status]; |
73
|
|
|
if ($subStatus) { |
74
|
|
|
$status['SubCode'] = $subStatus; |
75
|
|
|
} |
76
|
|
|
if ($message) { |
77
|
|
|
$status['Message'] = $message; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->response->setStatus($status); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function get() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$response = $this->response; |
88
|
|
|
|
89
|
|
|
$this->response = null; |
90
|
|
|
$this->responseContext = null; |
91
|
|
|
|
92
|
|
|
return $response; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function isValidResponseStatus($status) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
return in_array($status, [ |
|
|
|
|
98
|
|
|
Constants::STATUS_SUCCESS, // weeee! |
99
|
|
|
Constants::STATUS_REQUESTER, // Something is wrong with the AuthnRequest |
100
|
|
|
Constants::STATUS_RESPONDER, // Something went wrong with the Response |
101
|
|
|
Constants::STATUS_VERSION_MISMATCH, // The version of the request message was incorrect |
102
|
|
|
]); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function isValidResponseSubStatus($subStatus) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
return in_array($subStatus, [ |
|
|
|
|
108
|
|
|
Constants::STATUS_AUTHN_FAILED, // failed authentication |
109
|
|
|
Constants::STATUS_INVALID_ATTR, |
110
|
|
|
Constants::STATUS_INVALID_NAMEID_POLICY, |
111
|
|
|
Constants::STATUS_NO_AUTHN_CONTEXT, // insufficient Loa or Loa cannot be met |
112
|
|
|
Constants::STATUS_NO_AVAILABLE_IDP, |
113
|
|
|
Constants::STATUS_NO_PASSIVE, |
114
|
|
|
Constants::STATUS_NO_SUPPORTED_IDP, |
115
|
|
|
Constants::STATUS_PARTIAL_LOGOUT, |
116
|
|
|
Constants::STATUS_PROXY_COUNT_EXCEEDED, |
117
|
|
|
Constants::STATUS_REQUEST_DENIED, |
118
|
|
|
Constants::STATUS_REQUEST_UNSUPPORTED, |
119
|
|
|
Constants::STATUS_REQUEST_VERSION_DEPRECATED, |
120
|
|
|
Constants::STATUS_REQUEST_VERSION_TOO_HIGH, |
121
|
|
|
Constants::STATUS_REQUEST_VERSION_TOO_LOW, |
122
|
|
|
Constants::STATUS_RESOURCE_NOT_RECOGNIZED, |
123
|
|
|
Constants::STATUS_TOO_MANY_RESPONSES, |
124
|
|
|
Constants::STATUS_UNKNOWN_ATTR_PROFILE, |
125
|
|
|
Constants::STATUS_UNKNOWN_PRINCIPAL, |
126
|
|
|
Constants::STATUS_UNSUPPORTED_BINDING, |
127
|
|
|
]); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|