1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 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\SecondFactorOnlyBundle\Adfs; |
20
|
|
|
|
21
|
|
|
use InvalidArgumentException; |
22
|
|
|
use Psr\Log\LoggerInterface; |
23
|
|
|
use SAML2_DOMDocumentFactory; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Webmozart\Assert\Assert; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The Adfs helper service is used to transform Adfs requests. Stripping the Adfs specific parameters. |
29
|
|
|
* @package Surfnet\StepupGateway\SecondFactorOnlyBundle\Service |
30
|
|
|
*/ |
31
|
|
|
final class RequestHelper |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** @var LoggerInterface */ |
35
|
|
|
private $logger; |
36
|
|
|
|
37
|
|
|
/** @var StateHandler */ |
38
|
|
|
private $stateHandler; |
39
|
|
|
|
40
|
|
|
const ADFS_PARAM_AUTH_METHOD = 'AuthMethod'; |
41
|
|
|
const ADFS_PARAM_CONTEXT = 'Context'; |
42
|
|
|
|
43
|
|
|
private static $requiredParams = [ |
44
|
|
|
self::ADFS_PARAM_AUTH_METHOD, |
45
|
|
|
self::ADFS_PARAM_CONTEXT, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
public function __construct(StateHandler $stateHandler, LoggerInterface $logger) |
49
|
|
|
{ |
50
|
|
|
$this->stateHandler = $stateHandler; |
51
|
|
|
$this->logger = $logger; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $httpRequest |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function isAdfsRequest(Request $httpRequest) |
59
|
|
|
{ |
60
|
|
|
foreach (self::$requiredParams as $param) { |
61
|
|
|
if (!$httpRequest->request->has($param)) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Transforms the Adfs request to a valid Saml AuthnRequest |
70
|
|
|
* |
71
|
|
|
* @param Request $httpRequest |
72
|
|
|
* @param string $requestId AuthnRequest ID |
73
|
|
|
* @return Request |
74
|
|
|
* @throws InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
|
public function transformRequest(Request $httpRequest, $requestId) |
77
|
|
|
{ |
78
|
|
|
$this->logger->notice('Receiving and validating ADFS request parameters'); |
79
|
|
|
$authMethod = $httpRequest->request->get(self::ADFS_PARAM_AUTH_METHOD); |
80
|
|
|
$context = $httpRequest->request->get(self::ADFS_PARAM_CONTEXT); |
81
|
|
|
|
82
|
|
|
Assert::stringNotEmpty($requestId); |
83
|
|
|
Assert::stringNotEmpty($authMethod); |
84
|
|
|
Assert::stringNotEmpty($context); |
85
|
|
|
|
86
|
|
|
$this->stateHandler |
87
|
|
|
->setRequestId($requestId) |
88
|
|
|
->setAuthMethod($authMethod) |
89
|
|
|
->setContext($context); |
90
|
|
|
|
91
|
|
|
$this->logger->notice('Transforming ADFS Request to a valid AuthnRequest'); |
92
|
|
|
|
93
|
|
|
$httpRequest->request->remove(self::ADFS_PARAM_AUTH_METHOD); |
94
|
|
|
$httpRequest->request->remove(self::ADFS_PARAM_CONTEXT); |
95
|
|
|
|
96
|
|
|
return $httpRequest; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|