Scope.php 2.8 KB

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