From f5b6b43bef08a1fcc5dcefbcabf525fe5339bd92 Mon Sep 17 00:00:00 2001
From: Alex Bilbie <hello@alexbilbie.com>
Date: Mon, 4 Mar 2013 13:10:00 +0000
Subject: [PATCH] Added requireScopes() method

---
 src/OAuth2/AuthServer.php              | 18 +++++++++++++++++-
 tests/authorization/AuthServerTest.php | 13 +++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/src/OAuth2/AuthServer.php b/src/OAuth2/AuthServer.php
index 1cdb182b..96551343 100644
--- a/src/OAuth2/AuthServer.php
+++ b/src/OAuth2/AuthServer.php
@@ -57,6 +57,12 @@ class AuthServer
      */
     static protected $grantTypes = array();
 
+    /**
+     * Require the "scope" parameter to be in checkAuthoriseParams()
+     * @var boolean
+     */
+    protected $requireScopes = true;
+
     /**
      * The request object
      * @var Util\RequestInterface
@@ -164,6 +170,16 @@ class AuthServer
         return (array_key_exists($identifier, self::$grantTypes));
     }
 
+    /**
+     * Require the "scope" paremter in checkAuthoriseParams()
+     * @param  boolean $require
+     * @return void
+     */
+    public function requireScopes($require = true)
+    {
+        $this->requireScopes = $require;
+    }
+
     /**
      * Get the scope delimeter
      *
@@ -285,7 +301,7 @@ class AuthServer
             if ($scopes[$i] === '') unset($scopes[$i]); // Remove any junk scopes
         }
 
-        if (count($scopes) === 0) {
+        if ($this->requireScopes === true && count($scopes) === 0) {
             throw new Exception\ClientException(sprintf(self::$exceptionMessages['invalid_request'], 'scope'), 0);
         }
 
diff --git a/tests/authorization/AuthServerTest.php b/tests/authorization/AuthServerTest.php
index 95ca9a99..6fa2f9a3 100644
--- a/tests/authorization/AuthServerTest.php
+++ b/tests/authorization/AuthServerTest.php
@@ -89,6 +89,19 @@ class Authorization_Server_test extends PHPUnit_Framework_TestCase
         $this->assertEquals(';', $a->getScopeDelimeter());
     }
 
+    public function test_requireScopes()
+    {
+        $a = $this->returnDefault();
+        $a->requireScopes(false);
+
+        $reflector = new ReflectionClass($a);
+        $requestProperty = $reflector->getProperty('requireScopes');
+        $requestProperty->setAccessible(true);
+        $v = $requestProperty->getValue($a);
+
+        $this->assertFalse($v);
+    }
+
     public function test_getExpiresIn()
     {
         $a = $this->returnDefault();