1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2020 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\Behat\Repository; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use PDO; |
23
|
|
|
use Ramsey\Uuid\Uuid; |
24
|
|
|
use Surfnet\StepupBundle\Value\VettingType; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A poor mans repository, a pdo connection to the test database is established in the constructor |
28
|
|
|
*/ |
29
|
|
|
class SecondFactorRepository |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var Connection |
33
|
|
|
*/ |
34
|
|
|
private $connection; |
35
|
|
|
|
36
|
|
|
public function __construct(Connection $connection) |
37
|
|
|
{ |
38
|
|
|
$this->connection = $connection; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function create($nameId, $tokenType, $institution, bool $selfAsserted = false, $identifier = null) |
42
|
|
|
{ |
43
|
|
|
$uuid = Uuid::uuid4()->toString(); |
44
|
|
|
|
45
|
|
|
// If an identifier is not important, simply use the UUID, otherwise use the provide one |
46
|
|
|
if (!$identifier) { |
47
|
|
|
$identifier = $uuid; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$data = [ |
51
|
|
|
'identityId' => $uuid, |
52
|
|
|
'nameId' => $nameId, |
53
|
|
|
'institution' => $institution, |
54
|
|
|
'secondFactorId' => $uuid, |
55
|
|
|
'secondFactorType' => $tokenType, |
56
|
|
|
'secondFactorIdentifier' => $identifier, |
57
|
|
|
'id' => $uuid, |
58
|
|
|
'displayLocale' => 'en_GB', |
59
|
|
|
'identityVetted' => $selfAsserted ? 0 : 1, |
60
|
|
|
]; |
61
|
|
|
$sql = <<<SQL |
62
|
|
|
INSERT INTO second_factor ( |
63
|
|
|
identity_id, |
64
|
|
|
name_id, |
65
|
|
|
institution, |
66
|
|
|
second_factor_id, |
67
|
|
|
second_factor_type, |
68
|
|
|
second_factor_identifier, |
69
|
|
|
id, |
70
|
|
|
display_locale, |
71
|
|
|
identity_vetted |
72
|
|
|
) |
73
|
|
|
VALUES ( |
74
|
|
|
:identityId, |
75
|
|
|
:nameId, |
76
|
|
|
:institution, |
77
|
|
|
:secondFactorId, |
78
|
|
|
:secondFactorType, |
79
|
|
|
:secondFactorIdentifier, |
80
|
|
|
:id, |
81
|
|
|
:displayLocale, |
82
|
|
|
:identityVetted |
83
|
|
|
) |
84
|
|
|
SQL; |
85
|
|
|
$stmt = $this->connection->prepare($sql); |
86
|
|
|
if ($stmt->execute($data)) { |
87
|
|
|
return $data; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
throw new Exception( |
91
|
|
|
sprintf( |
92
|
|
|
'Unable to insert the new second_factor. PDO raised this error: "%s"', |
93
|
|
|
$stmt->errorInfo()[2] |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function findBy(string $nameId, string $secondFactorType): array |
99
|
|
|
{ |
100
|
|
|
$sql = 'SELECT * FROM `second_factor` WHERE `name_id` = :nameId AND `second_factor_type` = :type'; |
101
|
|
|
$stmt = $this->connection->prepare($sql); |
102
|
|
|
$stmt->execute([ |
103
|
|
|
'nameId' => $nameId, |
104
|
|
|
'type' => $secondFactorType |
105
|
|
|
]); |
106
|
|
|
if ($result = $stmt->fetch(PDO::FETCH_ASSOC)) { |
107
|
|
|
return $result; |
108
|
|
|
} |
109
|
|
|
throw new Exception(sprintf('Unable to find %s SF token for %s', $secondFactorType, $nameId)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function has(string $nameId, string $secondFactorType): bool |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
|
|
$this->findBy($nameId, $secondFactorType); |
116
|
|
|
return true; |
117
|
|
|
} catch (Exception $e) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|