Completed
Push — feature/refactor-gateway-contr... ( 48680d...09b176 )
by
unknown
03:25
created

SecondFactorRespondService::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\SecondFactorOnlyBundle\Service\Gateway;
20
21
use Psr\Log\LoggerInterface;
22
use SAML2\Response;
23
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
24
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext;
25
use Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService;
26
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Exception\InvalidSecondFactorMethodException;
27
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Saml\ResponseFactory;
28
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Service\LoaAliasLookupService;
29
use Surfnet\StepupBundle\Service\LoaResolutionService;
30
31
class SecondFactorRespondService
32
{
33
    /** @var LoggerInterface */
34
    private $logger;
35
36
    /** @var LoaResolutionService */
37
    private $loaResolutionService;
38
39
    /** @var LoaAliasLookupService */
40
    private $loaAliasLookupService;
41
42
    /** @var ResponseFactory */
43
    private $responseFactory;
44
45
    /** @var SecondFactorService */
46
    private $secondFactorService;
47
48
    /** @var SecondFactorTypeService */
49
    private $secondFactorTypeService;
50
51
    /**
52
     * SecondFactorRespondService constructor.
53
     * @param LoggerInterface $logger
54
     * @param LoaResolutionService $loaResolutionService
55
     * @param LoaAliasLookupService $loaAliasLookupService
56
     * @param ResponseFactory $responseFactory
57
     * @param SecondFactorService $secondFactorService
58
     * @param SecondFactorTypeService $secondFactorTypeService
59
     */
60
    public function __construct(
61
        LoggerInterface $logger,
62
        LoaResolutionService $loaResolutionService,
63
        LoaAliasLookupService $loaAliasLookupService,
64
        ResponseFactory $responseFactory,
65
        SecondFactorService $secondFactorService,
66
        SecondFactorTypeService $secondFactorTypeService
67
    ) {
68
        $this->logger = $logger;
69
        $this->loaResolutionService = $loaResolutionService;
70
        $this->loaAliasLookupService = $loaAliasLookupService;
71
        $this->responseFactory = $responseFactory;
72
        $this->secondFactorService = $secondFactorService;
73
        $this->secondFactorTypeService = $secondFactorTypeService;
74
    }
75
76
77
    /**
78
     * Send a SAML response back to the service provider.
79
     *
80
     * Second factor verification is finished. This method builds a AuthnResponse
81
     * to send back to the service provider in response to the AuthnRequest received in
82
     * the SecondFactorLoginService.
83
     *
84
     * @param ResponseContext $responseContext
85
     * @return Response
86
     */
87
    public function respond(ResponseContext $responseContext)
88
    {
89
        $this->logger->notice('Creating second-factor-only Response');
90
91
        $selectedSecondFactorUuid = $responseContext->getSelectedSecondFactor();
92
        if (!$selectedSecondFactorUuid) {
93
            throw new InvalidSecondFactorMethodException(
94
                'Cannot verify possession of an unknown second factor.'
95
            );
96
        }
97
98
        if (!$responseContext->isSecondFactorVerified()) {
99
            throw new InvalidSecondFactorMethodException(
100
                'Second factor was not verified'
101
            );
102
        }
103
104
        $secondFactor = $this->secondFactorService->findByUuid($selectedSecondFactorUuid);
105
        $grantedLoa = $this->loaResolutionService
106
            ->getLoaByLevel($secondFactor->getLoaLevel($this->secondFactorTypeService));
107
108
        $authnContextClassRef = $this->loaAliasLookupService->findAliasByLoa($grantedLoa);
0 ignored issues
show
Bug introduced by
It seems like $grantedLoa defined by $this->loaResolutionServ...condFactorTypeService)) on line 105 can be null; however, Surfnet\StepupGateway\Se...rvice::findAliasByLoa() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
109
110
        $response = $this->responseFactory->createSecondFactorOnlyResponse(
111
            $responseContext->getIdentityNameId(),
112
            $responseContext->getDestination(),
113
            $authnContextClassRef
0 ignored issues
show
Security Bug introduced by
It seems like $authnContextClassRef defined by $this->loaAliasLookupSer...AliasByLoa($grantedLoa) on line 108 can also be of type false; however, Surfnet\StepupGateway\Se...ondFactorOnlyResponse() does only seem to accept string|null, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
114
        );
115
116
        $this->logger->notice(sprintf(
117
            'Responding to request "%s" with newly created response "%s"',
118
            $responseContext->getInResponseTo(),
119
            $response->getId()
120
        ));
121
122
        return $response;
123
    }
124
125
    /**
126
     * Reset the state of the response
127
     *
128
     * @param ResponseContext $responseContext
129
     */
130
    public function resetRespondState(ResponseContext $responseContext)
131
    {
132
        $responseContext->responseSent();
133
    }
134
}
135