Search code examples
phpbasic-authenticationrestler

Restler 3.0 - Undefined index: BasicAuthentication


When I run my GET /user with basic authentication (see class code below), the username and password are authenticated. But my xml response is:

<br />
<b>Notice</b>:  Undefined index: BasicAuthentication in <b>/...Web/api/vendor/Luracast/Restler/Restler.php</b> on line <b>600</b><br />
<?xml version="1.0" encoding="UTF-8"?>
<response>
    <prod1>yyyy-mm-dd</prod1>
    <prod2>yyyy-mm-dd</prod2>
</response>

In the error_log shows the user is authenticated, and the userInfo array:

[Tue Jul 14 14:56:30.239048 2015] [:error] [pid 35839] [client 192.168.42.59:59671] BasicAuthentication:__isAllowed:authorized:userInfo:Array\n(\n    [Signed Up] => 2013-10-03 15:06:33\n    [Last Updated] => 2013-10-03\n    [Subscribed to Mailing List] => Yes\n    [HRA  Free Membership] => Expire: 0000-00-00\n    [HRA SILVER 3 Months Membership] => Expire: 9999-12-31\n)\n

I am using the same template for class BasicAuthentication implements iAuthenticate as I have used before. But I have not seen this notice before.

It may also be causing an error when grabbing $userInfo from User.php:

$userInfo = BasicAuthentication::$userInfo();

This is the error when I am grabbing the public static $userInfo = array(); in User.php:

Fatal error: Cannot redeclare class BasicAuthentication in ...Web/api/public/v1/BasicAuthentication.php on line 26

The User class file (User.php):

/**
 * All methods in this class are protected
 * @access protected
 * @class BasicAuthentication {@requires member}
 */
class User {

    /**
     * Retrieve User information
     * 
     * Return some user information
     * 
     * @throttle 200
     *
     * @throws 400 Bad Request - parameter values are wrong
     * @throws 401 Unauthorized
     * @return array
     */
    function index() {

        $userInfo = BasicAuthentication::$userInfo();
        //$userInfo = array("prod1" => "yyyy-mm-dd","prod2" => "yyyy-mm-dd");
        return $userInfo;
    }
}

BasicAuthentication.php:

use Luracast\Restler\iAuthenticate;
use Luracast\Restler\RestException;
use Luracast\Restler\Resources;

class BasicAuthentication implements iAuthenticate {

const REALM = 'Restricted API';

public static $requires = 'non-member';
public static $role = 'non-member';
public static $active = FALSE;
public static $email = 0;
public static $userId = 0;
public static $userInfo = 0;
public static $userLevel = 0;
public static $productId = 0;
public static $productName = 0;

function __isAllowed() {
    self::$email = 0;
    self::$userId = 0;
    self::$userLevel = 0;
    self::$userInfo = array();
    self::$active = FALSE;
    self::$role = 'non-member';
    self::$productId = 0;
    self::$productName = 0;

    if (isset($_SERVER ['PHP_AUTH_USER']) && isset($_SERVER ['PHP_AUTH_PW'])) {

// ...snip... validation code

    }
    header('WWW-Authenticate: Basic realm="' . self::REALM . '"');
    error_log("BasicAuthentication:__isAllowed:failed - no user specified");
    throw new RestException(401, 'Basic Authentication Required');
    return FALSE;
}

/**
 *
 * @access private
 */
public static function verifyAccess(array $m) {
    $requires = isset($m ['class'] ['BasicAuthentication'] ['properties'] ['requires']) ? $m ['class'] ['BasicAuthentication'] ['properties'] ['requires'] : false;
    error_log("BasicAuthentication:verifyAccess:requires:" . $requires);
    return $requires ? static::$role == $requires : true;
}

/**
 *
 * @access private
 */
public static function email() {
    return self::$email;
}

/**
 *
 * @access private
 */
public static function userId() {
    return self::$userId;
}

/**
 *
 * @access private
 */
public static function active() {
    return self::$active;
}

/**
 *
 * @access private
 */
public static function userLevel() {
    return self::$userLevel;
}

/**
 *
 * @access private
 */
public static function userInfo() {
    return self::$userInfo;
}

/**
 *
 * @access private
 */
public function __getWWWAuthenticateString() {
    return 'Basic realm="' . self::REALM . '"';
}

}

Any ideas?


Solution

  • I have removed API versioning and the errors went away.

    index.php:

    #$r->setAPIVersion(1);
    

    I'll re-visit this when v2 needs to be tested ;)