Search code examples
phprestler

AuthenticationClass needs duplicating with UrlVersioning - Luracast Restler


I would like to have an authenitcation class and also have my APIS versioned without having to duplicate my security code.

I have setup restler and added the following to index.php;

Defaults::setProperty('useUrlBasedVersioning', true);
$r->addAuthenticationClass('MyOrg\\Security\\APIAuth');

I have then setup my authentication class within another folder outside of the public folder. It wasn't working on its own but I found due to using the UrlBased Versioning I had to repeat the class in the different namespaces.

e.g.

MyOrd ---> Security ---> v1 ---> APIAuth.php

MyOrd ---> Security ---> v2 ---> APIAuth.php

I don't want to have to do the above but more simple just have

MyOrd ---> Security ---> APIAuth.php

I'm using Restler RC5, any guidance would be appreciated or is this a bug with Restler.

Also logged as issue with the restler project https://github.com/Luracast/Restler/issues/433


Solution

  • Just implement the iProvideMultiVersionApi and return the maximum version that is supported by the auth class, which in your case will be 2. See the example below

    namespace MyOrg\Security;
    
    use Luracast\Restler\iAuthenticate;
    use Luracast\Restler\iProvideMultiVersionApi;
    
    class Auth implements iAuthenticate, iProvideMultiVersionApi{
    
        public function __isAllowed(){
            return isset($_GET['api_key']) && $_GET['api_key'] =='allow';
        }
    
        public function __getWWWAuthenticateString(){
            return 'Query';
        }
    
        /**
         * Maximum api version supported by the api class
         * @return int
         */
        public static function __getMaximumSupportedVersion()
        {
            return 2;
        }
    }