Completed
Push — master ( e96383...13486f )
by
unknown
06:11
created

RequestHelper::transformRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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