ClientCredentials.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace OAuth2\GrantType;
  3. use OAuth2\ClientAssertionType\HttpBasic;
  4. use OAuth2\ResponseType\AccessTokenInterface;
  5. use OAuth2\Storage\ClientCredentialsInterface;
  6. /**
  7. * @author Brent Shaffer <bshafs at gmail dot com>
  8. *
  9. * @see OAuth2\ClientAssertionType_HttpBasic
  10. */
  11. class ClientCredentials extends HttpBasic implements GrantTypeInterface
  12. {
  13. private $clientData;
  14. public function __construct(ClientCredentialsInterface $storage, array $config = array())
  15. {
  16. /**
  17. * The client credentials grant type MUST only be used by confidential clients
  18. *
  19. * @see http://tools.ietf.org/html/rfc6749#section-4.4
  20. */
  21. $config['allow_public_clients'] = false;
  22. parent::__construct($storage, $config);
  23. }
  24. public function getQuerystringIdentifier()
  25. {
  26. return 'client_credentials';
  27. }
  28. public function getScope()
  29. {
  30. $this->loadClientData();
  31. return isset($this->clientData['scope']) ? $this->clientData['scope'] : null;
  32. }
  33. public function getUserId()
  34. {
  35. $this->loadClientData();
  36. return isset($this->clientData['user_id']) ? $this->clientData['user_id'] : null;
  37. }
  38. public function createAccessToken(AccessTokenInterface $accessToken, $client_id, $user_id, $scope)
  39. {
  40. /**
  41. * Client Credentials Grant does NOT include a refresh token
  42. *
  43. * @see http://tools.ietf.org/html/rfc6749#section-4.4.3
  44. */
  45. $includeRefreshToken = false;
  46. return $accessToken->createAccessToken($client_id, $user_id, $scope, $includeRefreshToken);
  47. }
  48. private function loadClientData()
  49. {
  50. if (!$this->clientData) {
  51. $this->clientData = $this->storage->getClientDetails($this->getClientId());
  52. }
  53. }
  54. }