diff options
| -rw-r--r-- | src/OAuth2/Server.php | 2 | ||||
| -rw-r--r-- | src/OAuth2/Storage/AccessTokenInterface.php | 2 | ||||
| -rw-r--r-- | src/OAuth2/Storage/AuthorizationCodeInterface.php | 2 | ||||
| -rw-r--r-- | src/OAuth2/Storage/Memory.php | 4 | ||||
| -rw-r--r-- | src/OAuth2/Storage/Pdo.php | 28 | ||||
| -rw-r--r-- | src/OAuth2/Storage/RefreshTokenInterface.php | 2 | ||||
| -rw-r--r-- | test/OAuth2/Storage/PdoTest.php | 50 |
7 files changed, 81 insertions, 9 deletions
diff --git a/src/OAuth2/Server.php b/src/OAuth2/Server.php index a5380ce..b1cc4f9 100644 --- a/src/OAuth2/Server.php +++ b/src/OAuth2/Server.php @@ -396,7 +396,7 @@ class OAuth2_Server implements OAuth2_Response_ProviderInterface } if ($response_type == self::RESPONSE_TYPE_AUTHORIZATION_CODE) { if (!isset($this->grantTypes['code'])) { - $this->response = new OAuth2_Response_Redirect($redirect_uri, 302, 'unsupported_response_type', 'authorization code response type not supported', $state); + $this->response = new OAuth2_Response_Redirect($redirect_uri, 302, 'unsupported_response_type', 'authorization code grant type not supported', $state); return false; } if ($this->grantTypes['code']->enforceRedirect() && !$redirect_uri) { diff --git a/src/OAuth2/Storage/AccessTokenInterface.php b/src/OAuth2/Storage/AccessTokenInterface.php index 1fdd59d..df3120e 100644 --- a/src/OAuth2/Storage/AccessTokenInterface.php +++ b/src/OAuth2/Storage/AccessTokenInterface.php @@ -44,5 +44,5 @@ interface OAuth2_Storage_AccessTokenInterface * * @ingroup oauth2_section_4 */ - public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = NULL); + public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null); }
\ No newline at end of file diff --git a/src/OAuth2/Storage/AuthorizationCodeInterface.php b/src/OAuth2/Storage/AuthorizationCodeInterface.php index ab22fe9..4a90946 100644 --- a/src/OAuth2/Storage/AuthorizationCodeInterface.php +++ b/src/OAuth2/Storage/AuthorizationCodeInterface.php @@ -67,6 +67,6 @@ interface OAuth2_Storage_AuthorizationCodeInterface * * @ingroup oauth2_section_4 */ - public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = NULL); + public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null); }
\ No newline at end of file diff --git a/src/OAuth2/Storage/Memory.php b/src/OAuth2/Storage/Memory.php index 93034d2..c986076 100644 --- a/src/OAuth2/Storage/Memory.php +++ b/src/OAuth2/Storage/Memory.php @@ -40,7 +40,7 @@ class OAuth2_Storage_Memory implements OAuth2_Storage_AuthorizationCodeInterface return null; } - public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = NULL) + public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null) { $this->authorizationCodes[$code] = compact('code', 'client_id', 'user_id', 'redirect_uri', 'expires', 'scope'); } @@ -118,7 +118,7 @@ class OAuth2_Storage_Memory implements OAuth2_Storage_AuthorizationCodeInterface return isset($this->accessTokens[$access_token]) ? $this->accessTokens[$access_token] : null; } - public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = NULL) + public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = null) { $this->accessTokens[$access_token] = compact('access_token', 'client_id', 'user_id', 'expires', 'scope'); } diff --git a/src/OAuth2/Storage/Pdo.php b/src/OAuth2/Storage/Pdo.php index 8690b45..99afd35 100644 --- a/src/OAuth2/Storage/Pdo.php +++ b/src/OAuth2/Storage/Pdo.php @@ -3,7 +3,8 @@ /** * */ -class OAuth2_Storage_Pdo implements OAuth2_Storage_AccessTokenInterface, OAuth2_Storage_ClientCredentialsInterface +class OAuth2_Storage_Pdo implements OAuth2_Storage_AuthorizationCodeInterface, + OAuth2_Storage_AccessTokenInterface, OAuth2_Storage_ClientCredentialsInterface { private $db; private $config; @@ -32,6 +33,7 @@ class OAuth2_Storage_Pdo implements OAuth2_Storage_AccessTokenInterface, OAuth2_ $this->config = array_merge(array( 'client_table_name' => 'oauth_clients', 'token_table_name' => 'oauth_access_tokens', + 'code_table_name' => 'oauth_authorization_codes', ), $config); } @@ -74,14 +76,34 @@ class OAuth2_Storage_Pdo implements OAuth2_Storage_AccessTokenInterface, OAuth2_ return $stmt->fetch(); } - public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = NULL) + public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = null) { // if it exists, update it. if ($this->getAccessToken($access_token)) { $stmt = $this->db->prepare(sprintf('UPDATE %s SET client_id=:client_id, expires=:expires, user_id=:user_id, scope=:scope where access_token=:access_token', $this->config['token_table_name'])); } else { - $stmt = $this->db->prepare(sprintf('INSERT INTO %s (client_id, expires, user_id, scope, access_token) VALUES (:client_id, :expires, :user_id, :scope, :access_token)', $this->config['token_table_name'])); + $stmt = $this->db->prepare(sprintf('INSERT INTO %s (access_token, client_id, expires, user_id, scope) VALUES (:access_token, :client_id, :expires, :user_id, :scope)', $this->config['token_table_name'])); } return $stmt->execute(compact('access_token', 'client_id', 'user_id', 'expires', 'scope')); } + + /* AuthorizationCodeInterface */ + public function getAuthorizationCode($code) + { + $stmt = $this->db->prepare(sprintf('SELECT * from %s where authorization_code = "%s"', $this->config['code_table_name'], $code)); + $stmt->execute(); + + return $stmt->fetch(); + } + + public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null) + { + // if it exists, update it. + if ($this->getAuthorizationCode($code)) { + $stmt = $this->db->prepare($sql = sprintf('UPDATE %s SET client_id=:client_id, user_id=:user_id, redirect_uri=:redirect_uri, expires=:expires, scope=:scope where authorization_code=:code', $this->config['code_table_name'])); + } else { + $stmt = $this->db->prepare(sprintf('INSERT INTO %s (authorization_code, client_id, user_id, redirect_uri, expires, scope) VALUES (:code, :client_id, :user_id, :redirect_uri, :expires, :scope)', $this->config['code_table_name'])); + } + return $stmt->execute(compact('code', 'client_id', 'user_id', 'redirect_uri', 'expires', 'scope')); + } }
\ No newline at end of file diff --git a/src/OAuth2/Storage/RefreshTokenInterface.php b/src/OAuth2/Storage/RefreshTokenInterface.php index 31986b3..d1a32f7 100644 --- a/src/OAuth2/Storage/RefreshTokenInterface.php +++ b/src/OAuth2/Storage/RefreshTokenInterface.php @@ -56,7 +56,7 @@ interface OAuth2_Storage_RefreshTokenInterface * * @ingroup oauth2_section_6 */ - public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = NULL); + public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = null); /** * Expire a used refresh token. diff --git a/test/OAuth2/Storage/PdoTest.php b/test/OAuth2/Storage/PdoTest.php index fa16cfa..0f20168 100644 --- a/test/OAuth2/Storage/PdoTest.php +++ b/test/OAuth2/Storage/PdoTest.php @@ -48,6 +48,10 @@ class OAuth2_Storage_PdoTest extends PHPUnit_Framework_TestCase /** @dataProvider provideStorage */ public function testSetAccessToken($storage) { + // assert token we are about to add does not exist + $token = $storage->getAccessToken('newtoken'); + $this->assertFalse($token); + // add new token $success = $storage->setAccessToken('newtoken', 'client ID', 'SOMEUSERID', time() + 20); $this->assertTrue($success); @@ -71,6 +75,50 @@ class OAuth2_Storage_PdoTest extends PHPUnit_Framework_TestCase $this->assertEquals($token['user_id'], 'SOMEOTHERID'); } + /** @dataProvider provideStorage */ + public function testGetAuthorizationCode($storage) + { + // nonexistant client_id + $details = $storage->getAuthorizationCode('faketoken'); + $this->assertFalse($details); + + // valid client_id + $details = $storage->getAuthorizationCode('testtoken'); + $this->assertNotNull($details); + } + + /** @dataProvider provideStorage */ + public function testSetAuthorizationCode($storage) + { + // assert code we are about to add does not exist + $code = $storage->getAuthorizationCode('newcode'); + $this->assertFalse($code); + + // add new code + $success = $storage->setAuthorizationCode('newcode', 'client ID', 'SOMEUSERID', 'http://adobe.com', time() + 20); + $this->assertTrue($success); + + $code = $storage->getAuthorizationCode('newcode'); + $this->assertNotNull($code); + $this->arrayHasKey('access_token', $code); + $this->arrayHasKey('client_id', $code); + $this->arrayHasKey('user_id', $code); + $this->assertEquals($code['user_id'], 'SOMEUSERID'); + $this->arrayHasKey('redirect_uri', $code); + + // change existing code + $success = $storage->setAuthorizationCode('newcode', 'client ID', 'SOMEOTHERID', 'http://adobe.com', time() + 20); + $this->assertTrue($success); + + $code = $storage->getAuthorizationCode('newcode'); + $this->assertNotNull($code); + $this->arrayHasKey('access_token', $code); + $this->arrayHasKey('client_id', $code); + $this->arrayHasKey('user_id', $code); + $this->assertEquals($code['user_id'], 'SOMEOTHERID'); + $this->arrayHasKey('redirect_uri', $code); + } + public function provideStorage() { $this->removeSqliteDb(); // remove db to be safe @@ -92,10 +140,12 @@ class OAuth2_Storage_PdoTest extends PHPUnit_Framework_TestCase $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec('CREATE TABLE oauth_clients (client_id TEXT, client_secret TEXT, redirect_uri TEXT)'); $db->exec('CREATE TABLE oauth_access_tokens (access_token TEXT, client_id TEXT, user_id TEXT, expires TIMESTAMP, scope TEXT)'); + $db->exec('CREATE TABLE oauth_authorization_codes (authorization_code TEXT, client_id TEXT, user_id TEXT, redirect_uri TEXT, expires TIMESTAMP, scope TEXT)'); // test data $db->exec('INSERT INTO oauth_clients (client_id, client_secret) VALUES ("oauth_test_client", "testpass")'); $db->exec('INSERT INTO oauth_access_tokens (access_token, client_id) VALUES ("testtoken", "Some Client")'); + $db->exec('INSERT INTO oauth_authorization_codes (authorization_code, client_id) VALUES ("testcode", "Some Client")'); } private function removeSqliteDb() |
