1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\ldapPasswordReset; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use SimpleSAML\Configuration; |
9
|
|
|
use SimpleSAML\Store; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\Uid\Uuid; |
12
|
|
|
|
13
|
|
|
use function strval; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This class generates and stores tokens to be used in magic links |
17
|
|
|
* |
18
|
|
|
* @package simplesamlphp/simplesamlphp-module-ldapPasswordReset |
19
|
|
|
*/ |
20
|
|
|
class TokenStorage |
21
|
|
|
{ |
22
|
|
|
/** @var \SimpleSAML\Configuration */ |
23
|
|
|
protected Configuration $config; |
24
|
|
|
|
25
|
|
|
/** @var \SimpleSAML\Configuration */ |
26
|
|
|
protected Configuration $moduleConfig; |
27
|
|
|
|
28
|
|
|
/** @var \Symfony\Component\HttpFoundation\Request */ |
29
|
|
|
protected Request $request; |
30
|
|
|
|
31
|
|
|
/** @var \SimpleSAML\Store\StoreInterface */ |
32
|
|
|
protected Store\StoreInterface $store; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use. |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Configuration $config) |
39
|
|
|
{ |
40
|
|
|
$store = Store\StoreFactory::getInstance($config->getString('store.type')); |
41
|
|
|
if ($store === false) { |
|
|
|
|
42
|
|
|
throw new Exception('Using `phpsession` as a store is not supported when using this module.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->store = $store; |
46
|
|
|
$this->config = $config; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Store token |
52
|
|
|
* |
53
|
|
|
* @param string $token |
54
|
|
|
* @param string $mail |
55
|
|
|
* @param string $session |
56
|
|
|
* @param int $validUntil |
57
|
|
|
* @param string|null $referer |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function storeToken(string $token, string $mail, string $session, int $validUntil, ?string $referer): void |
61
|
|
|
{ |
62
|
|
|
$this->store->set( |
63
|
|
|
'magiclink', |
64
|
|
|
$token, |
65
|
|
|
['mail' => $mail, 'session' => $session, 'referer' => $referer], |
66
|
|
|
$validUntil, |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieve stored token |
73
|
|
|
* |
74
|
|
|
* @param string $token |
75
|
|
|
* @return array<mixed>|null |
76
|
|
|
*/ |
77
|
|
|
public function retrieveToken(string $token): ?array |
78
|
|
|
{ |
79
|
|
|
return $this->store->get('magiclink', $token); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Delete stored token |
85
|
|
|
* |
86
|
|
|
* @param string $token |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function deleteToken(string $token): void |
90
|
|
|
{ |
91
|
|
|
$this->store->delete('magiclink', $token); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Generate token |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function generateToken(): string |
101
|
|
|
{ |
102
|
|
|
return strval(Uuid::v4()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|