hackalive Posted June 17, 2012 Share Posted June 17, 2012 Hi guys, What does this error mean: Fatal error: Class OAuth2StoragePDO contains 4 abstract methods and must therefore be declared abstract or implement the remaining methods (IOAuth2GrantCode::createAuthCode, IOAuth2Storage::getClient, IOAuth2Storage::createAccessToken, ...) in A:\public\lib\OAuth2StoragePdo.php on line 288 and how do I resolve it? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/ Share on other sites More sharing options...
KevinM1 Posted June 17, 2012 Share Posted June 17, 2012 It means pretty much what it says. OAuthStoragePDO contains 4 abstract methods. The class that inherits from it must either provide concrete functionality for every one of them, or the child class itself must be declared as abstract. A non-abstract class cannot have abstract methods within it, even if those methods come from inheritance. Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354613 Share on other sites More sharing options...
hackalive Posted June 17, 2012 Author Share Posted June 17, 2012 So currently OAuth2StoragePDO is like this: class OAuth2StoragePDO implements IOAuth2GrantCode, IOAuth2RefreshTokens { Are you saying make it abstract class OAuth2StoragePDO implements IOAuth2GrantCode, IOAuth2RefreshTokens { ? Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354614 Share on other sites More sharing options...
KevinM1 Posted June 17, 2012 Share Posted June 17, 2012 Is that what you want? Not trying to be flippant here. Declaring a class as abstract would change the way it's used. You shouldn't be looking for a quick fix just to get rid of an error. Before you go any further you should read up on abstract classes and interfaces. They're basic OO constructs that you need to understand before attempting to use them. Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354618 Share on other sites More sharing options...
hackalive Posted June 17, 2012 Author Share Posted June 17, 2012 Doing the above returns Fatal error: Cannot instantiate abstract class OAuth2StoragePDO in A:\public\index.php on line 19 so that dosent work anyway. How can I fix this? This is really annoying me now ... grrrrr :/ Code of oAuth2StoragePdo.php <?php /** * @file * Sample OAuth2 Library PDO DB Implementation. * * Simply pass in a configured PDO class, eg: * new PDOOAuth2( new PDO('mysql:dbname=mydb;host=localhost', 'user', 'pass') ); */ //namespace OAuth2; require 'OAuth2/IOAuth2GrantCode.php'; require 'OAuth2/IOAuth2RefreshTokens.php'; /** * PDO storage engine for the OAuth2 Library. */ class OAuth2StoragePDO implements IOAuth2GrantCode, IOAuth2RefreshTokens { /**@#+ * Centralized table names * * @var string */ const TABLE_CLIENTS = 'clients'; const TABLE_CODES = 'auth_codes'; const TABLE_TOKENS = 'access_tokens'; const TABLE_REFRESH = 'refresh_tokens'; /**@#-*/ /** * @var PDO */ private $db; /** * @var string */ private $salt; /** * Implements OAuth2::__construct(). */ public function __construct(PDO $db, $salt = 'CHANGE_ME!') { $this->db = $db; } /** * Handle PDO exceptional cases. */ private function handleException($e) { throw $e; } /** * Little helper function to add a new client to the database. * * Do NOT use this in production! This sample code stores the secret * in plaintext! * * @param $client_id * Client identifier to be stored. * @param $client_secret * Client secret to be stored. * @param $redirect_uri * Redirect URI to be stored. */ public function addClient($client_id, $client_secret, $redirect_uri) { try { $client_secret = $this->hash($client_secret, $client_id); $sql = 'INSERT INTO '.self::TABLE_CLIENTS.' (client_id, client_secret, redirect_uri) VALUES (:client_id, :client_secret, :redirect_uri)'; $stmt = $this->db->prepare($sql); $stmt->bindParam(':client_id', $client_id, PDO::PARAM_STR); $stmt->bindParam(':client_secret', $client_secret, PDO::PARAM_STR); $stmt->bindParam(':redirect_uri', $redirect_uri, PDO::PARAM_STR); $stmt->execute(); } catch (PDOException $e) { $this->handleException($e); } } /** * Implements IOAuth2Storage::checkClientCredentials(). * */ public function checkClientCredentials(IOAuth2Client $client_id, $client_secret = NULL) { try { $sql = 'SELECT client_secret FROM '.self::TABLE_CLIENTS.' WHERE client_id = :client_id'; $stmt = $this->db->prepare($sql); $stmt->bindParam(':client_id', $client_id, PDO::PARAM_STR); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); if ($client_secret === NULL) return $result !== FALSE; return $this->checkPassword($client_secret, $result['client_secret'], $client_id); } catch (PDOException $e) { $this->handleException($e); } } /** * Implements IOAuth2Storage::getRedirectUri(). */ public function getClientDetails($client_id) { try { $sql = 'SELECT redirect_uri FROM '.self::TABLE_CLIENTS.' WHERE client_id = :client_id'; $stmt = $this->db->prepare($sql); $stmt->bindParam(':client_id', $client_id, PDO::PARAM_STR); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); if ($result === FALSE) return FALSE; return isset($result['redirect_uri']) && $result['redirect_uri'] ? $result : NULL; } catch (PDOException $e) { $this->handleException($e); } } /** * Implements IOAuth2Storage::getAccessToken(). */ public function getAccessToken($oauth_token) { return $this->getToken($oauth_token, FALSE); } /** * Implements IOAuth2Storage::setAccessToken(). */ public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = NULL) { $this->setToken($oauth_token, $client_id, $user_id, $expires, $scope, FALSE); } /** * @see IOAuth2Storage::getRefreshToken() */ public function getRefreshToken($refresh_token) { return $this->getToken($refresh_token, TRUE); } /** * @see IOAuth2Storage::setRefreshToken() */ public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = NULL) { return $this->setToken($refresh_token, $client_id, $user_id, $expires, $scope, TRUE); } /** * @see IOAuth2Storage::unsetRefreshToken() */ public function unsetRefreshToken($refresh_token) { try { $sql = 'DELETE FROM '.self::TABLE_TOKENS.' WHERE refresh_token = :refresh_token'; $stmt = $this->db->prepare($sql); $stmt->bindParam(':refresh_token', $refresh_token, PDO::PARAM_STR); $stmt->execute(); } catch (PDOException $e) { $this->handleException($e); } } /** * Implements IOAuth2Storage::getAuthCode(). */ public function getAuthCode($code) { try { $sql = 'SELECT code, client_id, user_id, redirect_uri, expires, scope FROM '.self::TABLE_CODES.' auth_codes WHERE code = :code'; $stmt = $this->db->prepare($sql); $stmt->bindParam(':code', $code, PDO::PARAM_STR); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); return $result !== FALSE ? $result : NULL; } catch (PDOException $e) { $this->handleException($e); } } /** * Implements IOAuth2Storage::setAuthCode(). */ public function setAuthCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = NULL) { try { $sql = 'INSERT INTO '.self::TABLE_CODES.' (code, client_id, user_id, redirect_uri, expires, scope) VALUES (:code, :client_id, :user_id, :redirect_uri, :expires, :scope)'; $stmt = $this->db->prepare($sql); $stmt->bindParam(':code', $code, PDO::PARAM_STR); $stmt->bindParam(':client_id', $client_id, PDO::PARAM_STR); $stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR); $stmt->bindParam(':redirect_uri', $redirect_uri, PDO::PARAM_STR); $stmt->bindParam(':expires', $expires, PDO::PARAM_INT); $stmt->bindParam(':scope', $scope, PDO::PARAM_STR); $stmt->execute(); } catch (PDOException $e) { $this->handleException($e); } } /** * @see IOAuth2Storage::checkRestrictedGrantType() */ public function checkRestrictedGrantType(IOAuth2Client $client_id, $grant_type) { return TRUE; // Not implemented } /** * Creates a refresh or access token * * @param string $token - Access or refresh token id * @param string $client_id * @param mixed $user_id * @param int $expires * @param string $scope * @param bool $isRefresh */ protected function setToken($token, $client_id, $user_id, $expires, $scope, $isRefresh = TRUE) { try { $tableName = $isRefresh ? self::TABLE_REFRESH : self::TABLE_TOKENS; $sql = "INSERT INTO $tableName (oauth_token, client_id, user_id, expires, scope) VALUES (:token, :client_id, :user_id, :expires, :scope)"; $stmt = $this->db->prepare($sql); $stmt->bindParam(':token', $token, PDO::PARAM_STR); $stmt->bindParam(':client_id', $client_id, PDO::PARAM_STR); $stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR); $stmt->bindParam(':expires', $expires, PDO::PARAM_INT); $stmt->bindParam(':scope', $scope, PDO::PARAM_STR); $stmt->execute(); } catch (PDOException $e) { $this->handleException($e); } } /** * Retrieves an access or refresh token. * * @param string $token * @param bool $refresh */ protected function getToken($token, $isRefresh = true) { try { $tableName = $isRefresh ? self::TABLE_REFRESH : self::TABLE_TOKENS; $tokenName = $isRefresh ? 'refresh_token' : 'oauth_token'; $sql = "SELECT oauth_token AS $tokenName, client_id, expires, scope, user_id FROM $tableName WHERE oauth_token = :token"; $stmt = $this->db->prepare($sql); $stmt->bindParam(':token', $token, PDO::PARAM_STR); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); return $result !== FALSE ? $result : NULL; } catch (PDOException $e) { $this->handleException($e); } } /** * Change/override this to whatever your own password hashing method is. * * @param string $secret * @return string */ protected function hash($client_secret, $client_id) { return hash('sha1', $client_id.$client_secret.$this->salt); } /** * Checks the password. * Override this if you need to * * @param string $client_id * @param string $client_secret * @param string $actualPassword */ protected function checkPassword($try, $client_secret, $client_id) { return $client_secret == $this->hash($try, $client_id); } } Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354619 Share on other sites More sharing options...
KevinM1 Posted June 17, 2012 Share Posted June 17, 2012 Please, follow the advice I gave in the 2nd paragraph. You're flailing blindly at your code due to frustration. You can alleviate that frustration by learning how things actually work. Making incremental changes without knowing what you're doing in the hope of stumbling on the magic solution is asinine. PHP.net explains abstract classes and methods, as well as interfaces. Go there, take your time to go through the examples, and try again. Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354621 Share on other sites More sharing options...
hackalive Posted June 17, 2012 Author Share Posted June 17, 2012 I was kinda hoping for some more direct advice - reading http://php.net/manual/en/language.oop5.abstract.php so far is not really giving me any ideas on what to do. Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354624 Share on other sites More sharing options...
hackalive Posted June 17, 2012 Author Share Posted June 17, 2012 I do understand WHY its doing it: Because class OAuth2StoragePDO is missing some functions from the interface and extends, but HOW do I resolve it? Im very confused as to where to go from here and what to do. Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354633 Share on other sites More sharing options...
ignace Posted June 17, 2012 Share Posted June 17, 2012 Because class OAuth2StoragePDO is missing some functions from the interface and extends, but HOW do I resolve it? Implement them? Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354653 Share on other sites More sharing options...
hackalive Posted June 17, 2012 Author Share Posted June 17, 2012 how? Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354735 Share on other sites More sharing options...
trq Posted June 18, 2012 Share Posted June 18, 2012 You need to implement the functions that are missing. I'm not sure it could be put any simpler. Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354759 Share on other sites More sharing options...
ignace Posted June 18, 2012 Share Posted June 18, 2012 Create the missing functions createAuthCode, getClient, createAccessToken, ... in OAuth2StoragePdo or download an works-out-of-the-box OAuth handler instead of the one you currently have. If you only need the consumer try Zend. Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354776 Share on other sites More sharing options...
hackalive Posted June 18, 2012 Author Share Posted June 18, 2012 Hi ignace, I am being the oAuth provider. Can you suggest works out of the box oAuth? That is PHP and dosent need Composer etc? Yet to find one. Cheer Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354795 Share on other sites More sharing options...
ignace Posted June 18, 2012 Share Posted June 18, 2012 Yet to find one. oauth Quote Link to comment https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354863 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.