[feature/oauth] Have token storage use DB

PHPBB3-11673
This commit is contained in:
Joseph Warner 2013-07-13 17:31:41 -04:00
parent 24bf333e16
commit 02921f4b23

View file

@ -15,8 +15,11 @@ if (!defined('IN_PHPBB'))
exit; exit;
} }
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Token\TokenInterface; 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 * OAuth storage wrapper for phpbb's cache
@ -32,6 +35,13 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface
*/ */
protected $db; protected $db;
/**
* phpBB user
*
* @var phpbb_user
*/
protected $user;
/** /**
* Name of the OAuth provider * Name of the OAuth provider
* *
@ -48,10 +58,13 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface
* Creates token storage for phpBB. * Creates token storage for phpBB.
* *
* @param phpbb_db_driver $db * @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->db = $db;
$this->user = $user;
$this->service_name = $service_name; $this->service_name = $service_name;
} }
@ -64,18 +77,46 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface
return $this->token; 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'); throw new TokenNotFoundException('Token not stored');
} }
$token = unserialize($row['oauth_token']);
// 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;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function storeAccessToken(TokenInterface $token) public function storeAccessToken(TokenInterface $token)
{ {
$this->cachedToken = $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);
} }
/** /**
@ -87,16 +128,32 @@ class phpbb_auth_oauth_token_storage implements TokenStorageInterface
return true; return true;
} }
// TODO: check cache for token $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 false;
} }
return true;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function clearToken() public function clearToken()
{ {
$this->cachedToken = null; $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);
} }
} }