|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Andrew Millington <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) Alex Bilbie |
|
6
|
|
|
* @license http://mit-license.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace OAuth2ServerExamples\Repositories; |
|
14
|
|
|
|
|
15
|
|
|
use DateTimeImmutable; |
|
16
|
|
|
use League\OAuth2\Server\Entities\DeviceCodeEntityInterface; |
|
17
|
|
|
use League\OAuth2\Server\Repositories\DeviceCodeRepositoryInterface; |
|
18
|
|
|
use OAuth2ServerExamples\Entities\ClientEntity; |
|
19
|
|
|
use OAuth2ServerExamples\Entities\DeviceCodeEntity; |
|
20
|
|
|
|
|
21
|
|
|
class DeviceCodeRepository implements DeviceCodeRepositoryInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getNewDeviceCode(): DeviceCodeEntityInterface |
|
27
|
|
|
{ |
|
28
|
|
|
return new DeviceCodeEntity(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function persistDeviceCode(DeviceCodeEntityInterface $deviceCodeEntity): void |
|
35
|
|
|
{ |
|
36
|
|
|
// Some logic to persist a new device code to a database |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function persistUser(DeviceCodeEntityInterface $deviceCodeEntity): void |
|
43
|
|
|
{ |
|
44
|
|
|
$user = $deviceCodeEntity->getUserIdentifier(); |
|
|
|
|
|
|
45
|
|
|
$approved = $deviceCodeEntity->getUserApproved(); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
// Some logic to persist user ID and approval status of the given device code entity to a database |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function persistLastPolledAt(DeviceCodeEntityInterface $deviceCodeEntity): void |
|
54
|
|
|
{ |
|
55
|
|
|
$lastPolledAt = $deviceCodeEntity->getLastPolledAt(); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
// Some logic to persist "last polled at" datetime of the given device code entity to a database |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getDeviceCodeEntityByDeviceCode($deviceCode): ?DeviceCodeEntityInterface |
|
64
|
|
|
{ |
|
65
|
|
|
$clientEntity = new ClientEntity(); |
|
66
|
|
|
$clientEntity->setIdentifier('myawesomeapp'); |
|
67
|
|
|
|
|
68
|
|
|
$deviceCodeEntity = new DeviceCodeEntity(); |
|
69
|
|
|
|
|
70
|
|
|
$deviceCodeEntity->setIdentifier($deviceCode); |
|
71
|
|
|
$deviceCodeEntity->setExpiryDateTime(new DateTimeImmutable('now +1 hour')); |
|
72
|
|
|
$deviceCodeEntity->setClient($clientEntity); |
|
73
|
|
|
|
|
74
|
|
|
// The user identifier should be set when the user authenticates on the |
|
75
|
|
|
// OAuth server, along with whether they approved the request |
|
76
|
|
|
$deviceCodeEntity->setUserApproved(true); |
|
77
|
|
|
$deviceCodeEntity->setUserIdentifier('1'); |
|
78
|
|
|
|
|
79
|
|
|
return $deviceCodeEntity; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function revokeDeviceCode($codeId): void |
|
86
|
|
|
{ |
|
87
|
|
|
// Some logic to revoke device code |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function isDeviceCodeRevoked($codeId): bool |
|
94
|
|
|
{ |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|