Completed
Pull Request — develop (#166)
by
unknown
07:21 queued 03:30
created

RespondService::respond()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\GatewayBundle\Service\Gateway;
20
21
use SAML2\Response as SAMLResponse;
22
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger;
23
use Surfnet\StepupBundle\Service\LoaResolutionService;
24
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
25
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext;
26
use Surfnet\StepupGateway\GatewayBundle\Service\ProxyResponseService;
27
use Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService;
28
29
/**
30
 * Entry point for the Stepup login flow.
31
 *
32
 * See docs/GatewayState.md for a high-level diagram on how this controller
33
 * interacts with outside actors and other parts of Stepup.
34
 */
35
class RespondService
36
{
37
    /** @var SamlAuthenticationLogger */
38
    private $samlLogger;
39
40
    /** @var LoaResolutionService */
41
    private $loaResolutionService;
42
43
    /** @var ProxyResponseService */
44
    private $responseProxy;
45
46
    /** @var SecondFactorService */
47
    private $secondFactorService;
48
49
    /** @var SecondFactorTypeService */
50
    private $secondFactorTypeService;
51
52
    /**
53
     * GatewayServiceProviderService constructor.
54
     * @param SamlAuthenticationLogger $samlLogger
55
     * @param LoaResolutionService $loaResolutionService
56
     * @param ProxyResponseService $responseProxy
57
     * @param SecondFactorService $secondFactorService
58
     * @param SecondFactorTypeService $secondFactorTypeService
59
     */
60 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
        SamlAuthenticationLogger $samlLogger,
62
        LoaResolutionService $loaResolutionService,
63
        ProxyResponseService $responseProxy,
64
        SecondFactorService $secondFactorService,
65
        SecondFactorTypeService $secondFactorTypeService
66
    )
67
    {
68
        $this->samlLogger = $samlLogger;
69
        $this->loaResolutionService = $loaResolutionService;
70
        $this->responseProxy = $responseProxy;
71
        $this->secondFactorService = $secondFactorService;
72
        $this->secondFactorTypeService = $secondFactorTypeService;
73
    }
74
75
    /**
76
     * Send a SAML response back to the service provider.
77
     *
78
     * Second factor verification handled by SecondFactorController is
79
     * finished. The user was forwarded back to this action with an internal
80
     * redirect. This method sends a AuthnResponse back to the service
81
     * provider in response to the AuthnRequest received in ssoAction().
82
     * @param ResponseContext $responseContext
83
     * @return SAMLResponse
84
     */
85
    public function respond(ResponseContext $responseContext)
86
    {
87
        $originalRequestId = $responseContext->getInResponseTo();
88
89
        $logger = $this->samlLogger->forAuthentication($originalRequestId);
90
        $logger->notice('Creating Response');
91
92
        $grantedLoa = null;
93
        if ($responseContext->isSecondFactorVerified()) {
94
            $secondFactor = $this->secondFactorService->findByUuid(
95
                $responseContext->getSelectedSecondFactor()
96
            );
97
98
            $secondFactorTypeService = $this->secondFactorTypeService;
99
            $grantedLoa = $this->loaResolutionService->getLoaByLevel(
100
                $secondFactor->getLoaLevel($secondFactorTypeService)
101
            );
102
        }
103
104
        $response = $this->responseProxy->createProxyResponse(
105
            $responseContext->reconstituteAssertion(),
106
            $responseContext->getDestination(),
107
            (string)$grantedLoa
108
        );
109
110
        $logger->notice(sprintf(
111
            'Responding to request "%s" with response based on response from the remote IdP with response "%s"',
112
            $responseContext->getInResponseTo(),
113
            $response->getId()
114
        ));
115
116
        return $response;
117
    }
118
119
    /**
120
     * Reset the state of the response
121
     *
122
     * @param ResponseContext $responseContext
123
     */
124
    public function respondReset(ResponseContext $responseContext)
125
    {
126
        $responseContext->responseSent();
127
    }
128
}
129