PersonalAccessTokenFactory.php 3.5 KB

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