36

我对 laravel 范围部分有点困惑。

我有一个用户模型和表格。

如何为用户分配用户、客户和/或管理员的角色。

我有一个带有 vue 和 laravel api 后端的 SPA。我使用https://laravel.com/docs/5.3/passport#sumption-your-api-with-javascript

    Passport::tokensCan([
        'user' => 'User',
        'customer' => 'Customer',
        'admin' => 'Admin',
    ]);

我如何分配哪个用户模型具有哪个范围?

还是范围与角色不同?

你将如何实现这一点?

提前致谢!

4

6 回答 6

78

还是范围与角色不同?

两者之间最大的区别在于它们适用的上下文。基于角色的访问控制 (RBAC) 管理用户在直接使用 Web 应用程序时的访问控制,而 Oauth-2 范围代表用户管理外部客户端对API 资源的访问。

我如何分配哪个用户模型具有哪个范围?

在一般的 Oauth 流程中,用户(作为资源所有者)被要求授权客户代表他/她可以和不能做的事情,这些就是你所说的范围成功授权后,客户端请求的范围将分配给生成的令牌,而不是用户本身。

根据您选择的 Oauth 授权流程,客户端应在其请求中包含范围。在授权代码授权流程中,当将用户重定向到授权页面时,范围应包含在 HTTP GET 查询参数中,而在密码授权流程中,范围必须包含在 HTTP POST 正文参数中以请求令牌。

你将如何实现这一点?

这是密码授予流程的示例,假设您事先完成了laravel/passport设置

定义管理员和用户角色的范围。尽可能具体,例如:管理员可以管理订单,用户只能阅读。

// in AuthServiceProvider boot
Passport::tokensCan([
    'manage-order' => 'Manage order scope'
    'read-only-order' => 'Read only order scope'
]);

准备 REST 控制器

// in controller
namespace App\Http\Controllers;

class OrderController extends Controller
{   
    public function index(Request $request)
    {
        // allow listing all order only for token with manage order scope
    }

    public function store(Request $request)
    {
        // allow storing a newly created order in storage for token with manage order scope
    }

    public function show($id)
    {
        // allow displaying the order for token with both manage and read only scope
    }
}

使用 api guard 和 scope 分配路由

// in api.php
Route::get('/api/orders', 'OrderController@index')
    ->middleware(['auth:api', 'scopes:manage-order']);
Route::post('/api/orders', 'OrderController@store')
    ->middleware(['auth:api', 'scopes:manage-order']);
Route::get('/api/orders/{id}', 'OrderController@show')
    ->middleware(['auth:api', 'scopes:manage-order, read-only-order']);

并且在发布令牌时首先检查用户角色并根据该角色授予范围。为了实现这一点,我们需要一个额外的控制器,它使用 AuthenticatesUsers 特征来提供登录端点。

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

class ApiLoginController extends Controller
{
    use AuthenticatesUsers;

    protected function authenticated(Request $request, $user)
    {               
        // implement your user role retrieval logic, for example retrieve from `roles` database table
        $role = $user->checkRole();

        // grant scopes based on the role that we get previously
        if ($role == 'admin') {
            $request->request->add([
                'scope' => 'manage-order' // grant manage order scope for user with admin role
            ]);
        } else {
            $request->request->add([
                'scope' => 'read-only-order' // read-only order scope for other user role
            ]);
        }

        // forward the request to the oauth token request endpoint
        $tokenRequest = Request::create(
            '/oauth/token',
            'post'
        );
        return Route::dispatch($tokenRequest);
    }
}

为 api 登录端点添加路由

//in api.php
Route::group('namespace' => 'Auth', function () {
    Route::post('login', 'ApiLoginController@login');
});

不是对 /oauth/token 路由进行 POST,而是对我们之前提供的 api 登录端点进行 POST

// from client application
$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/api/login', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'user@email.com',
        'password' => 'my-password',
    ],
]);

return json_decode((string) $response->getBody(), true);

成功授权后,将根据我们之前定义的范围为客户端应用程序发出 access_token 和 refresh_token。将其保存在某处,并在向 API 发出请求时将令牌包含到 HTTP 标头中。

// from client application
$response = $client->request('GET', '/api/my/index', [
    'headers' => [
        'Accept' => 'application/json',
        'Authorization' => 'Bearer '.$accessToken,
    ],
]);

API 现在应该返回

{"error":"unauthenticated"}

每当使用具有低权限的令牌来消耗受限端点时。

于 2016-11-04T23:17:43.440 回答
5

实施 Raymond Lagonda 响应,它工作得很好,只是要小心以下。您需要覆盖 ApiLoginController 中 AuthenticatesUsers 特征的一些方法:

    /**
     * Send the response after the user was authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    protected function sendLoginResponse(Request $request)
    {
        // $request->session()->regenerate(); // coment this becose api routes with passport failed here.

        $this->clearLoginAttempts($request);

        return $this->authenticated($request, $this->guard()->user())
                ?: response()->json(["status"=>"error", "message"=>"Some error for failes authenticated method"]);

    }

    /**
     * Get the failed login response instance.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function sendFailedLoginResponse(Request $request)
    {
        return response()->json([
                                "status"=>"error", 
                                "message"=>"Autentication Error", 
                                "data"=>[
                                    "errors"=>[
                                        $this->username() => Lang::get('auth.failed'),
                                    ]
                                ]
                            ]);
    }

如果您将登录名:用户名字段更改为自定义用户名字段,例如:e_mail。您必须像在 LoginController 中一样细化用户名方法。此外,您还必须重新定义和编辑方法:validateLogin、attemptLogin、credentials,因为一旦验证登录,请求就会转发到护照并且必须称为用户名。

于 2017-03-24T15:16:00.890 回答
4

我知道这有点晚了,但是如果您使用CreateFreshApiTokenweb 中间件在 SPA 中使用后端 API,那么您可以简单地向您的应用程序添加一个“管理员”中间件:

php artisan make:middleware Admin

然后\App\Http\Middleware\Admin执行以下操作:

public function handle($request, Closure $next)
{
    if (Auth::user()->role() !== 'admin') {
        return response(json_encode(['error' => 'Unauthorised']), 401)
            ->header('Content-Type', 'text/json');
    }

    return $next($request);
}

确保您已添加检索用户角色的role方法。\App\User

现在您需要做的就是在 中注册您的中间件app\Http\Kernel.php $routeMiddleware,如下所示:

protected $routeMiddleware = [
    // Other Middleware
    'admin' => \App\Http\Middleware\Admin::class,
];

并将其添加到您的路线中routes/api.php

Route::middleware(['auth:api','admin'])->get('/customers','Api\CustomersController@index');

现在,如果您尝试在未经许可的情况下访问 api,您将收到“401 Unauthorized”错误,您可以在您的应用程序中检查并处理该错误。

于 2017-08-08T15:16:40.157 回答
3

使用@RaymondLagonda 解决方案。如果您收到未找到类范围错误,请将以下中间件添加到文件的$routeMiddleware属性中app/Http/Kernel.php

'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class, 
'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,`

此外,如果您收到错误消息Type error: Too few arguments to function,您应该能够$user从如下请求中获取。

(我使用 laratrust 管理角色)

public function login(Request $request)
{

    $email = $request->input('username');
    $user = User::where('email','=',$email)->first();

    if($user && $user->hasRole('admin')){
        $request->request->add([
            'scope' => 'manage-everything'
        ]);
    }else{
        return response()->json(['message' => 'Unauthorized'],403);
    }

    $tokenRequest = Request::create(
      '/oauth/token',
      'post'
    );

    return Route::dispatch($tokenRequest);

}
于 2018-05-20T15:09:46.390 回答
3

对于带有Sentinel的Laravel 5.5,我已经设法通过 @RaymondLagonda 解决方案使其工作,但它应该也可以在没有 Sentinel 的情况下工作。

该解决方案需要覆盖一些类方法(因此请记住这一点,以备将来更新),并为您的 api 路由添加一些保护(例如,不公开 client_secret)。

第一步,是修改你ApiLoginController的以添加构造函数:

public function __construct(Request $request){
        $oauth_client_id = env('PASSPORT_CLIENT_ID');
        $oauth_client = OauthClients::findOrFail($oauth_client_id);

        $request->request->add([
            'email' => $request->username,
            'client_id' => $oauth_client_id,
            'client_secret' => $oauth_client->secret]);
    }

在此示例中,您需要在 .env 中定义 var ('PASSPORT_CLIENT_ID') 并创建 OauthClients 模型,但您可以通过在此处放置正确的测试值来安全地跳过此步骤。

需要注意的一件事是,我们正在$request->email为用户名设置值,只是为了遵守 Oauth2 约定。

第二步是,覆盖sendLoginResponse导致错误的方法Session storage not set,我们在这里不需要会话,因为它是 api。

protected function sendLoginResponse(Request $request)
    {
//        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->redirectPath());
    }

第三步是按照@RaymondLagonda 的建议修改您的身份验证方法。您需要在这里编写自己的逻辑,尤其是配置您的范围。

最后一步(如果您使用 Sentinel)是修改AuthServiceProvider. 添加

$this->app->rebinding('request', function ($app, $request) {
            $request->setUserResolver(function () use ($app) {
                 return \Auth::user();
//                return $app['sentinel']->getUser();
            });
        });

$this->registerPolicies();在启动方法之后。

在这些步骤之后,您应该能够通过提供用户名(“这将始终是电子邮件,在此实现中”)、密码和 grant_type='password' 来让您的 api 工作

此时,您可以添加到中间件范围scopes:...scope:...保护您的路由。

我希望,它真的会有所帮助......

于 2018-01-04T03:57:45.830 回答
0

谢谢你,这个问题让我困惑了一段时间!我采用 Raymond Lagonda 的解决方案为 Laravel 5.6 进行了一些定制,使用内置的速率限制,使用单个thirdparty客户端(或者如果需要,可以进行更多自定义),同时仍然为每个用​​户提供权限列表(范围)。

  • 使用 Laravel Passportpassword授权并遵循 Oauth 流程
  • 使您能够为不同的用户设置角色(范围)
  • 不要公开/发布客户端 ID 或客户端密码,只有用户的用户名(电子邮件)和密码,几乎是密码授权,减去客户端/授权的东西

底部示例

路线/api.php

    Route::group(['namespace' => 'ThirdParty', 'prefix' => 'thirdparty'], function () {
        Route::post('login', 'ApiLoginController@login');
    });

第三方/ApiLoginController.php

<?php

namespace App\Http\Controllers\ThirdParty;

use Hash;
use App\User;
use App\ThirdParty;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class ApiLoginController extends Controller
{
    use AuthenticatesUsers;

    /**
     * Thirdparty login method to handle different
     * clients logging in for different reasons,
     * we assign each third party user scopes
     * to assign to their token, so they
     * can perform different API tasks
     * with the same token.
     *
     * @param  Request $request
     * @return Illuminate\Http\Response
     */
    protected function login(Request $request)
    {
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        $user = $this->validateUserLogin($request);

        $client = ThirdParty::where(['id' => config('thirdparties.client_id')])->first();

        $request->request->add([
            'scope' => $user->scopes,
            'grant_type' => 'password',
            'client_id' => $client->id,
            'client_secret' => $client->secret
        ]);

        return Route::dispatch(
            Request::create('/oauth/token', 'post')
        );
    }

    /**
     * Validate the users login, checking
     * their username/password
     *
     * @param  Request $request
     * @return User
     */
    public function validateUserLogin($request)
    {
        $this->incrementLoginAttempts($request);

        $username = $request->username;
        $password = $request->password;

        $user = User::where(['email' => $username])->first();

        abort_unless($user, 401, 'Incorrect email/password.');

        $user->setVisible(['password']);

        abort_unless(Hash::check($password, $user->password), 401, 'Incorrect email/password.');

        return $user;
    }
}

配置/第三方.php

<?php

return [
    'client_id' => env('THIRDPARTY_CLIENT_ID', null),
];

第三方.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ThirdParty extends Model
{
    protected $table = 'oauth_clients';
}

.env

## THIRDPARTIES
THIRDPARTY_CLIENT_ID=3

php artisan make:migration add_scope_to_users_table --table=users

        // up
        Schema::table('users', function (Blueprint $table) {
            $table->text('scopes')->nullable()->after('api_access');
        });
        // down
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('scopes');
        });

(注意:api_access是一个标志,它决定用户是否可以登录到应用程序的网站/前端部分,查看仪表板/记录等),

路线/api.php

Route::group(['middleware' => ['auth.client:YOUR_SCOPE_HERE', 'throttle:60,1']], function () {
    ...routes...
});

MySQL - 用户范围

INSERT INTO `users` (`id`, `created_at`, `updated_at`, `name`, `email`, `password`, `remember_token`, `api_access`, `scopes`)
VALUES
    (5, '2019-03-19 19:27:08', '2019-03-19 19:27:08', '', 'hello@email.tld', 'YOUR_HASHED_PASSWORD', NULL, 1, 'YOUR_SCOPE_HERE ANOTHER_SCOPE_HERE');

MySQL - ThirdPartyOauth 客户端

INSERT INTO `oauth_clients` (`id`, `user_id`, `name`, `secret`, `redirect`, `personal_access_client`, `password_client`, `revoked`, `created_at`, `updated_at`)
VALUES
    (3, NULL, 'Thirdparty Password Grant Client', 'YOUR_SECRET', 'http://localhost', 0, 1, 0, '2019-03-19 19:12:37', '2019-03-19 19:12:37');

cURL - 登录/请求令牌

curl -X POST \
  http://site.localhost/api/v1/thirdparty/login \
  -H 'Accept: application/json' \
  -H 'Accept-Charset: application/json' \
  -F username=hello@email.tld \
  -F password=YOUR_UNHASHED_PASSWORD
{
    "token_type": "Bearer",
    "expires_in": 604800,
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciO...",
    "refresh_token": "def502008a75cd2cdd0dad086..."
}

像往常一样使用长寿命的 access_token/refresh_token!

访问禁止范围

{
    "data": {
        "errors": "Invalid scope(s) provided."
    },
    "meta": {
        "code": 403,
        "status": "FORBIDDEN"
    }
}
于 2019-03-21T16:52:06.140 回答