SecondFactorOnlyNameIdValidationService::with()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 SURFnet bv
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace Surfnet\StepupGateway\SecondFactorOnlyBundle\Service;
19
20
use Psr\Log\LoggerInterface;
21
use Surfnet\StepupGateway\GatewayBundle\Service\SamlEntityService;
22
23
final class SecondFactorOnlyNameIdValidationService
24
{
25
    /**
26
     * @var LoggerInterface
27
     */
28
    private $logger;
29
30
    /**
31
     * @var SamlEntityService
32
     */
33
    private $entityService;
34
35
    public function __construct(LoggerInterface $logger, SamlEntityService $entityService)
36
    {
37
        $this->logger = $logger;
38
        $this->entityService = $entityService;
39
    }
40
41
    /**
42
     * @param LoggerInterface $logger
43
     * @return $this
44
     */
45
    public function with(LoggerInterface $logger)
46
    {
47
        $this->logger = $logger;
48
        return $this;
49
    }
50
51
    /**
52
     * Is the given SP allowed to authenticate via Second Factor Only for the given NameID?
53
     */
54
    public function validate(string $spEntityId, string $nameId): bool
55
    {
56
        if (!$nameId) {
57
            $this->logger->notice(
58
                'No NameID provided, sending response with status Requester Error'
59
            );
60
            return false;
61
        }
62
63
        $serviceProvider = $this->entityService->getServiceProvider($spEntityId);
64
65
        if (!$serviceProvider->isAllowedToUseSecondFactorOnlyFor($nameId)) {
0 ignored issues
show
introduced by
The method isAllowedToUseSecondFactorOnlyFor() does not exist on Surfnet\SamlBundle\Entity\ServiceProvider. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        if (!$serviceProvider->/** @scrutinizer ignore-call */ isAllowedToUseSecondFactorOnlyFor($nameId)) {
Loading history...
66
            $this->logger->notice(
67
                sprintf(
68
                    'SP "%s" may not use SecondFactorOnly mode for nameid "%s", sending response with status Requester Error',
69
                    $spEntityId,
70
                    $nameId
71
                )
72
            );
73
            return false;
74
        }
75
76
        $this->logger->notice(
77
            sprintf(
78
                'SP "%s" is allowed to use SecondFactorOnly mode for nameid "%s"',
79
                $spEntityId,
80
                $nameId
81
            )
82
        );
83
84
        return true;
85
    }
86
}
87