1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the tiqr project. |
4
|
|
|
* |
5
|
|
|
* The tiqr project aims to provide an open implementation for |
6
|
|
|
* authentication using mobile devices. It was initiated by |
7
|
|
|
* SURFnet and developed by Egeniq. |
8
|
|
|
* |
9
|
|
|
* More information: http://www.tiqr.org |
10
|
|
|
* |
11
|
|
|
* @author Ivo Jansch <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @package tiqr |
14
|
|
|
* |
15
|
|
|
* @license New BSD License - See LICENSE file for details. |
16
|
|
|
* |
17
|
|
|
* @copyright (C) 2010-2012 SURFnet BV |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class implementing a factory to retrieve user secrets. |
24
|
|
|
* |
25
|
|
|
* @author lineke |
26
|
|
|
*/ |
27
|
|
|
class Tiqr_UserSecretStorage |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Get a secret storage of a certain type |
31
|
|
|
* |
32
|
|
|
* @param String $type The type of storage to create. Supported |
33
|
|
|
* types are 'file', 'pdo' or 'oathservice'. |
34
|
|
|
* @param array $options The options to pass to the storage |
35
|
|
|
* instance. See the documentation |
36
|
|
|
* in the UserSecretStorage/ subdirectory for |
37
|
|
|
* options per type. |
38
|
|
|
* |
39
|
|
|
* @return Tiqr_UserSecretStorage_Interface |
40
|
|
|
* @throws RuntimeException If an unknown type is requested. |
41
|
|
|
* @throws RuntimeException When the options configuration array misses a required parameter |
42
|
|
|
*/ |
43
|
11 |
|
public static function getSecretStorage(string $type, LoggerInterface $logger, array $options): Tiqr_UserSecretStorage_Interface |
44
|
|
|
{ |
45
|
|
|
// If not provided in config, we fall back to dummy (no) encryption |
46
|
11 |
|
$encryptionType = $config['encryption']['type'] ?? 'dummy'; |
|
|
|
|
47
|
|
|
// If the encryption configuration is not configured, we fall back to an empty encryption configuration |
48
|
11 |
|
$encryptionOptions = $config['encryption'] ?? []; |
49
|
11 |
|
$encryption = Tiqr_UserStorage_Encryption::getEncryption($logger, $encryptionType, $encryptionOptions); |
50
|
|
|
|
51
|
11 |
|
switch ($type) { |
52
|
11 |
|
case "file": |
53
|
2 |
|
if (!array_key_exists('path', $options)) { |
54
|
1 |
|
throw new RuntimeException('The path is missing in the UserSecretStorage configuration'); |
55
|
|
|
} |
56
|
1 |
|
$path = $options['path']; |
57
|
1 |
|
return new Tiqr_UserSecretStorage_File($encryption, $path, $logger); |
58
|
9 |
|
case "pdo": |
59
|
|
|
// Input validation on the required configuration options |
60
|
5 |
|
if (!array_key_exists('dsn', $options)) { |
61
|
1 |
|
throw new RuntimeException('The dsn is missing in the UserSecretStorage configuration'); |
62
|
|
|
} |
63
|
4 |
|
if (!array_key_exists('username', $options)) { |
64
|
1 |
|
throw new RuntimeException('The username is missing in the UserSecretStorage configuration'); |
65
|
|
|
} |
66
|
3 |
|
if (!array_key_exists('password', $options)) { |
67
|
1 |
|
throw new RuntimeException('The password is missing in the UserSecretStorage configuration'); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
$tableName = $options['table'] ?? 'tiqrusersecret'; |
71
|
2 |
|
$dsn = $options['dsn']; |
72
|
2 |
|
$userName = $options['username']; |
73
|
2 |
|
$password = $options['password']; |
74
|
|
|
|
75
|
|
|
try { |
76
|
2 |
|
$handle = new PDO($dsn, $userName, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) ); |
77
|
|
|
} catch (PDOException $e) { |
78
|
|
|
$logger->error( |
79
|
|
|
sprintf('Unable to establish a PDO connection. Error message from PDO: %s', $e->getMessage()) |
80
|
|
|
); |
81
|
|
|
throw ReadWriteException::fromOriginalException($e); |
82
|
|
|
} |
83
|
2 |
|
return new Tiqr_UserSecretStorage_Pdo($encryption, $logger, $handle, $tableName); |
84
|
|
|
|
85
|
4 |
|
case "oathserviceclient": |
86
|
3 |
|
if (!array_key_exists('apiURL', $options)) { |
87
|
1 |
|
throw new RuntimeException('The apiURL is missing in the UserSecretStorage configuration'); |
88
|
|
|
} |
89
|
2 |
|
if (!array_key_exists('consumerKey', $options)) { |
90
|
1 |
|
throw new RuntimeException('The consumerKey is missing in the UserSecretStorage configuration'); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$apiClient = new Tiqr_API_Client(); |
94
|
1 |
|
$apiClient->setBaseURL($options['apiURL']); |
95
|
1 |
|
$apiClient->setConsumerKey($options['consumerKey']); |
96
|
1 |
|
return new Tiqr_UserSecretStorage_OathServiceClient($apiClient, $logger); |
97
|
|
|
} |
98
|
1 |
|
throw new RuntimeException(sprintf('Unable to create a UserSecretStorage instance of type: %s', $type)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|