Completed
Pull Request — develop (#93)
by Boy
04:06
created

AuthnContextClassRefValidationService::validate()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.5806
cc 4
eloc 16
nc 4
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\StepupGateway\SecondFactorOnlyBundle\Service;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupBundle\Service\LoaResolutionService;
23
24
final class AuthnContextClassRefValidationService
25
{
26
    /**
27
     * @var LoggerInterface
28
     */
29
    private $logger;
30
31
    /**
32
     * @var LoaAliasLookupService
33
     */
34
    private $loaAliasLookup;
35
36
    /**
37
     * @var LoaResolutionService
38
     */
39
    private $loaResolution;
40
41
    public function __construct(
42
        LoggerInterface $logger,
43
        LoaAliasLookupService $loaAliasLookup,
44
        LoaResolutionService $loaResolution
45
    ) {
46
        $this->logger = $logger;
47
        $this->loaAliasLookup = $loaAliasLookup;
48
        $this->loaResolution = $loaResolution;
49
    }
50
51
    /**
52
     * @param LoggerInterface $logger
53
     * @return $this
54
     */
55
    public function with(LoggerInterface $logger)
56
    {
57
        $this->logger = $logger;
58
        return $this;
59
    }
60
61
    /**
62
     * Validate that a given ACCR was provided and has a valid LOA alias.
63
     *
64
     * Returns the LOA id.
65
     *
66
     * @param string $authnContextClassRef
67
     *   AuthnContextClassRef provided in AuthnRequest.
68
     *
69
     * @return string
70
     *   LOA Id
71
     */
72
    public function validate($authnContextClassRef) {
73
        if (empty($authnContextClassRef)) {
74
            $this->logger->info( 'No LOA requested, sending response with status Requester Error');
75
            return '';
76
        }
77
78
        $loaId = $this->loaAliasLookup->findLoaIdByAlias($authnContextClassRef);
79
80
        if (!$loaId) {
81
            $this->logger->info(sprintf(
82
                'Requested required Loa "%s" does not have a second factor alias',
83
                $authnContextClassRef
84
            ));
85
            return '';
86
        }
87
88
        if (!$this->loaResolution->hasLoa($loaId)) {
89
            $this->logger->info(sprintf(
90
                'Requested required Loa "%s" does not exist',
91
                $authnContextClassRef
92
            ));
93
            return '';
94
        }
95
96
        return $loaId;
97
    }
98
}
99