diff options
author | Joseph Warner <hardolaf@hardolaf.com> | 2013-07-13 17:31:41 -0400 |
---|---|---|
committer | Joseph Warner <hardolaf@hardolaf.com> | 2013-07-14 14:22:42 -0400 |
commit | 02921f4b23fd2fa3efc9eddedfe7bfd6d3347297 (patch) | |
tree | d0e59a7c0bcb458d60b394fd49338b77fa607597 /phpBB | |
parent | 24bf333e161332ddd589831228e35ad9eb1e8f18 (diff) | |
download | forums-02921f4b23fd2fa3efc9eddedfe7bfd6d3347297.tar forums-02921f4b23fd2fa3efc9eddedfe7bfd6d3347297.tar.gz forums-02921f4b23fd2fa3efc9eddedfe7bfd6d3347297.tar.bz2 forums-02921f4b23fd2fa3efc9eddedfe7bfd6d3347297.tar.xz forums-02921f4b23fd2fa3efc9eddedfe7bfd6d3347297.zip |
[feature/oauth] Have token storage use DB
PHPBB3-11673
Diffstat (limited to 'phpBB')
-rw-r--r-- | phpBB/includes/auth/oauth/token_storage.php | 77 |
1 files changed, 67 insertions, 10 deletions
diff --git a/phpBB/includes/auth/oauth/token_storage.php b/phpBB/includes/auth/oauth/token_storage.php index b658333900..4bf52e2ced 100644 --- a/phpBB/includes/auth/oauth/token_storage.php +++ b/phpBB/includes/auth/oauth/token_storage.php @@ -15,8 +15,11 @@ if (!defined('IN_PHPBB')) exit; } -use OAuth\Common\Storage\TokenStorageInterface; + use OAuth\Common\Token\TokenInterface; +use OAuth\Common\Storage\TokenStorageInterface; +use OAuth\Common\Storage\Exception\StorageException; +use OAuth\Common\Storage\Exception\TokenNotFoundException; /** * OAuth storage wrapper for phpbb's cache @@ -33,6 +36,13 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface protected $db; /** + * phpBB user + * + * @var phpbb_user + */ + protected $user; + + /** * Name of the OAuth provider * * @var string @@ -48,10 +58,13 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface * Creates token storage for phpBB. * * @param phpbb_db_driver $db + * @param phpbb_user $user + * @param string $service_name */ - public function __construct(phpbb_db_driver $db, $service_name) + public function __construct(phpbb_db_driver $db, phpbb_user $user, $service_name) { $this->db = $db; + $this->user = $user; $this->service_name = $service_name; } @@ -64,9 +77,31 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface return $this->token; } - // TODO: check to see if the token is cached + $sql = 'SELECT oauth_token FROM ' . AUTH_PROVIDER_OAUTH . + $db->sql_build_array('SELECT', array( + 'user_id' => $this->user->data['user_id'], + 'oauth_provider' => $this->service_name, + )); + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + if (!$row) + { + throw new TokenNotFoundException('Token not stored'); + } + + $token = unserialize($row['oauth_token']); - throw new TokenNotFoundException('Token not stored'); + // Ensure that the token was serialized/unserialized correctly + if (!($token instanceof TokenInterface)) + { + $this->clearToken(); + throw new TokenNotFoundException('Token not stored correctly'); + } + + $this->cachedToken = $token; + return $token; } /** @@ -75,7 +110,13 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface public function storeAccessToken(TokenInterface $token) { $this->cachedToken = $token; - // TODO: actually store the token + + $sql = 'INSERT INTO ' . AUTH_PROVIDER_OAUTH . ' ' . $this->db->sql_build_array('INSERT', array( + 'user_id' => $this->user->data['user_id'], + 'oauth_provider' => $this->service_name, + 'oauth_token' => serialize($token), + )); + $this->db->sql_query($sql); } /** @@ -84,11 +125,24 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface public function hasAccessToken() { if( $this->cachedToken ) { - return true; - } + return true; + } - // TODO: check cache for token - return false; + $sql = 'SELECT oauth_token FROM ' . AUTH_PROVIDER_OAUTH . + $db->sql_build_array('SELECT', array( + 'user_id' => $this->user->data['user_id'], + 'oauth_provider' => $this->service_name, + )); + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + if (!$row) + { + return false; + } + + return true; } /** @@ -97,6 +151,9 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface public function clearToken() { $this->cachedToken = null; - // TODO: clear cache of the token + + $sql = 'DELETE FROM ' . AUTH_PROVIDER_OAUTH . 'WHERE user_id = ' . $this->user->data['user_id'] . + ' AND oauth_provider = ' . $this->db->sql_escape($this->oauth_provider); + $this->db->sql_query($sql); } } |