Completed
Pull Request — develop (#120)
by Nic
05:15 queued 02:39
created

SamlController::testSecondFactorAction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 23

Duplication

Lines 11
Ratio 28.21 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 11
loc 39
rs 8.8571
cc 2
eloc 23
nc 2
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 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\StepupSelfService\SelfServiceBundle\Controller;
20
21
use Exception;
22
use Surfnet\SamlBundle\Http\XMLResponse;
23
use Surfnet\SamlBundle\SAML2\Response\Assertion\InResponseTo;
24
use Surfnet\StepupBundle\Value\SecondFactorType;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\Security\Core\Exception\AuthenticationException;
27
28
class SamlController extends Controller
29
{
30
    /**
31
     * @param string $secondFactorId
32
     *
33
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
34
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
35
     * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
36
     */
37
    public function testSecondFactorAction($secondFactorId)
38
    {
39
        $this->get('logger')->notice(
40
            'Starting second factor test'
41
        );
42
43
        $secondFactorService = $this->get('surfnet_stepup_self_service_self_service.service.second_factor');
44
        $identity            = $this->getIdentity();
45
46 View Code Duplication
        if (!$secondFactorService->identityHasSecondFactorOfStateWithId($identity->id, 'vetted', $secondFactorId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
47
            $this->get('logger')->error(
48
                sprintf(
49
                    'Identity "%s" tried to test second factor "%s", but does not own that second factor or it is not vetted',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 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...
50
                    $identity->id,
51
                    $secondFactorId
52
                )
53
            );
54
55
            throw $this->createNotFoundException();
56
        }
57
58
        $loaResolutionService         = $this->get('surfnet_stepup.service.loa_resolution');
59
        $authenticationRequestFactory = $this->get('self_service.test_second_factor_authentication_request_factory');
60
61
        $secondFactor     = $secondFactorService->findOneVetted($secondFactorId);
62
        $secondFactorType = new SecondFactorType($secondFactor->type);
63
64
        $authenticationRequest = $authenticationRequestFactory->createSecondFactorTestRequest(
65
            $identity->nameId,
66
            $loaResolutionService->getLoaByLevel($secondFactorType->getLevel())
67
        );
68
69
        $this->get('session')->set('second_factor_test_request_id', $authenticationRequest->getRequestId());
70
71
        $logger = $this->get('surfnet_saml.logger')->forAuthentication($authenticationRequest->getRequestId());
72
        $logger->notice('Sending authentication request to the second factor test IDP');
73
74
        return $this->get('surfnet_saml.http.redirect_binding')->createRedirectResponseFor($authenticationRequest);
75
    }
76
77
    public function consumeAssertionAction(Request $httpRequest)
78
    {
79
        $logger = $this->get('logger');
80
81
        $logger->notice('Received an authentication response for testing a second factor');
82
83
        $session = $this->get('session');
84
85
        if (!$session->has('second_factor_test_request_id')) {
86
            $logger->error(
87
                'Received an authentication response for testing a second factor, but no second factor test response was expected'
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
88
            );
89
90
            throw $this->createAccessDeniedException('Did not expect an authentication response');
91
        }
92
93
        $initiatedRequestId = $session->get('second_factor_test_request_id');
94
95
        $logger = $this->get('surfnet_saml.logger')->forAuthentication($initiatedRequestId);
96
97
        $session->remove('second_factor_test_request_id');
98
99
        $postBinding = $this->get('surfnet_saml.http.post_binding');
100
101
        try {
102
            $assertion = $postBinding->processResponse(
103
                $httpRequest,
104
                $this->get('self_service.second_factor_test_idp'),
105
                $this->get('surfnet_saml.hosted.service_provider')
106
            );
107
108
            if (!InResponseTo::assertEquals($assertion, $initiatedRequestId)) {
109
                $logger->error(
110
                    sprintf(
111
                        'Expected a response to the request with ID "%s", but the SAMLResponse was a response to a different request',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
112
                        $initiatedRequestId
113
                    )
114
                );
115
116
                throw new AuthenticationException('Unexpected InResponseTo in SAMLResponse');
117
            }
118
119
            $session->getFlashBag()->add('success', 'ss.test_second_factor.verification_successful');
120
        } catch (Exception $exception) {
121
            $session->getFlashBag()->add('error', 'ss.test_second_factor.verification_failed');
122
        }
123
124
        return $this->redirectToRoute('ss_second_factor_list');
125
    }
126
127
    public function metadataAction()
128
    {
129
        /** @var \Surfnet\SamlBundle\Metadata\MetadataFactory $metadataFactory */
130
        $metadataFactory = $this->get('surfnet_saml.metadata_factory');
131
132
        return new XMLResponse($metadataFactory->generate());
133
    }
134
}
135