-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ally
authored and
Ally
committed
Sep 17, 2019
1 parent
181e6ba
commit 2fbbbdf
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
namespace DesignMyNight\Mongodb\Passport; | ||
|
||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\ServerRequest; | ||
use Lcobucci\JWT\Parser as JwtParser; | ||
use League\OAuth2\Server\AuthorizationServer; | ||
use \Laravel\Passport\ClientRepository; | ||
use Laravel\Passport\PersonalAccessTokenResult; | ||
use Laravel\Passport\TokenRepository; | ||
|
||
class PersonalAccessTokenFactory | ||
{ | ||
/** | ||
* The authorization server instance. | ||
* | ||
* @var \League\OAuth2\Server\AuthorizationServer | ||
*/ | ||
protected $server; | ||
|
||
/** | ||
* The client repository instance. | ||
* | ||
* @var \Laravel\Passport\ClientRepository | ||
*/ | ||
protected $clients; | ||
|
||
/** | ||
* The token repository instance. | ||
* | ||
* @var \Laravel\Passport\TokenRepository | ||
*/ | ||
protected $tokens; | ||
|
||
/** | ||
* The JWT token parser instance. | ||
* | ||
* @var \Lcobucci\JWT\Parser | ||
*/ | ||
protected $jwt; | ||
|
||
/** | ||
* Create a new personal access token factory instance. | ||
* | ||
* @param \League\OAuth2\Server\AuthorizationServer $server | ||
* @param \Laravel\Passport\ClientRepository $clients | ||
* @param \Laravel\Passport\TokenRepository $tokens | ||
* @param \Lcobucci\JWT\Parser $jwt | ||
* @return void | ||
*/ | ||
public function __construct(AuthorizationServer $server, | ||
ClientRepository $clients, | ||
TokenRepository $tokens, | ||
JwtParser $jwt) | ||
{ | ||
$this->jwt = $jwt; | ||
$this->tokens = $tokens; | ||
$this->server = $server; | ||
$this->clients = $clients; | ||
} | ||
|
||
/** | ||
* Create a new personal access token. | ||
* | ||
* @param mixed $userId | ||
* @param string $name | ||
* @param array $scopes | ||
* @return \Laravel\Passport\PersonalAccessTokenResult | ||
*/ | ||
public function make($userId, $name, array $scopes = []) | ||
{ | ||
$response = $this->dispatchRequestToAuthorizationServer( | ||
$this->createRequest($this->clients->personalAccessClient(), $userId, $scopes) | ||
); | ||
|
||
$token = tap($this->findAccessToken($response), function ($token) use ($userId, $name) { | ||
$token->forceFill([ | ||
'user_id' => $userId, | ||
'name' => $name, | ||
])->save(); | ||
}); | ||
|
||
return new PersonalAccessTokenResult( | ||
$response['access_token'], $token | ||
); | ||
} | ||
|
||
/** | ||
* Create a request instance for the given client. | ||
* | ||
* @param \Laravel\Passport\Client $client | ||
* @param mixed $userId | ||
* @param array $scopes | ||
* @return \Zend\Diactoros\ServerRequest | ||
*/ | ||
protected function createRequest($client, $userId, array $scopes) | ||
{ | ||
return (new ServerRequest)->withParsedBody([ | ||
'grant_type' => 'personal_access', | ||
'client_id' => $client->id, | ||
'client_secret' => $client->secret, | ||
'user_id' => $userId, | ||
'scope' => implode(' ', $scopes), | ||
]); | ||
} | ||
|
||
/** | ||
* Dispatch the given request to the authorization server. | ||
* | ||
* @param \Zend\Diactoros\ServerRequest $request | ||
* @return array | ||
*/ | ||
protected function dispatchRequestToAuthorizationServer(ServerRequest $request) | ||
{ | ||
return json_decode($this->server->respondToAccessTokenRequest( | ||
$request, new Response | ||
)->getBody()->__toString(), true); | ||
} | ||
|
||
/** | ||
* Get the access token instance for the parsed response. | ||
* | ||
* @param array $response | ||
* @return Token | ||
*/ | ||
protected function findAccessToken(array $response) | ||
{ | ||
return $this->tokens->find( | ||
$this->jwt->parse($response['access_token'])->getClaim('jti') | ||
); | ||
} | ||
} |