1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class MemoryStorage |
4
|
|
|
* |
5
|
|
|
* @created 09.07.2017 |
6
|
|
|
* @author Smiley <[email protected]> |
7
|
|
|
* @copyright 2017 Smiley |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace chillerlan\OAuth\Storage; |
12
|
|
|
|
13
|
|
|
use chillerlan\OAuth\Core\AccessToken; |
14
|
|
|
use function array_key_exists; |
15
|
|
|
use function array_keys; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Implements a memory storage adapter. Memory storage is not persistent as tokens are only stored during script runtime. |
19
|
|
|
*/ |
20
|
|
|
class MemoryStorage extends OAuthStorageAbstract{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* the token storage array |
24
|
|
|
*/ |
25
|
|
|
protected array $tokens = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* the CSRF state storage array |
29
|
|
|
*/ |
30
|
|
|
protected array $states = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritDoc |
34
|
|
|
*/ |
35
|
|
|
public function storeAccessToken(AccessToken $token, string $service = null):OAuthStorageInterface{ |
36
|
|
|
$this->tokens[$this->getServiceName($service)] = $token; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
|
|
public function getAccessToken(string $service = null):AccessToken{ |
45
|
|
|
|
46
|
|
|
if($this->hasAccessToken($service)){ |
47
|
|
|
return $this->tokens[$this->getServiceName($service)]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
throw new OAuthStorageException('token not found'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function hasAccessToken(string $service = null):bool{ |
57
|
|
|
$serviceName = $this->getServiceName($service); |
58
|
|
|
|
59
|
|
|
return isset($this->tokens[$serviceName]) && $this->tokens[$serviceName] instanceof AccessToken; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function clearAccessToken(string $service = null):OAuthStorageInterface{ |
66
|
|
|
$serviceName = $this->getServiceName($service); |
67
|
|
|
|
68
|
|
|
if(array_key_exists($serviceName, $this->tokens)){ |
69
|
|
|
unset($this->tokens[$serviceName]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
|
|
public function clearAllAccessTokens():OAuthStorageInterface{ |
79
|
|
|
|
80
|
|
|
foreach(array_keys($this->tokens) as $service){ |
81
|
|
|
unset($this->tokens[$service]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->tokens = []; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
|
|
public function storeCSRFState(string $state, string $service = null):OAuthStorageInterface{ |
93
|
|
|
$this->states[$this->getServiceName($service)] = $state; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
|
|
public function getCSRFState(string $service = null):string{ |
102
|
|
|
|
103
|
|
|
if($this->hasCSRFState($service)){ |
104
|
|
|
return $this->states[$this->getServiceName($service)]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new OAuthStorageException('state not found'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
*/ |
113
|
|
|
public function hasCSRFState(string $service = null):bool{ |
114
|
|
|
$serviceName = $this->getServiceName($service); |
115
|
|
|
|
116
|
|
|
return isset($this->states[$serviceName]) && null !== $this->states[$serviceName]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritDoc |
121
|
|
|
*/ |
122
|
|
|
public function clearCSRFState(string $service = null):OAuthStorageInterface{ |
123
|
|
|
$serviceName = $this->getServiceName($service); |
124
|
|
|
|
125
|
|
|
if(array_key_exists($serviceName, $this->states)){ |
126
|
|
|
unset($this->states[$serviceName]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
*/ |
135
|
|
|
public function clearAllCSRFStates():OAuthStorageInterface{ |
136
|
|
|
$this->states = []; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|