1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* OAuth 2.0 Bearer Token Response. |
5
|
|
|
* |
6
|
|
|
* @author Alex Bilbie <[email protected]> |
7
|
|
|
* @copyright Copyright (c) Alex Bilbie |
8
|
|
|
* @license http://mit-license.org/ |
9
|
|
|
* |
10
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace League\OAuth2\Server\ResponseTypes; |
16
|
|
|
|
17
|
|
|
use League\OAuth2\Server\Entities\DeviceCodeEntityInterface; |
18
|
|
|
use LogicException; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
|
21
|
|
|
use function json_encode; |
22
|
|
|
use function time; |
23
|
|
|
|
24
|
|
|
class DeviceCodeResponse extends AbstractResponseType |
25
|
|
|
{ |
26
|
|
|
protected DeviceCodeEntityInterface $deviceCodeEntity; |
27
|
|
|
private bool $includeVerificationUriComplete = false; |
28
|
|
|
private bool $includeInterval = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
6 |
|
public function generateHttpResponse(ResponseInterface $response): ResponseInterface |
34
|
|
|
{ |
35
|
6 |
|
$expireDateTime = $this->deviceCodeEntity->getExpiryDateTime()->getTimestamp(); |
36
|
|
|
|
37
|
6 |
|
$responseParams = [ |
38
|
6 |
|
'device_code' => $this->deviceCodeEntity->getIdentifier(), |
39
|
6 |
|
'user_code' => $this->deviceCodeEntity->getUserCode(), |
40
|
6 |
|
'verification_uri' => $this->deviceCodeEntity->getVerificationUri(), |
41
|
6 |
|
'expires_in' => $expireDateTime - time(), |
42
|
6 |
|
]; |
43
|
|
|
|
44
|
6 |
|
if ($this->includeVerificationUriComplete === true) { |
45
|
1 |
|
$responseParams['verification_uri_complete'] = $this->deviceCodeEntity->getVerificationUriComplete(); |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
if ($this->includeInterval === true) { |
49
|
2 |
|
$responseParams['interval'] = $this->deviceCodeEntity->getInterval(); |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
$responseParams = json_encode($responseParams); |
53
|
|
|
|
54
|
6 |
|
if ($responseParams === false) { |
55
|
|
|
throw new LogicException('Error encountered JSON encoding response parameters'); |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
$response = $response |
59
|
6 |
|
->withStatus(200) |
60
|
6 |
|
->withHeader('pragma', 'no-cache') |
61
|
6 |
|
->withHeader('cache-control', 'no-store') |
62
|
6 |
|
->withHeader('content-type', 'application/json; charset=UTF-8'); |
63
|
|
|
|
64
|
6 |
|
$response->getBody()->write($responseParams); |
65
|
|
|
|
66
|
6 |
|
return $response; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
6 |
|
public function setDeviceCodeEntity(DeviceCodeEntityInterface $deviceCodeEntity): void |
73
|
|
|
{ |
74
|
6 |
|
$this->deviceCodeEntity = $deviceCodeEntity; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function includeVerificationUriComplete(): void |
78
|
|
|
{ |
79
|
1 |
|
$this->includeVerificationUriComplete = true; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
public function includeInterval(): void |
83
|
|
|
{ |
84
|
2 |
|
$this->includeInterval = true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add custom fields to your Bearer Token response here, then override |
89
|
|
|
* AuthorizationServer::getResponseType() to pull in your version of |
90
|
|
|
* this class rather than the default. |
91
|
|
|
* |
92
|
|
|
* @return array<array-key,mixed> |
|
|
|
|
93
|
|
|
*/ |
94
|
|
|
protected function getExtraParams(DeviceCodeEntityInterface $deviceCode): array |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|