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\StepupBundle\Controller; |
20
|
|
|
|
21
|
|
|
use DateTime; |
22
|
|
|
use Exception; |
23
|
|
|
use SAML2\Response\Exception\InvalidResponseException; |
24
|
|
|
use SAML2\Response\Exception\PreconditionNotMetException; |
25
|
|
|
use Surfnet\SamlBundle\Http\Exception\AuthnFailedSamlResponseException; |
26
|
|
|
use Surfnet\SamlBundle\Http\Exception\SignatureValidationFailedException; |
27
|
|
|
use Surfnet\SamlBundle\Http\Exception\UnknownServiceProviderException; |
28
|
|
|
use Surfnet\SamlBundle\Http\Exception\UnsignedRequestException; |
29
|
|
|
use Surfnet\SamlBundle\Http\Exception\UnsupportedSignatureException; |
30
|
|
|
use Surfnet\StepupBundle\EventListener\RequestIdRequestResponseListener; |
31
|
|
|
use Surfnet\StepupBundle\Exception\Art; |
32
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller as FrameworkController; |
33
|
|
|
use Symfony\Component\HttpFoundation\Request; |
34
|
|
|
use Symfony\Component\HttpFoundation\Response; |
35
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
36
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
37
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
38
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
39
|
|
|
|
40
|
|
|
class ExceptionController extends FrameworkController |
41
|
|
|
{ |
42
|
|
|
public function showAction(Request $request, Exception $exception) |
43
|
|
|
{ |
44
|
|
|
$statusCode = $this->getStatusCode($exception); |
45
|
|
|
|
46
|
|
|
if ($statusCode == 404) { |
47
|
|
|
$template = 'SurfnetStepupBundle:Exception:error404.html.twig'; |
48
|
|
|
} else { |
49
|
|
|
$template = 'SurfnetStepupBundle:Exception:error.html.twig'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$response = new Response('', $statusCode); |
53
|
|
|
|
54
|
|
|
$timestamp = (new DateTime)->format(DateTime::ISO8601); |
55
|
|
|
$hostname = $request->getHost(); |
56
|
|
|
$requestId = $request->headers->get(RequestIdRequestResponseListener::HEADER_NAME, '-'); |
57
|
|
|
$errorCode = Art::forException($exception); |
58
|
|
|
$userAgent = $request->headers->get('User-Agent'); |
59
|
|
|
$ipAddress = $request->getClientIp(); |
60
|
|
|
|
61
|
|
|
return $this->render( |
62
|
|
|
$template, |
63
|
|
|
[ |
64
|
|
|
'timestamp' => $timestamp, |
65
|
|
|
'hostname' => $hostname, |
66
|
|
|
'request_id' => $requestId, |
67
|
|
|
'error_code' => $errorCode, |
68
|
|
|
'user_agent' => $userAgent, |
69
|
|
|
'ip_address' => $ipAddress, |
70
|
|
|
] + $this->getPageTitleAndDescription($exception), |
71
|
|
|
$response |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Exception $exception |
77
|
|
|
* @return int HTTP status code |
78
|
|
|
*/ |
79
|
|
|
protected function getStatusCode(Exception $exception) |
80
|
|
|
{ |
81
|
|
|
if ($exception instanceof AuthenticationException || |
82
|
|
|
$exception instanceof InvalidResponseException) { |
83
|
|
|
return Response::HTTP_UNAUTHORIZED; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($exception instanceof AccessDeniedException || |
87
|
|
|
$exception instanceof PreconditionNotMetException) { |
88
|
|
|
return Response::HTTP_FORBIDDEN; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($exception instanceof HttpExceptionInterface) { |
92
|
|
|
return $exception->getStatusCode(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Unknown exceptions are server errors! |
96
|
|
|
return 500; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Exception $exception |
101
|
|
|
* @return array View parameters 'title' and 'description' |
102
|
|
|
*/ |
103
|
|
|
protected function getPageTitleAndDescription(Exception $exception) |
104
|
|
|
{ |
105
|
|
|
$translator = $this->getTranslator(); |
106
|
|
|
$parameters = []; |
|
|
|
|
107
|
|
|
|
108
|
|
|
if ($exception instanceof SignatureValidationFailedException) { |
109
|
|
|
$title = $translator->trans('stepup.error.signature_validation_failed.title'); |
110
|
|
|
$description = $translator->trans('stepup.error.signature_validation_failed.description'); |
111
|
|
|
|
112
|
|
|
} elseif ($exception instanceof UnsignedRequestException) { |
113
|
|
|
$title = $translator->trans('stepup.error.unsigned_request.title'); |
114
|
|
|
$description = $translator->trans('stepup.error.unsigned_request.description'); |
115
|
|
|
|
116
|
|
|
} elseif ($exception instanceof UnsupportedSignatureException) { |
117
|
|
|
$title = $translator->trans('stepup.error.unsupported_signature.title'); |
118
|
|
|
$description = $translator->trans('stepup.error.unsupported_signature.description'); |
119
|
|
|
|
120
|
|
|
} elseif ($exception instanceof UnknownServiceProviderException) { |
121
|
|
|
$title = $translator->trans('stepup.error.unknown_service_provider.title'); |
122
|
|
|
$description = $translator->trans('stepup.error.unknown_service_provider.description'); |
123
|
|
|
|
124
|
|
|
} elseif ($exception instanceof AuthnFailedSamlResponseException) { |
125
|
|
|
$title = $translator->trans('stepup.error.authn_failed.title'); |
126
|
|
|
$description = $translator->trans('stepup.error.authn_failed.description'); |
127
|
|
|
|
128
|
|
|
} elseif ($exception instanceof PreconditionNotMetException) { |
129
|
|
|
$title = $translator->trans('stepup.error.precondition_not_met.title'); |
130
|
|
|
$description = $translator->trans('stepup.error.precondition_not_met.description'); |
131
|
|
|
|
132
|
|
|
} elseif ($exception instanceof InvalidResponseException) { |
133
|
|
|
$title = $translator->trans('stepup.error.authentication_error.title'); |
134
|
|
|
$description = $translator->trans('stepup.error.authentication_error.description'); |
135
|
|
|
} elseif ($exception instanceof AuthenticationException) { |
136
|
|
|
$title = $translator->trans('stepup.error.authentication_error.title'); |
137
|
|
|
$description = $translator->trans('stepup.error.authentication_error.description'); |
138
|
|
|
} else { |
139
|
|
|
$title = $translator->trans('stepup.error.generic_error.title'); |
140
|
|
|
$description = $translator->trans('stepup.error.generic_error.description'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return [ |
144
|
|
|
'title' => $title, |
145
|
|
|
'description' => $description, |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return TranslatorInterface |
151
|
|
|
*/ |
152
|
|
|
protected function getTranslator() |
153
|
|
|
{ |
154
|
|
|
return $this->get('translator'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.