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