Completed
Pull Request — develop (#227)
by Michiel
04:22 queued 02:14
created

SelfVetMarshaller   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 47
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A isAllowed() 0 21 5
1
<?php
2
3
/**
4
 * Copyright 2021 SURF B.V.
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\Service;
20
21
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
22
use Surfnet\StepupBundle\Value\SecondFactorType;
23
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactor;
25
26
class SelfVetMarshaller implements VettingMarshaller
27
{
28
    /**
29
     * @var SecondFactorService
30
     */
31
    private $secondFactorService;
32
33
    /**
34
     * @var SecondFactorTypeService
35
     */
36
    private $secondFactorTypeService;
37
38
    public function __construct(
39
        SecondFactorService $secondFactorService,
40
        SecondFactorTypeService $secondFactorTypeService
41
    ) {
42
        $this->secondFactorService = $secondFactorService;
43
        $this->secondFactorTypeService = $secondFactorTypeService;
44
    }
45
46
    /**
47
     * You are allowed to self vet when:
48
     * 1. You already have a vetted token
49
     * 2. The vetted token has higher LoA (or equal) to the one being vetted
50
     */
51
    public function isAllowed(Identity $identity, string $secondFactorId): bool
52
    {
53
        $vettedSecondFactors = $this->secondFactorService->findVettedByIdentity($identity->id);
54
        if ($vettedSecondFactors->getTotalItems() === 0) {
55
            return false;
56
        }
57
        $candidateToken = $this->secondFactorService->findOneVerified($secondFactorId);
58
        if ($candidateToken) {
59
            /** @var VettedSecondFactor $authoringSecondFactor */
60
            foreach ($vettedSecondFactors->getElements() as $authoringSecondFactor) {
61
                $hasSuitableToken = $this->secondFactorTypeService->hasEqualOrLowerLoaComparedTo(
62
                    new SecondFactorType($candidateToken->type),
63
                    new SecondFactorType($authoringSecondFactor->type)
64
                );
65
                if ($hasSuitableToken) {
66
                    return true;
67
                }
68
            }
69
        }
70
        return false;
71
    }
72
}
73