1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Models\Clients\PersonalAccess; |
4
|
|
|
|
5
|
|
|
use ByTIC\Hello\Models\AccessTokens\Token; |
6
|
|
|
use ByTIC\Hello\Models\Clients\Client; |
7
|
|
|
use ByTIC\Hello\Utility\ModelsHelper; |
8
|
|
|
use Laminas\Diactoros\Response; |
9
|
|
|
use Laminas\Diactoros\ServerRequest; |
10
|
|
|
use Lcobucci\JWT\Parser as JwtParser; |
11
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
12
|
|
|
use Nip\Container\Container; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class TokenFactory |
16
|
|
|
* @package ByTIC\Hello\Models\Clients\PersonalAccess |
17
|
|
|
*/ |
18
|
|
|
class TokenFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The authorization server instance. |
22
|
|
|
* |
23
|
|
|
* @var \League\OAuth2\Server\AuthorizationServer |
24
|
|
|
*/ |
25
|
|
|
protected $server; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Client |
29
|
|
|
*/ |
30
|
|
|
protected $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Client |
34
|
|
|
*/ |
35
|
|
|
protected $jwt; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* TokenFactory constructor |
39
|
|
|
* |
40
|
|
|
* @param \League\OAuth2\Server\AuthorizationServer $server |
41
|
|
|
* @param $client |
42
|
|
|
*/ |
43
|
1 |
|
public function __construct(AuthorizationServer $server = null, $client = null, JwtParser $jwt = null) |
44
|
|
|
{ |
45
|
1 |
|
$this->server = $server ? $server : Container::getInstance()->get(AuthorizationServer::class); |
46
|
1 |
|
$this->client = $client ? $client : ClientsManager::get(); |
47
|
1 |
|
$this->jwt = $jwt ? $jwt : new JwtParser(); |
|
|
|
|
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a new personal access token. |
53
|
|
|
*a |
54
|
|
|
* @param mixed $userId |
55
|
|
|
* @param string $name |
56
|
|
|
* @param array $scopes |
57
|
|
|
* @return \Laravel\Passport\PersonalAccessTokenResult |
|
|
|
|
58
|
|
|
*/ |
59
|
1 |
|
public function make($userId, $name, array $scopes = []) |
60
|
|
|
{ |
61
|
1 |
|
$response = $this->dispatchRequestToAuthorizationServer( |
62
|
1 |
|
$this->createRequest($this->client, $userId, $scopes) |
63
|
|
|
); |
64
|
|
|
|
65
|
1 |
|
$token = $this->findAccessToken($response); |
66
|
|
|
|
67
|
1 |
|
return $token; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a request instance for the given client. |
72
|
|
|
* |
73
|
|
|
* @param Client $client |
74
|
|
|
* @param mixed $userId |
75
|
|
|
* @param array $scopes |
76
|
|
|
* @return Request |
|
|
|
|
77
|
|
|
*/ |
78
|
1 |
|
protected function createRequest($client, $userId, array $scopes) |
79
|
|
|
{ |
80
|
1 |
|
return (new ServerRequest())->withParsedBody([ |
|
|
|
|
81
|
1 |
|
'grant_type' => 'personal_access', |
82
|
1 |
|
'client_id' => $client->getIdentifier(), |
83
|
1 |
|
'client_secret' => $client->getSecret(), |
84
|
1 |
|
'user_id' => $userId, |
85
|
1 |
|
'scope' => implode(' ', $scopes), |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Dispatch the given request to the authorization server. |
91
|
|
|
* |
92
|
|
|
* @param Request $request |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
1 |
|
protected function dispatchRequestToAuthorizationServer(ServerRequest $request) |
96
|
|
|
{ |
97
|
1 |
|
return json_decode( |
98
|
1 |
|
$this->server->respondToAccessTokenRequest( |
99
|
1 |
|
$request, |
100
|
1 |
|
new Response() |
101
|
1 |
|
)->getBody()->__toString(), |
102
|
1 |
|
true |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the access token instance for the parsed response. |
108
|
|
|
* |
109
|
|
|
* @param array $response |
110
|
|
|
* @return Token |
111
|
|
|
*/ |
112
|
|
|
protected function findAccessToken(array $response) |
113
|
|
|
{ |
114
|
|
|
return ModelsHelper::accessTokens()->getByIdentifier( |
|
|
|
|
115
|
|
|
$this->jwt->parse($response['access_token'])->getClaim('jti') |
|
|
|
|
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..