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
|
|
|
* This user storage implementation implements a simple user's secret storage using json files. |
24
|
|
|
* This is mostly for demonstration and development purposes. In a production environment |
25
|
|
|
* please supply your own implementation that hosts the data in your user database OR |
26
|
|
|
* in a secure (e.g. hardware encrypted) storage. |
27
|
|
|
* @author ivo |
28
|
|
|
* |
29
|
|
|
* @see Tiqr_UserSecretStorage::getSecretStorage() |
30
|
|
|
* @see Tiqr_UserSecretStorage_Interface |
31
|
|
|
* |
32
|
|
|
* Supported options: |
33
|
|
|
* path : Path to the directory where the user data is stored |
34
|
|
|
* |
35
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
class Tiqr_UserSecretStorage_File implements Tiqr_UserSecretStorage_Interface |
38
|
|
|
{ |
39
|
|
|
use UserSecretStorageTrait; |
40
|
|
|
use FileTrait; |
41
|
|
|
|
42
|
|
|
private $path; |
43
|
|
|
|
44
|
8 |
|
public function __construct( |
45
|
|
|
Tiqr_UserSecretStorage_Encryption_Interface $encryption, |
46
|
|
|
string $path, |
47
|
|
|
LoggerInterface $logger, |
48
|
|
|
array $decryption = array() |
49
|
|
|
) { |
50
|
|
|
// See UserSecretStorageTrait |
51
|
8 |
|
$this->encryption = $encryption; |
52
|
8 |
|
$this->decryption = $decryption; |
53
|
8 |
|
$this->logger = $logger; |
54
|
|
|
|
55
|
|
|
// See FileTrait |
56
|
8 |
|
$this->path = $path; |
57
|
8 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the user's secret |
61
|
|
|
* |
62
|
|
|
* @param String $userId |
63
|
|
|
* |
64
|
|
|
* @return String The user's secret |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
3 |
|
private function getUserSecret(string $userId): string |
|
|
|
|
68
|
|
|
{ |
69
|
3 |
|
if ($data = $this->_loadUser($userId)) { |
70
|
3 |
|
if (isset($data["secret"])) { |
71
|
3 |
|
return $data["secret"]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
$this->logger->error(sprintf('User or user secret not found in secret storage (file) for user "%s"', $userId)); |
75
|
|
|
throw new RuntimeException('User or user secret not found in secret storage (File)'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Store a secret for a user |
80
|
|
|
* |
81
|
|
|
* @param String $userId |
82
|
|
|
* @param String $secret |
83
|
|
|
* @throws Exception |
84
|
|
|
*/ |
85
|
3 |
|
private function setUserSecret(string $userId, string $secret): void |
|
|
|
|
86
|
|
|
{ |
87
|
3 |
|
$data=array(); |
88
|
3 |
|
if ($this->_userExists($userId)) { |
89
|
1 |
|
$data = $this->_loadUser($userId); |
90
|
|
|
} |
91
|
3 |
|
$data["secret"] = $secret; |
92
|
3 |
|
$this->_saveUser($userId, $data); |
93
|
3 |
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.