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