1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\OAuth2\Storage\RefreshTokens; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use DateTime; |
7
|
|
|
use kalanis\OAuth2\IKeyGenerator; |
8
|
|
|
use kalanis\OAuth2\Storage\Clients\IClient; |
9
|
|
|
use kalanis\OAuth2\Storage\Exceptions\InvalidRefreshTokenException; |
10
|
|
|
use kalanis\OAuth2\Storage\ITokenFacade; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* RefreshToken |
15
|
|
|
* @package kalanis\OAuth2\Token |
16
|
|
|
*/ |
17
|
1 |
|
class RefreshTokenFacade implements ITokenFacade |
18
|
|
|
{ |
19
|
1 |
|
public function __construct( |
20
|
|
|
private readonly int $lifetime, |
21
|
|
|
private readonly IKeyGenerator $keyGenerator, |
22
|
|
|
private readonly IRefreshTokenStorage $storage, |
23
|
|
|
) |
24
|
|
|
{ |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create new refresh token |
29
|
|
|
*/ |
30
|
|
|
public function create(IClient $client, string|int|null $userId, array $scope = []): IRefreshToken |
31
|
|
|
{ |
32
|
1 |
|
$expires = new DateTime; |
33
|
1 |
|
$expires->modify('+' . $this->lifetime . ' seconds'); |
34
|
1 |
|
$refreshToken = new RefreshToken( |
35
|
1 |
|
$this->keyGenerator->generate(), |
36
|
|
|
$expires, |
37
|
1 |
|
$client->getId(), |
38
|
|
|
$userId |
39
|
|
|
); |
40
|
1 |
|
$this->storage->store($refreshToken); |
41
|
|
|
|
42
|
1 |
|
return $refreshToken; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get refresh token entity |
47
|
|
|
*/ |
48
|
|
|
public function getEntity(string $token): IRefreshToken |
49
|
|
|
{ |
50
|
1 |
|
$entity = $this->storage->getValidRefreshToken($token); |
51
|
1 |
|
if (!$entity) { |
52
|
1 |
|
$this->storage->remove($token); |
53
|
1 |
|
throw new InvalidRefreshTokenException; |
54
|
|
|
} |
55
|
1 |
|
return $entity; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get token identifier name |
60
|
|
|
*/ |
61
|
|
|
public function getIdentifier(): string |
62
|
|
|
{ |
63
|
1 |
|
return self::REFRESH_TOKEN; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get token lifetime |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
public function getLifetime(): int |
71
|
|
|
{ |
72
|
|
|
return $this->lifetime; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get storage |
77
|
|
|
* @return IRefreshTokenStorage |
78
|
|
|
*/ |
79
|
|
|
public function getStorage(): IRefreshTokenStorage |
80
|
|
|
{ |
81
|
|
|
return $this->storage; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|