-
Notifications
You must be signed in to change notification settings - Fork 79
/
Storage.php
152 lines (132 loc) · 3.66 KB
/
Storage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace dokuwiki\plugin\oauth;
use OAuth\Common\Storage\Exception\TokenNotFoundException;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Token\TokenInterface;
/**
* Implements custom handling for storing tokens
*/
class Storage implements TokenStorageInterface
{
/** @var string */
protected $storageId;
/**
* @param string $storageId The ID identifying the user
*/
public function __construct($storageId)
{
$this->storageId = $storageId;
}
/**
* The path to the file where tokens for this service and user are stored
*
* @param string $service
* @return string
*/
protected function getServiceFile($service)
{
return getCacheName($this->storageId . $service, '.oauth');
}
/**
* Load the data from disk
*
* @param string $service
* @return array
*/
protected function loadServiceFile($service)
{
$file = $this->getServiceFile($service);
if (file_exists($file)) {
return unserialize(io_readFile($file, false));
} else {
return [];
}
}
/**
* Store the data to disk
*
* @param string $service
* @param array $data
*/
protected function saveServiceFile($service, $data)
{
$file = $this->getServiceFile($service);
io_saveFile($file, serialize($data));
}
/** @inheritDoc */
public function retrieveAccessToken($service)
{
$data = $this->loadServiceFile($service);
if (!isset($data['token'])) {
throw new TokenNotFoundException('No token found in storage');
}
return $data['token'];
}
/** @inheritDoc */
public function storeAccessToken($service, TokenInterface $token)
{
$data = $this->loadServiceFile($service);
$data['token'] = $token;
$this->saveServiceFile($service, $data);
}
/** @inheritDoc */
public function hasAccessToken($service)
{
$data = $this->loadServiceFile($service);
return isset($data['token']);
}
/** @inheritDoc */
public function clearToken($service)
{
$data = $this->loadServiceFile($service);
if (isset($data['token'])) unset($data['token']);
$this->saveServiceFile($service, $data);
return $this;
}
/** @inheritDoc */
public function clearAllTokens()
{
// TODO: Implement clearAllTokens() method.
return $this;
}
/** @inheritDoc */
public function storeAuthorizationState($service, $state)
{
$data = $this->loadServiceFile($service);
$data['state'] = $state;
$this->saveServiceFile($service, $data);
return $this;
}
/** @inheritDoc */
public function hasAuthorizationState($service)
{
$data = $this->loadServiceFile($service);
return isset($data['state']);
}
/**
* @inheritDoc
* @throws TokenNotFoundException
*/
public function retrieveAuthorizationState($service)
{
$data = $this->loadServiceFile($service);
if (!isset($data['state'])) {
throw new TokenNotFoundException('No state found in storage');
}
return $data['state'];
}
/** @inheritDoc */
public function clearAuthorizationState($service)
{
$data = $this->loadServiceFile($service);
if (isset($data['state'])) unset($data['state']);
$this->saveServiceFile($service, $data);
return $this;
}
/** @inheritDoc */
public function clearAllAuthorizationStates()
{
// TODO: Implement clearAllAuthorizationStates() method.
return $this;
}
}