0

I have implemented this oauth server http://bshaffer.github.io/oauth2-server-php-docs/

It has a Laravel implementation : http://bshaffer.github.io/oauth2-server-php-docs/cookbook/laravel/

This guide you and gives that code for routes :

App::singleton('oauth2', function() {
    $storage = new OAuth2\Storage\Pdo(array('dsn' => 'mysql:dbname=oauth2;host=localhost', 'username' => 'root', 'password' => 'root'));
    $server = new OAuth2\Server($storage);

    $server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
    $server->addGrantType(new OAuth2\GrantType\UserCredentials($storage));

    return $server;
});

Route::get('private', function()
{
    $bridgedRequest  = OAuth2\HttpFoundationBridge\Request::createFromRequest(Request::instance());
    $bridgedResponse = new OAuth2\HttpFoundationBridge\Response();

        // fix for laravel
        $bridgedRequest->request = new \Symfony\Component\HttpFoundation\ParameterBag();
        $rawHeaders = getallheaders();
        if (isset($rawHeaders["Authorization"])) {
            $authorizationHeader = $rawHeaders["Authorization"];
            $bridgedRequest->headers->add([ 'Authorization' => $authorizationHeader]);
        }

    if (App::make('oauth2')->verifyResourceRequest($bridgedRequest, $bridgedResponse)) {

        $token = App::make('oauth2')->getAccessTokenData($bridgedRequest);

        return Response::json(array(
            'private' => 'stuff',
            'user_id' => $token['user_id'],
            'client'  => $token['client_id'],
            'expires' => $token['expires'],
        ));
    }
    else {
        return Response::json(array(
            'error' => 'Unauthorized'
        ), $bridgedResponse->getStatusCode());
    }
});

It works perfectly well like that. Now I want to transform that check function in the "private" route to a middleware I could apply to each necessary route. I created the middleware using

php artisan make:middleware AuthChecker

Added it to the kernel.php, and pasted the code of the verification function inside of it. And I immediately got an error :

FatalErrorException in AuthChecker.php line 17:
Class 'Oauth2\HttpFoundationBridge\Request' not found

So, I guess I will have to "use" things, but since I'm still a beginner I don't really know what to do...

Thanks ahead for your help !

[EDIT] the content of the middleware currently look like this :

namespace App\Http\Middleware;

use Closure;

class OauthCheck {
    public function handle($request, Closure $next)
    {
        $bridgedRequest  = OAuth2\HttpFoundationBridge\Request::createFromRequest($request);
        $bridgedResponse = new OAuth2\HttpFoundationBridge\Response();

        // fix for laravel
        $bridgedRequest->request = new \Symfony\Component\HttpFoundation\ParameterBag();
        $rawHeaders = getallheaders();
        if (isset($rawHeaders["Authorization"])) {
            $authorizationHeader = $rawHeaders["Authorization"];
            $bridgedRequest->headers->add([ 'Authorization' => $authorizationHeader]);
        }

        if (App::make('oauth2')->verifyResourceRequest($bridgedRequest, $bridgedResponse)) {

            $token = App::make('oauth2')->getAccessTokenData($bridgedRequest);

            return Response::json(array(
                'private' => 'stuff',
                'user_id' => $token['user_id'],
                'client'  => $token['client_id'],
                'expires' => $token['expires'],
            ));

            return $next($request);
        }
        else {
            return Response::json(array(
                'error' => 'Unauthorized'
            ), $bridgedResponse->getStatusCode());
        }
    }

}

Thanks again

Jeremy Belolo
  • 4,319
  • 6
  • 44
  • 88
  • 1
    Please post the contents of the `AuthChecker.php` middleware. – Bogdan May 28 '15 at 23:52
  • 1
    Maybe you should change it to `\Oauth2\HttpFoundationBridge\Request` – Jake Opena May 29 '15 at 02:18
  • @Bogdan I just added it, it's basically the same as the function that checked "private" earlier. – Jeremy Belolo May 29 '15 at 07:31
  • @JakeOpena sorry, I don't understand what you mean, but I doubt I need to change the code of this since it works perfectly when used in the closure function directly. I'm sure it's all about paths and dependencies – Jeremy Belolo May 29 '15 at 07:31
  • 1
    What I mean is add a backslash before `OAuth2\HttpFoundationBridge\Request` since it is not under your current namespace. It works in your closure function because you are in the global namespace. – Jake Opena May 29 '15 at 07:51
  • @JakeOpena actually it worked ! Setting a \ before every Response, App and Oauth2 worked perfectly. Is that the way to do it in Laravel ? Am I not said to add "use xxxxx" at the beginning of the file ? – Jeremy Belolo May 29 '15 at 08:12
  • 1
    That's how it works on PHP since [5.3.0](http://php.net/releases/5_3_0.php). For me, importing classes using `use` is better coz you don't need to add the backslash and you can reuse it anywhere in that php file. – Jake Opena May 29 '15 at 08:32
  • @JakeOpena so how would I import, for exemple, the Response, or the Oauth2 ? What use to write ? And more importantly, how to know what Use to write ? – Jeremy Belolo May 29 '15 at 10:20
  • 1
    Hey, I just added an answer to be clear. Please accept and upvote :) – Jake Opena May 29 '15 at 11:40

1 Answers1

1

FatalErrorException in AuthChecker.php line 17: Class 'Oauth2\HttpFoundationBridge\Request' not found

So you want to use the Request class from Oauth2\HttpFoundationBridge namespace to your OauthCheck class from App\Http\Middleware.

You can do it in either ways:

  1. Import the class

     namespace App\Http\Middleware;
    
     use Oauth2\HttpFoundationBridge\Request;
    
     class OauthCheck {
    
         public function handle($request, Closure $next)
         {
             $bridgedRequest = Request::createFromRequest($request);
             ....
         }
     }
    
  2. Use the class explicitly

    namespace App\Http\Middleware;
    
    class OauthCheck {
    
         public function handle($request, Closure $next)
         {
             $bridgedRequest = \Oauth2\HttpFoundationBridge\Request::createFromRequest($request);
             ....
         }
     }
    

    Take note of the backslash before Oauth2\HttpFoundationBridge\Request. If you just say $bridgedRequest = Oauth2\HttpFoundationBridge\Request, then PHP will look for App\Http\Middleware\Oauth2\HttpFoundationBridge\Request.

Jake Opena
  • 1,475
  • 1
  • 11
  • 18
  • Hey ! I will accept the answer, of course, but before I tested it, and the use solution doesn't work. Adding "use Oauth2\HttpFoundationBridge\Request;" still gives "FatalErrorException in AuthChecker.php line 17: Class 'Oauth2\HttpFoundationBridge\Request' not found" – Jeremy Belolo May 29 '15 at 11:47
  • 1
    Take a look at solution 1. Change `Oauth2\HttpFoundationBridge\Request` to `Request` only since you already imported the class. – Jake Opena May 29 '15 at 11:56
  • Yes, I did change to Request only, but the error message is still the same. It uses properly the imported class, only that he doesn't find it, I guess. I tried to add a \ also - "use \Oauth2\HttpFoundationBridge\Request;" didn't work. Yes solution 2 with \ works, but I really wanted to understand how these "use" works also. – Jeremy Belolo May 29 '15 at 12:03