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