Completed
Push — feature/refactor-gateway-contr... ( 8c53ef...4df939 )
by
unknown
02:47 queued 24s
created

RespondService::respondReset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 Exception;
22
use SAML2\Constants;
23
use SAML2\Response as SAMLResponse;
24
use Surfnet\SamlBundle\Entity\IdentityProvider;
25
use Surfnet\SamlBundle\Entity\ServiceProvider;
26
use Surfnet\SamlBundle\Http\PostBinding;
27
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger;
28
use Surfnet\SamlBundle\SAML2\AuthnRequest;
29
use Surfnet\SamlBundle\SAML2\AuthnRequestFactory;
30
use Surfnet\SamlBundle\SAML2\ReceivedAuthnRequest;
31
use Surfnet\StepupBundle\Service\LoaResolutionService;
32
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
33
use Surfnet\StepupGateway\GatewayBundle\Exception\RequesterFailureException;
34
use Surfnet\StepupGateway\GatewayBundle\Exception\ResponseFailureException;
35
use Surfnet\StepupGateway\GatewayBundle\Saml\AssertionAdapter;
36
use Surfnet\StepupGateway\GatewayBundle\Saml\Exception\UnknownInResponseToException;
37
use Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler;
38
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder;
39
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext;
40
use Surfnet\StepupGateway\GatewayBundle\Service\ProxyResponseService;
41
use Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService;
42
use Symfony\Component\HttpFoundation\Request;
43
44
/**
45
 * Entry point for the Stepup login flow.
46
 *
47
 * See docs/GatewayState.md for a high-level diagram on how this controller
48
 * interacts with outside actors and other parts of Stepup.
49
 */
50
class RespondService
51
{
52
    /** @var SamlAuthenticationLogger */
53
    private $samlLogger;
54
55
    /** @var LoaResolutionService */
56
    private $loaResolutionService;
57
58
    /** @var ProxyResponseService */
59
    private $responseProxy;
60
61
    /** @var SecondFactorService */
62
    private $secondFactorService;
63
64
    /** @var SecondFactorTypeService */
65
    private $secondFactorTypeService;
66
67
    /**
68
     * GatewayServiceProviderService constructor.
69
     * @param SamlAuthenticationLogger $samlLogger
70
     * @param LoaResolutionService $loaResolutionService
71
     * @param ProxyResponseService $responseProxy
72
     * @param SecondFactorService $secondFactorService
73
     * @param SecondFactorTypeService $secondFactorTypeService
74
     */
75 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...
76
        SamlAuthenticationLogger $samlLogger,
77
        LoaResolutionService $loaResolutionService,
78
        ProxyResponseService $responseProxy,
79
        SecondFactorService $secondFactorService,
80
        SecondFactorTypeService $secondFactorTypeService
81
    ) {
82
        $this->samlLogger = $samlLogger;
83
        $this->loaResolutionService = $loaResolutionService;
84
        $this->responseProxy = $responseProxy;
85
        $this->secondFactorService = $secondFactorService;
86
        $this->secondFactorTypeService = $secondFactorTypeService;
87
    }
88
89
    /**
90
     * Send a SAML response back to the service provider.
91
     *
92
     * Second factor verification handled by SecondFactorController is
93
     * finished. The user was forwarded back to this action with an internal
94
     * redirect. This method sends a AuthnResponse back to the service
95
     * provider in response to the AuthnRequest received in ssoAction().
96
     * @param ResponseContext $responseContext
97
     * @return SAMLResponse
98
     */
99
    public function respond(ResponseContext $responseContext)
100
    {
101
        $originalRequestId = $responseContext->getInResponseTo();
102
103
        $logger = $this->samlLogger->forAuthentication($originalRequestId);
104
        $logger->notice('Creating Response');
105
106
        $grantedLoa = null;
107
        if ($responseContext->isSecondFactorVerified()) {
108
            $secondFactor = $this->secondFactorService->findByUuid(
109
                $responseContext->getSelectedSecondFactor()
110
            );
111
112
            $secondFactorTypeService = $this->secondFactorTypeService;
113
            $grantedLoa = $this->loaResolutionService->getLoaByLevel(
114
                $secondFactor->getLoaLevel($secondFactorTypeService)
115
            );
116
        }
117
118
        $response = $this->responseProxy->createProxyResponse(
119
            $responseContext->reconstituteAssertion(),
120
            $responseContext->getDestination(),
121
            (string)$grantedLoa
122
        );
123
124
        $logger->notice(sprintf(
125
            'Responding to request "%s" with response based on response from the remote IdP with response "%s"',
126
            $responseContext->getInResponseTo(),
127
            $response->getId()
128
        ));
129
130
        return $response;
131
    }
132
133
    /**
134
     * Reset the state of the response
135
     *
136
     * @param ResponseContext $responseContext
137
     */
138
    public function respondReset(ResponseContext $responseContext)
139
    {
140
        $responseContext->responseSent();
141
    }
142
}
143