Completed
Pull Request — develop (#166)
by
unknown
06:00 queued 02:46
created

ConsumeAssertionService::consumeAssertion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 3
nc 3
nop 2
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 Surfnet\SamlBundle\Entity\IdentityProvider;
23
use Surfnet\SamlBundle\Entity\ServiceProvider;
24
use Surfnet\SamlBundle\Http\PostBinding;
25
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger;
26
use Surfnet\StepupGateway\GatewayBundle\Exception\ResponseFailureException;
27
use Surfnet\StepupGateway\GatewayBundle\Saml\AssertionAdapter;
28
use Surfnet\StepupGateway\GatewayBundle\Saml\Exception\UnknownInResponseToException;
29
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext;
30
use Symfony\Component\HttpFoundation\Request;
31
32
/**
33
 * Entry point for the Stepup login flow.
34
 *
35
 * See docs/GatewayState.md for a high-level diagram on how this controller
36
 * interacts with outside actors and other parts of Stepup.
37
 */
38
class ConsumeAssertionService
39
{
40
    const RESPONSE_CONTEXT_SERVICE_ID = 'gateway.proxy.response_context';
41
42
    /** @var PostBinding */
43
    private $postBinding;
44
45
    /** @var SamlAuthenticationLogger */
46
    private $samlLogger;
47
48
    /** @var ServiceProvider */
49
    private $hostedServiceProvider;
50
51
    /** @var IdentityProvider */
52
    private $remoteIdp;
53
54
    /**
55
     * GatewayServiceProviderService constructor.
56
     * @param PostBinding $postBinding
57
     * @param SamlAuthenticationLogger $samlLogger
58
     * @param ServiceProvider $hostedServiceProvider
59
     * @param IdentityProvider $remoteIdp
60
     */
61
    public function __construct(
62
        PostBinding $postBinding,
63
        SamlAuthenticationLogger $samlLogger,
64
        ServiceProvider $hostedServiceProvider,
65
        IdentityProvider $remoteIdp
66
    ) {
67
        $this->postBinding = $postBinding;
68
        $this->samlLogger = $samlLogger;
69
        $this->hostedServiceProvider = $hostedServiceProvider;
70
        $this->remoteIdp = $remoteIdp;
71
    }
72
73
    /**
74
     * Receive an AuthnResponse from an identity provider.
75
     *
76
     * The AuthnRequest started in ssoAction() resulted in an AuthnResponse
77
     * from the IDP. This method handles the assertion and forwards the user
78
     * using an internal redirect to the SecondFactorController to start the
79
     * actual second factor verification.
80
     *
81
     * @param Request $request
82
     * @param ResponseContext $responseContext
83
     * @return void
84
     */
85
    public function consumeAssertion(Request $request, ResponseContext $responseContext)
86
    {
87
        $originalRequestId = $responseContext->getInResponseTo();
88
89
        $logger = $this->samlLogger->forAuthentication($originalRequestId);
90
        $logger->notice('Received SAMLResponse, attempting to process for Proxy Response');
91
92
        try {
93
            $assertion = $this->postBinding->processResponse(
94
                $request,
95
                $this->remoteIdp,
96
                $this->hostedServiceProvider
97
            );
98
        } catch (Exception $exception) {
99
            $message = sprintf('Could not process received Response, error: "%s"', $exception->getMessage());
100
            $logger->error($message);
101
102
            throw new ResponseFailureException($message);
103
        }
104
105
        $adaptedAssertion = new AssertionAdapter($assertion);
106
        $expectedInResponseTo = $responseContext->getExpectedInResponseTo();
107
        if (!$adaptedAssertion->inResponseToMatches($expectedInResponseTo)) {
108
            throw new UnknownInResponseToException(
109
                $adaptedAssertion->getInResponseTo(),
110
                $expectedInResponseTo
111
            );
112
        }
113
114
        $logger->notice('Successfully processed SAMLResponse');
115
116
        $responseContext->saveAssertion($assertion);
117
118
        $logger->notice(sprintf('Forwarding to second factor controller for loa determination and handling'));
119
    }
120
}
121