Completed
Push — feature/post-binding-without-c... ( 539191...2342a3 )
by
unknown
02:39
created

RequestHelper::transformRequest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 17
nc 1
nop 3
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
     * @param string $acsUrl AuthnRequest assertion consumer service URL
74
     * @return Request
75
     * @throws InvalidArgumentException
76
     */
77
    public function transformRequest(Request $httpRequest, $requestId, $acsUrl)
78
    {
79
        $this->logger->notice('Receiving and validating ADFS request parameters');
80
        $authMethod = $httpRequest->request->get(self::ADFS_PARAM_AUTH_METHOD);
81
        $context = $httpRequest->request->get(self::ADFS_PARAM_CONTEXT);
82
83
        Assert::stringNotEmpty($requestId);
84
        Assert::stringNotEmpty($acsUrl);
85
        Assert::stringNotEmpty($authMethod);
86
        Assert::stringNotEmpty($context);
87
88
        $this->stateHandler
89
            ->setRequestId($requestId)
90
            ->setAuthMethod($authMethod)
91
            ->setContext($context)
92
            ->setAssertionConsumerServiceUrl($acsUrl);
93
94
        $this->logger->notice('Transforming ADFS Request to a valid AuthnRequest');
95
96
        $httpRequest->request->remove(self::ADFS_PARAM_AUTH_METHOD);
97
        $httpRequest->request->remove(self::ADFS_PARAM_CONTEXT);
98
99
        return $httpRequest;
100
    }
101
}
102