Jump to content

Fatal error: Declaration


hackalive

Recommended Posts

Hi Guys,

 

Managed to resolve most my oAuth issues.

 

I am suing this library https://github.com/vbardales/oauth2-php - except im removing its reliance on Composer.

 

However - I am having an issue.

 

I get this error:

Fatal error: Declaration of OAuth2\OAuth2StoragePDO::checkClientCredentials() must be compatible with that of OAuth2\IOAuth2Storage::checkClientCredentials() in A:\public\lib\OAuth2StoragePdo.php on line 19

when you visit protected_resource.php (PDO).

 

Any ideas why this error occurs? Whats its reasoning?

 

Code for OAuth2StoragePDO

<?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($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($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);
  }
}

 

AND

 

IOAuth2Storage.php

<?php

namespace OAuth2;

require_once 'Model/IOAuth2Client.php';

/**
* All storage engines need to implement this interface in order to use OAuth2 server
* 
* @author David Rochwerger <catch.dave@gmail.com>
*/
interface IOAuth2Storage {

    /**
     * @return IOAuth2Client
     */
    public function getClient($client_id);

/**
 * Make sure that the client credentials is valid.
 * 
 * @param $client_id
 * Client identifier to be check with.
 * @param $client_secret
 * (optional) If a secret is required, check that they've given the right one.
 *
 * @return
 * TRUE if the client credentials are valid, and MUST return FALSE if it isn't.
 * @endcode
 *
 * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-3.1
 *
 * @ingroup oauth2_section_3
 */
public function checkClientCredentials(IOAuth2Client $client, $client_secret = NULL);

/**
 * Look up the supplied oauth_token from storage.
 *
 * We need to retrieve access token data as we create and verify tokens.
 *
 * @param $oauth_token
 * oauth_token to be check with.
 *
 * @return IOAuth2AccessToken
 *
 * @ingroup oauth2_section_7
 */
public function getAccessToken($oauth_token);

/**
 * Store the supplied access token values to storage.
 *
 * We need to store access token data as we create and verify tokens.
 *
 * @param $oauth_token
 * oauth_token to be stored.
 * @param $client_id
 * Client identifier to be stored.
 * @param $user_id
 * User identifier to be stored.
 * @param $expires
 * Expiration to be stored.
 * @param $scope
 * (optional) Scopes to be stored in space-separated string.
 *
 * @ingroup oauth2_section_4
 */
public function createAccessToken($oauth_token, IOAuth2Client $client, $data, $expires, $scope = NULL);

/**
 * Check restricted grant types of corresponding client identifier.
 *
 * If you want to restrict clients to certain grant types, override this
 * function.
 *
 * @param IOAuth2Client $client
 * Client to be check with.
 * @param $grant_type
 * Grant type to be check with, would be one of the values contained in
 * OAuth2::GRANT_TYPE_REGEXP.
 *
 * @return
 * TRUE if the grant type is supported by this client identifier, and
 * FALSE if it isn't.
 *
 * @ingroup oauth2_section_4
 */
public function checkRestrictedGrantType(IOAuth2Client $client, $grant_type);
}

 

Many many thanks in advance.

 

UPDATE: Someone suggested remove namespaces - so I did - error is now

Fatal error: Declaration of OAuth2StoragePDO::checkClientCredentials() must be compatible with that of IOAuth2Storage::checkClientCredentials() in A:\public\lib\OAuth2StoragePdo.php on line 19

Link to comment
Share on other sites

When you create an interface, any class that implements it must do so exactly.  The method in question is defined to take a User object as its first argument in the interface, but in the class that implements it you pass in an integer.  See the problem?

 

Either get rid of the type hint in the interface, or pass in a User object in the class method.

Link to comment
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.