|
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\Encoding\JoseEncoder; |
|
|
|
|
|
|
11
|
|
|
use Lcobucci\JWT\Token\RegisteredClaims; |
|
12
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
|
13
|
|
|
use Nip\Container\Container; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class TokenFactory |
|
17
|
|
|
* @package ByTIC\Hello\Models\Clients\PersonalAccess |
|
18
|
|
|
*/ |
|
19
|
|
|
class TokenFactory |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The authorization server instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @var \League\OAuth2\Server\AuthorizationServer |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $server; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Client |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $client; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Client |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $jwt; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* TokenFactory constructor |
|
40
|
|
|
* |
|
41
|
|
|
* @param \League\OAuth2\Server\AuthorizationServer $server |
|
42
|
|
|
* @param $client |
|
43
|
1 |
|
*/ |
|
44
|
|
|
public function __construct(AuthorizationServer $server = null, $client = null, \Lcobucci\JWT\Parser $jwt = null) |
|
45
|
1 |
|
{ |
|
46
|
1 |
|
$this->server = $server ? $server : Container::getInstance()->get(AuthorizationServer::class); |
|
47
|
1 |
|
$this->client = $client ? $client : ClientsManager::get(); |
|
48
|
1 |
|
if (!$jwt) { |
|
49
|
|
|
if (class_exists(\Lcobucci\JWT\Parser::class)) { |
|
50
|
|
|
$jwt = new \Lcobucci\JWT\Parser(); |
|
51
|
|
|
} else { |
|
52
|
|
|
$jwt = new \Lcobucci\JWT\Token\Parser(new JoseEncoder()); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
$this->jwt = $jwt; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
1 |
|
/** |
|
60
|
|
|
* Create a new personal access token. |
|
61
|
1 |
|
*a |
|
62
|
1 |
|
* @param mixed $userId |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* @param array $scopes |
|
65
|
1 |
|
* @return \Laravel\Passport\PersonalAccessTokenResult |
|
|
|
|
|
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function make($userId, $name, array $scopes = []) |
|
68
|
|
|
{ |
|
69
|
|
|
$response = $this->dispatchRequestToAuthorizationServer( |
|
70
|
|
|
$this->createRequest($this->client, $userId, $scopes) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$token = $this->findAccessToken($response); |
|
74
|
|
|
|
|
75
|
|
|
return $token; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
/** |
|
79
|
|
|
* Create a request instance for the given client. |
|
80
|
1 |
|
* |
|
81
|
1 |
|
* @param Client $client |
|
82
|
1 |
|
* @param mixed $userId |
|
83
|
1 |
|
* @param array $scopes |
|
84
|
1 |
|
* @return ServerRequest|\Psr\Http\Message\ServerRequestInterface |
|
85
|
1 |
|
*/ |
|
86
|
|
|
protected function createRequest($client, $userId, array $scopes) |
|
87
|
|
|
{ |
|
88
|
|
|
return (new ServerRequest())->withParsedBody([ |
|
89
|
|
|
'grant_type' => 'personal_access', |
|
90
|
|
|
'client_id' => $client->getIdentifier(), |
|
91
|
|
|
'client_secret' => $client->getSecret(), |
|
92
|
|
|
'user_id' => $userId, |
|
93
|
|
|
'scope' => implode(' ', $scopes), |
|
94
|
|
|
]); |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
/** |
|
98
|
1 |
|
* Dispatch the given request to the authorization server. |
|
99
|
1 |
|
* |
|
100
|
1 |
|
* @param ServerRequest $request |
|
101
|
1 |
|
* @return array |
|
102
|
1 |
|
*/ |
|
103
|
|
|
protected function dispatchRequestToAuthorizationServer(ServerRequest $request) |
|
104
|
|
|
{ |
|
105
|
|
|
return json_decode( |
|
106
|
|
|
$this->server->respondToAccessTokenRequest( |
|
107
|
|
|
$request, |
|
108
|
|
|
new Response() |
|
109
|
|
|
)->getBody()->__toString(), |
|
110
|
|
|
true |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the access token instance for the parsed response. |
|
116
|
|
|
* |
|
117
|
|
|
* @param array $response |
|
118
|
|
|
* @return Token |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function findAccessToken(array $response) |
|
121
|
|
|
{ |
|
122
|
|
|
$token = $this->jwt->parse($response['access_token']); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
return ModelsHelper::accessTokens()->getByIdentifier( |
|
|
|
|
|
|
125
|
|
|
$token->claims()->get(RegisteredClaims::ID) |
|
|
|
|
|
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths