1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eole\Sandstone\OAuth2\Storage; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Server\Entity\AccessTokenEntity; |
6
|
|
|
use League\OAuth2\Server\Entity\ScopeEntity; |
7
|
|
|
use League\OAuth2\Server\Entity\SessionEntity; |
8
|
|
|
use League\OAuth2\Server\Storage\AccessTokenInterface; |
9
|
|
|
use League\OAuth2\Server\Storage\AbstractStorage; |
10
|
|
|
|
11
|
|
|
class AccessToken extends AbstractStorage implements AccessTokenInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $accessTokensDir; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $accessTokensDir |
20
|
|
|
*/ |
21
|
|
|
public function __construct($accessTokensDir) |
22
|
|
|
{ |
23
|
|
|
$this->accessTokensDir = $accessTokensDir; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@InheritDoc} |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function get($token) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
if (!file_exists($this->accessTokensDir.'/'.$token)) { |
32
|
|
|
return null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$tokenContent = explode('-', file_get_contents($this->accessTokensDir.'/'.$token)); |
36
|
|
|
$sessionId = $tokenContent[0]; |
37
|
|
|
$expireTime = $tokenContent[1]; |
38
|
|
|
|
39
|
|
|
$session = new SessionEntity($this->server); |
40
|
|
|
$session->setId($sessionId); |
41
|
|
|
|
42
|
|
|
$accessToken = new AccessTokenEntity($this->server); |
43
|
|
|
$accessToken->setId($token); |
44
|
|
|
$accessToken->setExpireTime(intval($expireTime)); |
45
|
|
|
$accessToken->setSession($session); |
46
|
|
|
|
47
|
|
|
return $accessToken; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@InheritDoc} |
52
|
|
|
*/ |
53
|
|
|
public function getScopes(AccessTokenEntity $token) |
54
|
|
|
{ |
55
|
|
|
throw new \Eole\Sandstone\OAuth2\Exception\NotImplementedException(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@InheritDoc} |
60
|
|
|
*/ |
61
|
|
|
public function create($token, $expireTime, $sessionId) |
62
|
|
|
{ |
63
|
|
|
file_put_contents($this->accessTokensDir.'/'.$token, $sessionId.'-'.$expireTime); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@InheritDoc} |
68
|
|
|
*/ |
69
|
|
|
public function associateScope(AccessTokenEntity $token, ScopeEntity $scope) |
70
|
|
|
{ |
71
|
|
|
$token->associateScope($scope); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@InheritDoc} |
76
|
|
|
*/ |
77
|
|
|
public function delete(AccessTokenEntity $token) |
78
|
|
|
{ |
79
|
|
|
if (file_exists($this->accessTokensDir.'/'.$token->getId())) { |
80
|
|
|
unlink($this->accessTokensDir.'/'.$token->getId()); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.