Passed
Push — feature/add-azure-mfa-registra... ( feab5b...3767e0 )
by
unknown
01:55
created

SecondFactor::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
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\GatewayBundle\Entity;
20
21
use Doctrine\ORM\Mapping as ORM;
22
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
23
use Surfnet\StepupBundle\Value\Loa;
24
use Surfnet\StepupBundle\Value\SecondFactorType;
25
use Surfnet\StepupBundle\Value\VettingType;
26
27
/**
28
 * WARNING: Any schema change made to this entity should also be applied to the Middleware SecondFactor entity!
29
 *          Migrations are managed by Middleware.
30
 *
31
 * @see Surfnet\StepupMiddleware\GatewayBundle\Entity\SecondFactor (in OpenConext/Stepup-Middleware project)
32
 *
33
 * @ORM\Entity(repositoryClass="Surfnet\StepupGateway\GatewayBundle\Entity\DoctrineSecondFactorRepository")
34
 * @ORM\Table(
35
 *      indexes={
36
 *          @ORM\Index(name="idx_secondfactor_nameid", columns={"name_id"}),
37
 *      }
38
 * )
39
 */
40
class SecondFactor
41
{
42
    /**
43
     * @var int
44
     *
45
     * @ORM\Id
46
     * @ORM\Column(length=36)
47
     */
48
    public $id;
49
50
    /**
51
     * @var string
52
     *
53
     * @ORM\Id
54
     * @ORM\Column(length=36)
55
     */
56
    public $identityId;
57
58
    /**
59
     * @var string
60
     *
61
     * @ORM\Column(length=200)
62
     */
63
    public $nameId;
64
65
    /**
66
     * @var string
67
     *
68
     * @ORM\Column(length=200)
69
     */
70
    public $institution;
71
72
    /**
73
     * In which language to display any second factor verification screens.
74
     *
75
     * @var string
76
     *
77
     * @ORM\Column
78
     */
79
    public $displayLocale;
80
81
    /**
82
     * @var string
83
     *
84
     * @ORM\Column(length=36)
85
     */
86
    public $secondFactorId;
87
88
    /**
89
     * @var string
90
     *
91
     * @ORM\Column(length=50)
92
     */
93
    public $secondFactorType;
94
95
    /**
96
     * @var string
97
     *
98
     * @ORM\Column(length=255)
99
     */
100
    public $secondFactorIdentifier;
101
102
    /**
103
     * This boolean indicates if the second factor token was vetted
104
     * using one of the vetting types that are considered 'identity-vetted'.
105
     * That in turn means if the owner of the second factor token has its
106
     * identity vetted (verified) by a RA(A) at the service desk. This trickles
107
     * down to the self-vet vetting type. As the token used for self vetting
108
     * was RA vetted.
109
     *
110
     * @ORM\Column(type="boolean", options={"default":"1"})
111
     */
112
    public $identityVetted;
113
114
    /**
115
     * No new second factors should be created by the gateway
116
     */
117
    final private function __construct()
118
    {
119
    }
120
121
    public static function create(string $id, string $type, string $displayLocale)
122
    {
123
        $sf = new self();
124
        $sf->secondFactorId = $id;
125
        $sf->secondFactorType = $type;
126
        $sf->displayLocale = $displayLocale;
127
        return $sf;
128
    }
129
130
    public function canSatisfy(Loa $loa, SecondFactorTypeService $service): bool
131
    {
132
        $secondFactorType = new SecondFactorType($this->secondFactorType);
133
        $vettingType = $this->determineVettingType($this->identityVetted);
134
        return $service->canSatisfy($secondFactorType, $loa, $vettingType);
135
    }
136
137
    /**
138
     * @param SecondFactorTypeService $service
139
     * @return float
140
     */
141
    public function getLoaLevel(SecondFactorTypeService $service): float
142
    {
143
        $secondFactorType = new SecondFactorType($this->secondFactorType);
144
        $vettingType = $this->determineVettingType($this->identityVetted);
145
        $level = $service->getLevel($secondFactorType, $vettingType);
146
        return $level;
147
    }
148
149
    private function determineVettingType(bool $identityVetted): VettingType
150
    {
151
        if ($identityVetted) {
152
            return new VettingType(VettingType::TYPE_ON_PREMISE);
153
        }
154
        return new VettingType(VettingType::TYPE_SELF_ASSERTED_REGISTRATION);
155
    }
156
}
157