Passed
Push — feature/create_docker_publish_... ( f65c8d...09f4fb )
by Michiel
15:10 queued 08:47
created

ResponseValidator::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 2
b 0
f 0
nc 3
nop 3
dl 0
loc 24
rs 9.7
1
<?php
2
3
/**
4
 * Copyright 2023 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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupGateway\SecondFactorOnlyBundle\Service\Gateway;
20
21
use Surfnet\SamlBundle\Http\PostBinding;
22
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
23
use Surfnet\StepupBundle\Value\SecondFactorType;
24
use Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor;
25
use Surfnet\StepupGateway\SamlStepupProviderBundle\Provider\ProviderRepository;
26
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Exception\ReceivedInvalidSubjectNameIdException;
27
use Symfony\Component\HttpFoundation\Request;
28
29
class ResponseValidator
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ResponseValidator
Loading history...
30
{
31
    /** @var SecondFactorTypeService */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
32
    private $secondFactorTypeService;
0 ignored issues
show
Coding Style introduced by
Private member variable "secondFactorTypeService" must be prefixed with an underscore
Loading history...
33
34
    /** @var ProviderRepository */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
35
    private $providerRepository;
0 ignored issues
show
Coding Style introduced by
Private member variable "providerRepository" must be prefixed with an underscore
Loading history...
36
37
    /** @var PostBinding  */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
38
    private $postBinding;
0 ignored issues
show
Coding Style introduced by
Private member variable "postBinding" must be prefixed with an underscore
Loading history...
39
40
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
41
        SecondFactorTypeService $secondFactorTypeService,
42
        ProviderRepository $providerRepository,
43
        PostBinding $postBinding
44
    ) {
45
        $this->secondFactorTypeService = $secondFactorTypeService;
46
        $this->providerRepository = $providerRepository;
47
        $this->postBinding = $postBinding;
48
    }
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Doc comment is empty
Loading history...
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $secondFactor should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $nameIdFromState should have a doc-comment as per coding-style.
Loading history...
51
     *
52
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
53
    public function validate(Request $request, SecondFactor $secondFactor, string $nameIdFromState)
54
    {
55
        $secondFactorType = new SecondFactorType($secondFactor->secondFactorType);
56
        $hasSamlResponse = $request->request->has('SAMLResponse');
57
        // When dealing with a GSSP response. It is advised to receive the SAML response through POST Binding,
58
        // testing the preconditions.
59
        if ($hasSamlResponse && $this->secondFactorTypeService->isGssf($secondFactorType)) {
60
            $provider = $this->providerRepository->get($secondFactorType->getSecondFactorType());
61
            // Receive the response via POST Binding, this will test all the regular pre-conditions
62
            $samlResponse = $this->postBinding->processResponse(
63
                $request,
64
                $provider->getRemoteIdentityProvider(),
65
                $provider->getServiceProvider()
66
            );
67
            $subjectNameIdFromResponse = $samlResponse->getNameId()->getValue();
68
            // Additionally test if the name id from the GSSP matches the SF identifier that we have in state
69
            if ($subjectNameIdFromResponse !== $secondFactor->secondFactorIdentifier) {
70
                throw new ReceivedInvalidSubjectNameIdException(
71
                    sprintf(
72
                        'The nameID received from the GSSP (%s) did not match the selected second factor (%s). This '.
73
                        'might be an indication someone is tampering with a GSSP. The authentication was started by %s',
74
                        $subjectNameIdFromResponse,
75
                        $secondFactor->secondFactorIdentifier,
76
                        $nameIdFromState
77
                    )
78
                );
79
            }
80
        }
81
    }
82
}
83