1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Surfnet\StepupGateway\Behat\Repository; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PDO; |
7
|
|
|
use Ramsey\Uuid\Uuid; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A poor mans repository, a pdo connection to the test database is established in the constructor |
11
|
|
|
*/ |
12
|
|
|
class SamlEntityRepository |
13
|
|
|
{ |
14
|
|
|
const SP_ACS_LOCATION = 'https://gateway.stepup.example.com/test/authentication/consume-assertion'; |
15
|
|
|
|
16
|
|
|
const SP_ADFS_SSO_LOCATION = 'https://gateway.stepup.example.com/test/authentication/adfs/sso'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Connection |
20
|
|
|
*/ |
21
|
|
|
private $connection; |
22
|
|
|
|
23
|
|
|
public function __construct(Connection $connection) |
24
|
|
|
{ |
25
|
|
|
$this->connection = $connection; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function createSpIfNotExists($entityId, $certificate, $sfoEnabled = false) |
29
|
|
|
{ |
30
|
|
|
// Does the SP exist? |
31
|
|
|
$stmt = $this->connection->prepare('SELECT * FROM saml_entity WHERE entity_id=:entityId LIMIT 1'); |
32
|
|
|
$stmt->bindParam('entityId', $entityId); |
33
|
|
|
$stmt->execute(); |
34
|
|
|
if ($stmt->rowCount() === 0) { |
35
|
|
|
// If not, create it |
36
|
|
|
$uuid = Uuid::uuid4()->toString(); |
37
|
|
|
$type = 'sp'; |
38
|
|
|
$configuration['acs'] = [self::SP_ACS_LOCATION]; |
|
|
|
|
39
|
|
|
$configuration['public_key'] = $certificate; |
40
|
|
|
$configuration['loa'] = ['__default__' => 'http://stepup.example.com/assurance/loa1']; |
41
|
|
|
$configuration['second_factor_only'] = $sfoEnabled; |
42
|
|
|
$configuration['set_sso_cookie_on_2fa'] = true; |
43
|
|
|
$configuration['allow_sso_on_2fa'] = true; |
44
|
|
|
$configuration['second_factor_only_nameid_patterns'] = [ |
45
|
|
|
'urn:collab:person:stepup.example.com:admin', |
46
|
|
|
'urn:collab:person:stepup.example.com:*', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
$data = [ |
50
|
|
|
'entityId' => $entityId, |
51
|
|
|
'type' => $type, |
52
|
|
|
'configuration' => json_encode($configuration), |
53
|
|
|
'id' => $uuid, |
54
|
|
|
]; |
55
|
|
|
$sql = <<<SQL |
56
|
|
|
INSERT INTO saml_entity ( |
57
|
|
|
`entity_id`, |
58
|
|
|
`type`, |
59
|
|
|
`configuration`, |
60
|
|
|
`id` |
61
|
|
|
) |
62
|
|
|
VALUES ( |
63
|
|
|
:entityId, |
64
|
|
|
:type, |
65
|
|
|
:configuration, |
66
|
|
|
:id |
67
|
|
|
) |
68
|
|
|
SQL; |
69
|
|
|
$stmt = $this->connection->prepare($sql); |
70
|
|
|
if ($stmt->execute($data)) { |
71
|
|
|
return $data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new Exception('Unable to insert the new SP saml_entity'); |
75
|
|
|
} else { |
76
|
|
|
// Return the SP data |
77
|
|
|
$results = $stmt->fetchAll(); |
78
|
|
|
$result = $results[0]; |
79
|
|
|
$data = [ |
80
|
|
|
'entityId' => $result['entity_id'], |
81
|
|
|
'type' => $result['type'], |
82
|
|
|
'configuration' => $result['configuration'], |
83
|
|
|
'id' => $result['id'], |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
return $data; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function createIdpIfNotExists($entityId, $certificate) |
91
|
|
|
{ |
92
|
|
|
// Does the SP exist? |
93
|
|
|
$stmt = $this->connection->prepare('SELECT * FROM saml_entity WHERE entity_id=:entityId LIMIT 1'); |
94
|
|
|
$stmt->bindParam('entityId', $entityId, PDO::PARAM_STR); |
95
|
|
|
$stmt->execute(); |
96
|
|
|
if ($stmt->rowCount() === 0) { |
97
|
|
|
// If not, create it |
98
|
|
|
$uuid = Uuid::uuid4()->toString(); |
99
|
|
|
$type = 'idp'; |
100
|
|
|
|
101
|
|
|
$configuration['public_key'] = $certificate; |
|
|
|
|
102
|
|
|
|
103
|
|
|
$data = [ |
104
|
|
|
'entityId' => $entityId, |
105
|
|
|
'type' => $type, |
106
|
|
|
'configuration' => json_encode($configuration), |
107
|
|
|
'id' => $uuid, |
108
|
|
|
]; |
109
|
|
|
$sql = <<<SQL |
110
|
|
|
INSERT INTO saml_entity ( |
111
|
|
|
`entity_id`, |
112
|
|
|
`type`, |
113
|
|
|
`configuration`, |
114
|
|
|
`id` |
115
|
|
|
) |
116
|
|
|
VALUES ( |
117
|
|
|
:entityId, |
118
|
|
|
:type, |
119
|
|
|
:configuration, |
120
|
|
|
:id |
121
|
|
|
) |
122
|
|
|
SQL; |
123
|
|
|
$stmt = $this->connection->prepare($sql); |
124
|
|
|
if ($stmt->execute($data)) { |
125
|
|
|
return $data; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
throw new Exception('Unable to insert the new SP saml_entity'); |
129
|
|
|
} else { |
130
|
|
|
// Return the SP data |
131
|
|
|
$results = $stmt->fetchAll(); |
132
|
|
|
$result = $results[0]; |
133
|
|
|
$data = [ |
134
|
|
|
'entityId' => $result['entity_id'], |
135
|
|
|
'type' => $result['type'], |
136
|
|
|
'configuration' => $result['configuration'], |
137
|
|
|
'id' => $result['id'], |
138
|
|
|
]; |
139
|
|
|
return $data; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|