1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 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\Gateway; |
20
|
|
|
|
21
|
|
|
use SAML2\Constants; |
22
|
|
|
use SAML2\Response as SAMLResponse; |
23
|
|
|
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger; |
24
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder; |
25
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Entry point for the Stepup login flow. |
29
|
|
|
* |
30
|
|
|
* See docs/GatewayState.md for a high-level diagram on how this controller |
31
|
|
|
* interacts with outside actors and other parts of Stepup. |
32
|
|
|
*/ |
33
|
|
|
class FailedResponseService |
34
|
|
|
{ |
35
|
|
|
/** @var SamlAuthenticationLogger */ |
36
|
|
|
private $samlLogger; |
37
|
|
|
|
38
|
|
|
/** @var ResponseBuilder */ |
39
|
|
|
private $responseBuilder; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* GatewayServiceProviderService constructor. |
43
|
|
|
* @param SamlAuthenticationLogger $samlLogger |
44
|
|
|
* @param ResponseBuilder $responseBuilder |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
SamlAuthenticationLogger $samlLogger, |
48
|
|
|
ResponseBuilder $responseBuilder |
49
|
|
|
) { |
50
|
|
|
$this->samlLogger = $samlLogger; |
51
|
|
|
$this->responseBuilder = $responseBuilder; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param ResponseContext $responseContext |
56
|
|
|
* @return SAMLResponse |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function sendLoaCannotBeGiven(ResponseContext $responseContext) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
61
|
|
|
|
62
|
|
|
$logger = $this->samlLogger->forAuthentication($originalRequestId); |
63
|
|
|
$logger->notice('Loa cannot be given, creating Response with NoAuthnContext status'); |
64
|
|
|
|
65
|
|
|
$response = $this->responseBuilder |
66
|
|
|
->createNewResponse($responseContext) |
67
|
|
|
->setResponseStatus(Constants::STATUS_RESPONDER, Constants::STATUS_NO_AUTHN_CONTEXT) |
68
|
|
|
->get(); |
69
|
|
|
|
70
|
|
|
$logger->notice(sprintf( |
71
|
|
|
'Responding to request "%s" with response based on response from the remote IdP with response "%s"', |
72
|
|
|
$responseContext->getInResponseTo(), |
73
|
|
|
$response->getId() |
74
|
|
|
)); |
75
|
|
|
|
76
|
|
|
return $response; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param ResponseContext $responseContext |
81
|
|
|
* @return SAMLResponse |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function sendAuthenticationCancelledByUser(ResponseContext $responseContext) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
86
|
|
|
|
87
|
|
|
$logger = $this->samlLogger->forAuthentication($originalRequestId); |
88
|
|
|
$logger->notice('Authentication was cancelled by the user, creating Response with AuthnFailed status'); |
89
|
|
|
|
90
|
|
|
$response = $this->responseBuilder |
91
|
|
|
->createNewResponse($responseContext) |
92
|
|
|
->setResponseStatus( |
93
|
|
|
Constants::STATUS_RESPONDER, |
94
|
|
|
Constants::STATUS_AUTHN_FAILED, |
95
|
|
|
'Authentication cancelled by user' |
96
|
|
|
) |
97
|
|
|
->get(); |
98
|
|
|
|
99
|
|
|
$logger->notice(sprintf( |
100
|
|
|
'Responding to request "%s" with response based on response from the remote IdP with response "%s"', |
101
|
|
|
$responseContext->getInResponseTo(), |
102
|
|
|
$response->getId() |
103
|
|
|
)); |
104
|
|
|
|
105
|
|
|
return $response; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return SAMLResponse |
110
|
|
|
*/ |
111
|
|
|
public function createRequesterFailureResponse() |
112
|
|
|
{ |
113
|
|
|
$context = $this->getResponseContext(); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$response = $this->responseBuilder |
116
|
|
|
->createNewResponse($context) |
117
|
|
|
->setResponseStatus(Constants::STATUS_REQUESTER, Constants::STATUS_REQUEST_UNSUPPORTED) |
118
|
|
|
->get(); |
119
|
|
|
|
120
|
|
|
return $response; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $context |
125
|
|
|
* @return SAMLResponse |
126
|
|
|
*/ |
127
|
|
|
public function createResponseFailureResponse($context) |
128
|
|
|
{ |
129
|
|
|
$response = $this->responseBuilder |
130
|
|
|
->createNewResponse($context) |
131
|
|
|
->setResponseStatus(Constants::STATUS_RESPONDER) |
132
|
|
|
->get(); |
133
|
|
|
|
134
|
|
|
return $response; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.