1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
<?php
/**
* Helper class for working with persistent NameIDs stored in SQL datastore.
*
* @package SimpleSAMLphp
*/
class sspmod_saml_IdP_SQLNameID {
/**
* Create NameID table in SQL, if it is missing.
*
* @param SimpleSAML_Store_SQL $store The datastore.
*/
private static function createTable(SimpleSAML_Store_SQL $store) {
if ($store->getTableVersion('saml_PersistentNameID') === 1) {
return;
}
$query = 'CREATE TABLE ' . $store->prefix . '_saml_PersistentNameID (
_idp VARCHAR(256) NOT NULL,
_sp VARCHAR(256) NOT NULL,
_user VARCHAR(256) NOT NULL,
_value VARCHAR(40) NOT NULL,
UNIQUE (_idp, _sp, _user)
)';
$store->pdo->exec($query);
$query = 'CREATE INDEX ' . $store->prefix . '_saml_PersistentNameID_idp_sp ON ' . $store->prefix . '_saml_PersistentNameID (_idp, _sp)';
$store->pdo->exec($query);
$store->setTableVersion('saml_PersistentNameID', 1);
}
/**
* Retrieve the SQL datastore.
*
* Will also ensure that the NameID table is present.
*
* @return SimpleSAML_Store_SQL SQL datastore.
*/
private static function getStore() {
$store = SimpleSAML_Store::getInstance();
if (!($store instanceof SimpleSAML_Store_SQL)) {
throw new SimpleSAML_Error_Exception('SQL NameID store requires SimpleSAMLphp to be configured with a SQL datastore.');
}
self::createTable($store);
return $store;
}
/**
* Add a NameID into the database.
*
* @param SimpleSAML_Store_SQL $store The data store.
* @param string $idpEntityId The IdP entityID.
* @param string $spEntityId The SP entityID.
* @param string $user The user's unique identificator (e.g. username).
* @param string $value The NameID value.
*/
public static function add($idpEntityId, $spEntityId, $user, $value) {
assert('is_string($idpEntityId)');
assert('is_string($spEntityId)');
assert('is_string($user)');
assert('is_string($value)');
$store = self::getStore();
$params = array(
'_idp' => $idpEntityId,
'_sp' => $spEntityId,
'_user' => $user,
'_value' => $value,
);
$query = 'INSERT INTO ' . $store->prefix . '_saml_PersistentNameID (_idp, _sp, _user, _value) VALUES(:_idp, :_sp, :_user, :_value)';
$query = $store->pdo->prepare($query);
$query->execute($params);
}
/**
* Retrieve a NameID into from database.
*
* @param string $idpEntityId The IdP entityID.
* @param string $spEntityId The SP entityID.
* @param string $user The user's unique identificator (e.g. username).
* @return string|NULL $value The NameID value, or NULL of no NameID value was found.
*/
public static function get($idpEntityId, $spEntityId, $user) {
assert('is_string($idpEntityId)');
assert('is_string($spEntityId)');
assert('is_string($user)');
$store = self::getStore();
$params = array(
'_idp' => $idpEntityId,
'_sp' => $spEntityId,
'_user' => $user,
);
$query = 'SELECT _value FROM ' . $store->prefix . '_saml_PersistentNameID WHERE _idp = :_idp AND _sp = :_sp AND _user = :_user';
$query = $store->pdo->prepare($query);
$query->execute($params);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row === FALSE) {
// No NameID found
return NULL;
}
return $row['_value'];
}
/**
* Delete a NameID from the database.
*
* @param string $idpEntityId The IdP entityID.
* @param string $spEntityId The SP entityID.
* @param string $user The user's unique identificator (e.g. username).
*/
public static function delete($idpEntityId, $spEntityId, $user) {
assert('is_string($idpEntityId)');
assert('is_string($spEntityId)');
assert('is_string($user)');
$store = self::getStore();
$params = array(
'_idp' => $idpEntityId,
'_sp' => $spEntityId,
'_user' => $user,
);
$query = 'DELETE FROM ' . $store->prefix . '_saml_PersistentNameID WHERE _idp = :_idp AND _sp = :_sp AND _user = :_user';
$query = $store->pdo->prepare($query);
$query->execute($params);
}
/**
* Retrieve all federated identities for an IdP-SP pair.
*
* @param string $idpEntityId The IdP entityID.
* @param string $spEntityId The SP entityID.
* @return array Array of userid => NameID.
*/
public static function getIdentities($idpEntityId, $spEntityId) {
assert('is_string($idpEntityId)');
assert('is_string($spEntityId)');
$query = 'SELECT _user, _value FROM ' . $store->prefix . '_saml_PersistentNameID WHERE _idp = :_idp AND _sp = :_sp';
$query = $store->pdo->prepare($query);
$query->execute($params);
$res = array();
while ( ($row = $query->fetch(PDO::FETCH_ASSOC)) !== FALSE) {
$res[$row['_user']] = $row['_value'];
}
return $res;
}
}
|