0

我已经实现了这个 oauth 服务器http://bshaffer.github.io/oauth2-server-php-docs/

它有一个 Laravel 实现:http ://bshaffer.github.io/oauth2-server-php-docs/cookbook/laravel/

本指南为您提供指导并提供路线代码:

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

它就像那样工作得很好。现在我想将“私有”路由中的检查功能转换为可以应用于每个必要路由的中间件。我使用创建了中间件

php artisan make:middleware AuthChecker

将其添加到kernel.php中,并将验证函数的代码粘贴在其中。我立即得到一个错误:

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

所以,我想我将不得不“使用”一些东西,但由于我还是一个初学者,我真的不知道该怎么做......

提前感谢您的帮助!

[编辑] 中间件的内容目前看起来像这样:

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

}

再次感谢

4

1 回答 1

1

AuthChecker.php 第 17 行中的 FatalErrorException:找不到类“Oauth2\HttpFoundationBridge\Request”

所以你想使用命名空间中的RequestOauth2\HttpFoundationBridge到你的OauthCheckApp\Http\Middleware

您可以通过以下任何一种方式进行操作:

  1. 导入类

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

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

    注意前面的反斜杠Oauth2\HttpFoundationBridge\Request。如果你只是说$bridgedRequest = Oauth2\HttpFoundationBridge\Request,那么 PHP 会寻找App\Http\Middleware\Oauth2\HttpFoundationBridge\Request

于 2015-05-29T11:38:48.870 回答