Completed
Push — feature/refactor-gateway-contr... ( 4996d0...48680d )
by
unknown
02:33
created

SecondFactorAdfsService::handleAdfsRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 3
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\SecondFactorOnlyBundle\Service\Gateway;
20
21
use Exception;
22
use Psr\Log\LoggerInterface;
23
use Surfnet\SamlBundle\SAML2\ReceivedAuthnRequest;
24
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext;
25
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\Exception\InvalidAdfsRequestException;
26
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\Exception\InvalidAdfsResponseException;
27
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\RequestHelper;
28
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\ResponseHelper;
29
use Symfony\Component\HttpFoundation\Request;
30
31
class SecondFactorAdfsService
32
{
33
    /** @var RequestHelper */
34
    private $adfsRequestHelper;
35
36
    /** @var ResponseHelper */
37
    private $adfsResponseHelper;
38
39
    /**
40
     * SecondFactorAdfsService constructor.
41
     * @param RequestHelper $adfsRequestHelper
42
     * @param ResponseHelper $adfsResponseHelper
43
     */
44
    public function __construct(RequestHelper $adfsRequestHelper, ResponseHelper $adfsResponseHelper)
45
    {
46
        $this->adfsRequestHelper = $adfsRequestHelper;
47
        $this->adfsResponseHelper = $adfsResponseHelper;
48
    }
49
50
    /**
51
     * @param LoggerInterface $logger
52
     * @param Request $httpRequest
53
     * @param ReceivedAuthnRequest $originalRequest
54
     * @return Request
55
     * @throws InvalidAdfsRequestException
56
     */
57
    public function handleAdfsRequest(LoggerInterface $logger, Request $httpRequest, ReceivedAuthnRequest $originalRequest)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
58
    {
59
        if ($this->adfsRequestHelper->isAdfsRequest($httpRequest)) {
60
            $logger->notice('Received AuthnRequest from an ADFS');
61
            try {
62
                $httpRequest = $this->adfsRequestHelper->transformRequest(
63
                    $httpRequest,
64
                    $originalRequest->getRequestId()
65
                );
66
            } catch (Exception $e) {
67
                throw new InvalidAdfsRequestException(
68
                    sprintf('Could not process ADFS Request, error: "%s"', $e->getMessage())
69
                );
70
            }
71
        }
72
73
        return $httpRequest;
74
    }
75
76
    /**
77
     * @param LoggerInterface $logger
78
     * @param ResponseContext $responseContext
79
     * @return null|\Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\ValueObject\Response
80
     * @throws InvalidAdfsResponseException
81
     */
82
    public function handleAdfsResponse(LoggerInterface $logger, ResponseContext $responseContext)
83
    {
84
        if ($this->adfsResponseHelper->isAdfsResponse($responseContext->getInResponseTo())) {
85
86
            // Handle Adfs response
87
            try {
88
                $adfsParameters = $this->adfsResponseHelper->retrieveAdfsParameters();
89
            } catch (Exception $e) {
90
                throw new InvalidAdfsResponseException(
91
                    sprintf('Could not process ADFS Response parameters, error: "%s"', $e->getMessage())
92
                );
93
            }
94
95
            $logger->notice('Sending ACS Response to ADFS plugin');
96
97
            return $adfsParameters;
98
        }
99
100
        return null;
101
    }
102
}
103