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-2011 SURFnet BV |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* File based implementation to store session state data. |
25
|
|
|
* Note that it is more secure to use a memory based storage such as memcached. |
26
|
|
|
* This implementation is mostly for demo, test or experimental setups that |
27
|
|
|
* do not have access to a memcache instance. |
28
|
|
|
* |
29
|
|
|
* This StateStorage implementation has no options, files are always stored |
30
|
|
|
* in /tmp and prefixed with tiqr_state_* |
31
|
|
|
* |
32
|
|
|
* @author ivo |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
class Tiqr_StateStorage_File implements Tiqr_StateStorage_StateStorageInterface |
36
|
|
|
{ |
37
|
|
|
private $logger; |
38
|
|
|
|
39
|
|
|
private $path; |
40
|
|
|
|
41
|
|
|
public function __construct(string $path, LoggerInterface $logger) |
42
|
|
|
{ |
43
|
|
|
$this->logger = $logger; |
44
|
|
|
$this->path = $path; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setValue($key, $value, $expire=0) |
48
|
|
|
{ |
49
|
|
|
$envelope = array("expire"=>$expire, |
50
|
|
|
"createdAt"=>time(), |
51
|
|
|
"value"=>$value); |
52
|
|
|
$filename = $this->getFilenameByKey($key); |
53
|
|
|
|
54
|
|
|
if (!file_put_contents($filename, serialize($envelope))) { |
55
|
|
|
throw new ReadWriteException(sprintf('Unable to store "%s" state to the filesystem', $key)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $key; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* (non-PHPdoc) |
63
|
|
|
* @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::unsetValue() |
64
|
|
|
*/ |
65
|
|
|
public function unsetValue($key) |
66
|
|
|
{ |
67
|
|
|
$filename = $this->getFilenameByKey($key); |
68
|
|
|
if (file_exists($filename) && !unlink($filename)) { |
69
|
|
|
throw new ReadWriteException( |
70
|
|
|
sprintf( |
71
|
|
|
'Unable to unlink the "%s" value from state storage on filesystem', |
72
|
|
|
$key |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* (non-PHPdoc) |
80
|
|
|
* @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::getValue() |
81
|
|
|
*/ |
82
|
|
|
public function getValue($key) |
83
|
|
|
{ |
84
|
|
|
$filename = $this->getFilenameByKey($key); |
85
|
|
|
if (file_exists($filename)) { |
86
|
|
|
$envelope = unserialize(file_get_contents($filename), ['allowed_classes' => false]); |
87
|
|
|
// This data is time-limited. If it's too old we discard it. |
88
|
|
|
if (($envelope["expire"] != 0) && time() - $envelope["createdAt"] > $envelope["expire"]) { |
89
|
|
|
$this->unsetValue($key); |
90
|
|
|
$this->logger->notice('Unable to retrieve the state storage value, it is expired'); |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
return $envelope["value"]; |
94
|
|
|
} |
95
|
|
|
$this->logger->notice('Unable to retrieve the state storage value, file not found'); |
96
|
|
|
return NULL; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getPath(): string |
100
|
|
|
{ |
101
|
|
|
if (substr($this->path, -1)!=="/") { |
102
|
|
|
return $this->path . "/"; |
103
|
|
|
} |
104
|
|
|
return $this->path; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Determine the name of a temporary file to hold the contents of $key |
109
|
|
|
*/ |
110
|
|
|
private function getFilenameByKey(string $key): string |
111
|
|
|
{ |
112
|
|
|
return sprintf( |
113
|
|
|
"%stiqr_state_%s", |
114
|
|
|
$this->getPath(), |
115
|
|
|
strtr(base64_encode($key), '+/', '-_') |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function init() |
120
|
|
|
{ |
121
|
|
|
# Nothing to do here |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|