forked from Tiqr/tiqr-server-libphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateStorage.php
93 lines (86 loc) · 3.48 KB
/
StateStorage.php
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
<?php
/**
* This file is part of the tiqr project.
*
* The tiqr project aims to provide an open implementation for
* authentication using mobile devices. It was initiated by
* SURFnet and developed by Egeniq.
*
* More information: http://www.tiqr.org
*
* @author Ivo Jansch <[email protected]>
*
* @package tiqr
*
* @license New BSD License - See LICENSE file for details.
*
* @copyright (C) 2010-2011 SURFnet BV
*/
use Psr\Log\LoggerInterface;
/**
* Utility class to create specific StateStorage instances.
* StateStorage is used to store temporary information used during
* enrollment and authentication sessions.
* @author ivo
*
*/
class Tiqr_StateStorage
{
/**
* Get a storage of a certain type
* @param String $type The type of storage to create. Supported
* types are 'file', 'pdo' and 'memcache'.
* @param array $options The options to pass to the storage
* instance. See the documentation
* in the StateStorage/ subdirectory for
* options per type.
* @throws RuntimeException If an unknown type is requested.
* @throws RuntimeException When the options configuration array misses a required parameter
*
*/
public static function getStorage(string $type, array $options, LoggerInterface $logger)
{
switch ($type) {
case "file":
if (!array_key_exists('path', $options)) {
throw new RuntimeException('The path is missing in the StateStorage configuration');
}
$instance = new Tiqr_StateStorage_File($options['path'], $logger);
$instance->init();
return $instance;
case "memcache":
$instance = new Tiqr_StateStorage_Memcache($options, $logger);
$instance->init();
return $instance;
case "pdo":
$requiredOptions = ['table', 'dsn', 'username', 'password'];
foreach ($requiredOptions as $requirement) {
if (!array_key_exists($requirement, $options)) {
throw new RuntimeException(
sprintf(
'Please configure the "%s" configuration option for the PDO state storage',
$requirement
)
);
}
}
$pdoInstance = new PDO(
$options['dsn'],
$options['username'],
$options['password'],
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);
// Set a hard-coded default for the probability the expired state is removed
// 0.1 translates to a 10% chance the garbage collection is executed
$cleanupProbability = 0.1;
if (array_key_exists('cleanup_probability', $options) && is_numeric($options['cleanup_probability'])) {
$cleanupProbability = $options['cleanup_probability'];
}
$tablename = $options['table'];
$instance = new Tiqr_StateStorage_Pdo($pdoInstance, $logger, $tablename, $cleanupProbability);
$instance->init();
return $instance;
}
throw new RuntimeException(sprintf('Unable to create a StateStorage instance of type: %s', $type));
}
}