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