Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/264334-abstract-methods/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354613
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354618
Share on other sites

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);
  }
}

Link to comment
https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354619
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264334-abstract-methods/#findComment-1354621
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.