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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\saml\IdP;
use PDO;
use PDOStatement;
use SimpleSAML\Error;
use SimpleSAML\Store;
use SimpleSAML\Database;
use SimpleSAML\Configuration;
/**
* Helper class for working with persistent NameIDs stored in SQL datastore.
*
* @package SimpleSAMLphp
*/
class SQLNameID
{
public const TABLE_VERSION = 1;
public const DEFAULT_TABLE_PREFIX = '';
public const TABLE_SUFFIX = '_saml_PersistentNameID';
/**
* @param string $query
* @param array $params Parameters
* @param array $config
* @return \PDOStatement object
*/
private static function read(string $query, array $params = [], array $config = []): PDOStatement
{
if (!empty($config)) {
$database = Database::getInstance(Configuration::loadFromArray($config));
$stmt = $database->read($query, $params);
} else {
$store = self::getStore();
$stmt = $store->pdo->prepare($query);
$stmt->execute($params);
}
return $stmt;
}
/**
* @param string $query
* @param array $params Parameters
* @param array $config
* @return int|false The number of rows affected by the query or false on error.
*/
private static function write(string $query, array $params = [], array $config = [])
{
if (!empty($config)) {
$database = Database::getInstance(Configuration::loadFromArray($config));
$res = $database->write($query, $params);
} else {
$store = self::getStore();
$query = $store->pdo->prepare($query);
$res = $query->execute($params);
if ($res) {
$res = $query->rowCount();
}
}
return $res;
}
/**
* @param array $config
* @return string
*/
private static function tableName(array $config = []): string
{
$store = empty($config) ? self::getStore() : null;
$prefix = $store === null ? self::DEFAULT_TABLE_PREFIX : $store->prefix;
$table = $prefix . self::TABLE_SUFFIX;
return $table;
}
/**
* @param array $config
* @return void
*/
private static function create(array $config = []): void
{
$store = empty($config) ? self::getStore() : null;
$table = self::tableName($config);
if ($store === null) {
try {
self::createTable($table, $config);
} catch (\Exception $e) {
\SimpleSAML\Logger::debug('SQL persistent NameID table already exists.');
}
} elseif ($store->getTableVersion('saml_PersistentNameID') !== self::TABLE_VERSION) {
self::createTable($table);
$store->setTableVersion('saml_PersistentNameID', self::TABLE_VERSION);
}
}
/**
* @param string $query
* @param array $params
* @param array $config
* @return \PDOStatement
*/
private static function createAndRead(string $query, array $params = [], array $config = []): PDOStatement
{
self::create($config);
return self::read($query, $params, $config);
}
/**
* @param string $query
* @param array $params
* @param array $config
* @return int|false The number of rows affected by the query or false on error.
*/
private static function createAndWrite(string $query, array $params = [], array $config = [])
{
self::create($config);
return self::write($query, $params, $config);
}
/**
* Create NameID table in SQL.
*
* @param string $table The table name.
* @param array $config
* @return void
*/
private static function createTable(string $table, array $config = []): void
{
$query = 'CREATE TABLE ' . $table . ' (
_idp VARCHAR(256) NOT NULL,
_sp VARCHAR(256) NOT NULL,
_user VARCHAR(256) NOT NULL,
_value VARCHAR(40) NOT NULL,
UNIQUE (_idp, _sp, _user)
)';
self::write($query, [], $config);
$query = 'CREATE INDEX ' . $table . '_idp_sp ON ';
$query .= $table . ' (_idp, _sp)';
self::write($query, [], $config);
}
/**
* Retrieve the SQL datastore.
*
* @return \SimpleSAML\Store\SQL SQL datastore.
*/
private static function getStore(): Store\SQL
{
$store = Store::getInstance();
if (!($store instanceof Store\SQL)) {
throw new Error\Exception(
'SQL NameID store requires SimpleSAMLphp to be configured with a SQL datastore.'
);
}
return $store;
}
/**
* Add a NameID into 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).
* @param string $value The NameID value.
* @param array $config
* @return void
*/
public static function add(
string $idpEntityId,
string $spEntityId,
string $user,
string $value,
array $config = []
): void {
$params = [
'_idp' => $idpEntityId,
'_sp' => $spEntityId,
'_user' => $user,
'_value' => $value,
];
$query = 'INSERT INTO ' . self::tableName($config);
$query .= ' (_idp, _sp, _user, _value) VALUES(:_idp, :_sp, :_user, :_value)';
self::createAndWrite($query, $params, $config);
}
/**
* 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).
* @param array $config
* @return string|null $value The NameID value, or NULL of no NameID value was found.
*/
public static function get($idpEntityId, $spEntityId, $user, array $config = [])
{
assert(is_string($idpEntityId));
assert(is_string($spEntityId));
assert(is_string($user));
$params = [
'_idp' => $idpEntityId,
'_sp' => $spEntityId,
'_user' => $user,
];
$query = 'SELECT _value FROM ' . self::tableName($config);
$query .= ' WHERE _idp = :_idp AND _sp = :_sp AND _user = :_user';
$query = self::createAndRead($query, $params, $config);
$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).
* @param array $config
* @return void
*/
public static function delete($idpEntityId, $spEntityId, $user, array $config = [])
{
assert(is_string($idpEntityId));
assert(is_string($spEntityId));
assert(is_string($user));
$params = [
'_idp' => $idpEntityId,
'_sp' => $spEntityId,
'_user' => $user,
];
$query = 'DELETE FROM ' . self::tableName($config);
$query .= ' WHERE _idp = :_idp AND _sp = :_sp AND _user = :_user';
self::createAndWrite($query, $params, $config);
}
/**
* Retrieve all federated identities for an IdP-SP pair.
*
* @param string $idpEntityId The IdP entityID.
* @param string $spEntityId The SP entityID.
* @param array $config
* @return array Array of userid => NameID.
*/
public static function getIdentities($idpEntityId, $spEntityId, array $config = [])
{
assert(is_string($idpEntityId));
assert(is_string($spEntityId));
$params = [
'_idp' => $idpEntityId,
'_sp' => $spEntityId,
];
$query = 'SELECT _user, _value FROM ' . self::tableName($config);
$query .= ' WHERE _idp = :_idp AND _sp = :_sp';
$query = self::createAndRead($query, $params, $config);
$res = [];
while (($row = $query->fetch(PDO::FETCH_ASSOC)) !== false) {
$user = strval($row['_user']);
$value = strval($row['_value']);
$res[$user] = $value;
}
return $res;
}
}
|