ClientRepository.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Module\OAuth\Repositories;
  3. use App\Module\OAuth\Models\OAuthClient;
  4. use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
  5. use League\OAuth2\Server\Entities\ClientEntityInterface;
  6. use League\OAuth2\Server\Entities\Traits\ClientTrait;
  7. use League\OAuth2\Server\Entities\Traits\EntityTrait;
  8. class ClientRepository implements ClientRepositoryInterface
  9. {
  10. public function getClientEntity($clientIdentifier)
  11. {
  12. $client = OAuthClient::where('client_id', $clientIdentifier)->first();
  13. if (!$client) {
  14. return null;
  15. }
  16. return new class($client) implements ClientEntityInterface {
  17. use EntityTrait, ClientTrait;
  18. private $client;
  19. public function __construct($client)
  20. {
  21. $this->client = $client;
  22. $this->setIdentifier($client->client_id);
  23. $this->name = $client->name;
  24. $this->redirectUri = $client->redirect_uri;
  25. }
  26. public function isConfidential()
  27. {
  28. return true;
  29. }
  30. };
  31. }
  32. public function validateClient($clientIdentifier, $clientSecret, $grantType)
  33. {
  34. $client = OAuthClient::where('client_id', $clientIdentifier)
  35. ->where('client_secret', $clientSecret)
  36. ->first();
  37. if (!$client) {
  38. return false;
  39. }
  40. return in_array($grantType, $client->grant_types);
  41. }
  42. }