1
0

Scope.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace OAuth2;
  3. use InvalidArgumentException;
  4. use OAuth2\Storage\Memory;
  5. use OAuth2\Storage\ScopeInterface as ScopeStorageInterface;
  6. /**
  7. * @see ScopeInterface
  8. */
  9. class Scope implements ScopeInterface
  10. {
  11. protected $storage;
  12. /**
  13. * Constructor
  14. *
  15. * @param mixed $storage - Either an array of supported scopes, or an instance of OAuth2\Storage\ScopeInterface
  16. *
  17. * @throws InvalidArgumentException
  18. */
  19. public function __construct($storage = null)
  20. {
  21. if (is_null($storage) || is_array($storage)) {
  22. $storage = new Memory((array) $storage);
  23. }
  24. if (!$storage instanceof ScopeStorageInterface) {
  25. throw new InvalidArgumentException("Argument 1 to OAuth2\Scope must be null, an array, or instance of OAuth2\Storage\ScopeInterface");
  26. }
  27. $this->storage = $storage;
  28. }
  29. /**
  30. * Check if everything in required scope is contained in available scope.
  31. *
  32. * @param string $required_scope - A space-separated string of scopes.
  33. * @param string $available_scope - A space-separated string of scopes.
  34. * @return bool - TRUE if everything in required scope is contained in available scope and FALSE
  35. * if it isn't.
  36. *
  37. * @see http://tools.ietf.org/html/rfc6749#section-7
  38. *
  39. * @ingroup oauth2_section_7
  40. */
  41. public function checkScope($required_scope, $available_scope)
  42. {
  43. $required_scope = explode(' ', trim($required_scope));
  44. $available_scope = explode(' ', trim($available_scope));
  45. return (count(array_diff($required_scope, $available_scope)) == 0);
  46. }
  47. /**
  48. * Check if the provided scope exists in storage.
  49. *
  50. * @param string $scope - A space-separated string of scopes.
  51. * @return bool - TRUE if it exists, FALSE otherwise.
  52. */
  53. public function scopeExists($scope)
  54. {
  55. // Check reserved scopes first.
  56. $scope = explode(' ', trim($scope));
  57. $reservedScope = $this->getReservedScopes();
  58. $nonReservedScopes = array_diff($scope, $reservedScope);
  59. if (count($nonReservedScopes) == 0) {
  60. return true;
  61. } else {
  62. // Check the storage for non-reserved scopes.
  63. $nonReservedScopes = implode(' ', $nonReservedScopes);
  64. return $this->storage->scopeExists($nonReservedScopes);
  65. }
  66. }
  67. /**
  68. * @param RequestInterface $request
  69. * @return string
  70. */
  71. public function getScopeFromRequest(RequestInterface $request)
  72. {
  73. // "scope" is valid if passed in either POST or QUERY
  74. return $request->request('scope', $request->query('scope'));
  75. }
  76. /**
  77. * @param null $client_id
  78. * @return mixed
  79. */
  80. public function getDefaultScope($client_id = null)
  81. {
  82. return $this->storage->getDefaultScope($client_id);
  83. }
  84. /**
  85. * Get reserved scopes needed by the server.
  86. *
  87. * In case OpenID Connect is used, these scopes must include:
  88. * 'openid', offline_access'.
  89. *
  90. * @return array - An array of reserved scopes.
  91. */
  92. public function getReservedScopes()
  93. {
  94. return array('openid', 'offline_access');
  95. }
  96. }