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\Service; |
20
|
|
|
|
21
|
|
|
use SAML2_Const; |
22
|
|
|
use SAML2_Response; |
23
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder; |
24
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext; |
25
|
|
|
use Symfony\Bundle\TwigBundle\TwigEngine; |
26
|
|
|
use Symfony\Component\HttpFoundation\Response; |
27
|
|
|
|
28
|
|
|
final class ResponseRenderingService |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ResponseBuilder |
32
|
|
|
*/ |
33
|
|
|
private $responseBuilder; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var TwigEngine |
37
|
|
|
*/ |
38
|
|
|
private $twigEngine; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* SamlResponseRenderingService constructor. |
42
|
|
|
* @param ResponseBuilder $responseBuilder |
43
|
|
|
* @param TwigEngine $twigEngine |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
ResponseBuilder $responseBuilder, |
47
|
|
|
TwigEngine $twigEngine |
48
|
|
|
) { |
49
|
|
|
$this->responseBuilder = $responseBuilder; |
50
|
|
|
$this->twigEngine = $twigEngine; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return Response |
55
|
|
|
*/ |
56
|
|
|
public function renderRequesterFailureResponse(ResponseContext $context) |
57
|
|
|
{ |
58
|
|
|
return $this->renderResponse( |
59
|
|
|
$context, |
60
|
|
|
$this->responseBuilder |
61
|
|
|
->createNewResponse($context) |
62
|
|
|
->setResponseStatus( |
63
|
|
|
SAML2_Const::STATUS_REQUESTER, |
64
|
|
|
SAML2_Const::STATUS_REQUEST_UNSUPPORTED |
65
|
|
|
) |
66
|
|
|
->get() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return Response |
72
|
|
|
*/ |
73
|
|
|
public function renderUnprocessableResponse(ResponseContext $context) |
74
|
|
|
{ |
75
|
|
|
return $this->renderSamlResponse( |
76
|
|
|
$context, |
77
|
|
|
'unprocessableResponse', |
78
|
|
|
$this->responseBuilder |
79
|
|
|
->createNewResponse($context) |
80
|
|
|
->setResponseStatus(SAML2_Const::STATUS_RESPONDER) |
81
|
|
|
->get() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param SAML2_Response $response |
87
|
|
|
* @return Response |
88
|
|
|
*/ |
89
|
|
|
public function renderResponse( |
90
|
|
|
ResponseContext $context, |
91
|
|
|
SAML2_Response $response |
92
|
|
|
) { |
93
|
|
|
return $this->renderSamlResponse($context, 'consumeAssertion', $response); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $view |
98
|
|
|
* @param SAML2_Response $response |
99
|
|
|
* @return Response |
100
|
|
|
*/ |
101
|
|
|
private function renderSamlResponse( |
102
|
|
|
ResponseContext $context, |
103
|
|
|
$view, |
104
|
|
|
SAML2_Response $response |
105
|
|
|
) { |
106
|
|
|
return $this->twigEngine->renderResponse( |
107
|
|
|
'SurfnetStepupGatewayGatewayBundle:Gateway:' . $view . '.html.twig', |
108
|
|
|
[ |
109
|
|
|
'acu' => $context->getDestination(), |
110
|
|
|
'response' => $this->getResponseAsXML($response), |
111
|
|
|
'relayState' => $context->getRelayState() |
112
|
|
|
] |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param SAML2_Response $response |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
private function getResponseAsXML(SAML2_Response $response) |
121
|
|
|
{ |
122
|
|
|
return base64_encode($response->toUnsignedXML()->ownerDocument->saveXML()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|