Skip to content

Commit 2fbbbdf

Browse files
AllyAlly
authored andcommitted
feat: add token factory
1 parent 181e6ba commit 2fbbbdf

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
namespace DesignMyNight\Mongodb\Passport;
4+
5+
use Zend\Diactoros\Response;
6+
use Zend\Diactoros\ServerRequest;
7+
use Lcobucci\JWT\Parser as JwtParser;
8+
use League\OAuth2\Server\AuthorizationServer;
9+
use \Laravel\Passport\ClientRepository;
10+
use Laravel\Passport\PersonalAccessTokenResult;
11+
use Laravel\Passport\TokenRepository;
12+
13+
class PersonalAccessTokenFactory
14+
{
15+
/**
16+
* The authorization server instance.
17+
*
18+
* @var \League\OAuth2\Server\AuthorizationServer
19+
*/
20+
protected $server;
21+
22+
/**
23+
* The client repository instance.
24+
*
25+
* @var \Laravel\Passport\ClientRepository
26+
*/
27+
protected $clients;
28+
29+
/**
30+
* The token repository instance.
31+
*
32+
* @var \Laravel\Passport\TokenRepository
33+
*/
34+
protected $tokens;
35+
36+
/**
37+
* The JWT token parser instance.
38+
*
39+
* @var \Lcobucci\JWT\Parser
40+
*/
41+
protected $jwt;
42+
43+
/**
44+
* Create a new personal access token factory instance.
45+
*
46+
* @param \League\OAuth2\Server\AuthorizationServer $server
47+
* @param \Laravel\Passport\ClientRepository $clients
48+
* @param \Laravel\Passport\TokenRepository $tokens
49+
* @param \Lcobucci\JWT\Parser $jwt
50+
* @return void
51+
*/
52+
public function __construct(AuthorizationServer $server,
53+
ClientRepository $clients,
54+
TokenRepository $tokens,
55+
JwtParser $jwt)
56+
{
57+
$this->jwt = $jwt;
58+
$this->tokens = $tokens;
59+
$this->server = $server;
60+
$this->clients = $clients;
61+
}
62+
63+
/**
64+
* Create a new personal access token.
65+
*
66+
* @param mixed $userId
67+
* @param string $name
68+
* @param array $scopes
69+
* @return \Laravel\Passport\PersonalAccessTokenResult
70+
*/
71+
public function make($userId, $name, array $scopes = [])
72+
{
73+
$response = $this->dispatchRequestToAuthorizationServer(
74+
$this->createRequest($this->clients->personalAccessClient(), $userId, $scopes)
75+
);
76+
77+
$token = tap($this->findAccessToken($response), function ($token) use ($userId, $name) {
78+
$token->forceFill([
79+
'user_id' => $userId,
80+
'name' => $name,
81+
])->save();
82+
});
83+
84+
return new PersonalAccessTokenResult(
85+
$response['access_token'], $token
86+
);
87+
}
88+
89+
/**
90+
* Create a request instance for the given client.
91+
*
92+
* @param \Laravel\Passport\Client $client
93+
* @param mixed $userId
94+
* @param array $scopes
95+
* @return \Zend\Diactoros\ServerRequest
96+
*/
97+
protected function createRequest($client, $userId, array $scopes)
98+
{
99+
return (new ServerRequest)->withParsedBody([
100+
'grant_type' => 'personal_access',
101+
'client_id' => $client->id,
102+
'client_secret' => $client->secret,
103+
'user_id' => $userId,
104+
'scope' => implode(' ', $scopes),
105+
]);
106+
}
107+
108+
/**
109+
* Dispatch the given request to the authorization server.
110+
*
111+
* @param \Zend\Diactoros\ServerRequest $request
112+
* @return array
113+
*/
114+
protected function dispatchRequestToAuthorizationServer(ServerRequest $request)
115+
{
116+
return json_decode($this->server->respondToAccessTokenRequest(
117+
$request, new Response
118+
)->getBody()->__toString(), true);
119+
}
120+
121+
/**
122+
* Get the access token instance for the parsed response.
123+
*
124+
* @param array $response
125+
* @return Token
126+
*/
127+
protected function findAccessToken(array $response)
128+
{
129+
return $this->tokens->find(
130+
$this->jwt->parse($response['access_token'])->getClaim('jti')
131+
);
132+
}
133+
}

0 commit comments

Comments
 (0)