fuzz / laravel-oauth
身份验证
Requires
- fuzz/magic-box: 1.1.*
- laravel/framework: 5.2.*
- lucadegasperi/oauth2-server-laravel: 5.1.*
- symfony/security-core: 3.0.*
Requires (Dev)
- doctrine/dbal: ~2.3
- fuzz/rest-tester: 1.0.*
- fzaninotto/faker: ~1.4
- mockery/mockery: 0.9.*
- orchestra/testbench: 3.2.*
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-17 11:22:49 UTC
README
这是一个 OAuth 包装器,用于连接 lucadegasperi/oauth2-server-laravel
和 Laravel 的身份验证系统,同时提供对 fuzz/magic-box
仓库的可选支持。
设置
-
需要 composer 包
-
将你的项目
AuthServiceProvider
设置为扩展Fuzz\Auth\Providers\AuthServiceProvider
-
按照
lucadegasperi/oauth2-server-laravel
中的说明进行设置。 -
在
config/oauth2.php
中配置grant_types
数组以使用 Fuzz 授予(或扩展/创建自己的授予)'grant_types' => [ 'password' => [ 'class' => \Fuzz\Auth\OAuth\Grants\PasswordGrant::class, 'callback' => '\Fuzz\Auth\OAuth\Grants\PasswordGrantVerifier@verify', 'access_token_ttl' => 7600, ], 'refresh_token' => [ 'class' => \Fuzz\Auth\OAuth\Grants\RefreshTokenGrant::class, 'access_token_ttl' => 7600, 'refresh_token_ttl' => 14600, ], ],
-
设置
config/auth.php
设置默认保护为
api
'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ],
设置
api
保护以使用\Fuzz\Auth\Guards\OAuthGuard::class
作为其驱动程序'api' => [ 'driver' => \Fuzz\Auth\Guards\OAuthGuard::class, 'provider' => 'users', ],
设置 Laravel 使用
oauth
用户提供者,并设置你的项目用户类'providers' => [ 'users' => [ 'driver' => 'oauth', 'model' => \App\User::class, 'token_key' => 'access_token', ], ],
-
创建
app/Http/Middleware/OAuthMiddleware.php
并扩展Fuzz\Auth\Middleware\OAuthAuthenticateMiddleware
。将其添加到 `app/Http/Kernel.php` 中的$routeMiddleware
数组 -
你的用户类应实现
Fuzz\Auth\Models\AgentInterface
和Illuminate\Contracts\Auth\Authenticatable
及其所需的方法
用法
保护路由
需要身份验证的路由现在可以使用 auth
中间件进行保护
$router->group(
['middleware' => 'auth'], function (Router $router) {
$router->get('locations', 'LocationsController@index');
});
在任何已验证的路由中,你可以使用所有默认的 Laravel Auth
方法,例如 Auth::user()
来解析当前已验证的用户。lucadegasperi/oauth2-server-laravel
提供了一种基于作用域保护路由的方法,但你也可以使用 Fuzz\Auth\Policies\RepositoryModelPolicy@requireScopes
来抛出 League\OAuth2\Server\Exception\AccessDeniedException
异常,当用户没有所需的权限时。
保护资源
Laravel OAuth 伴随一个基本的 Fuzz\Auth\Policies\RepositoryModelPolicy
,但你可以创建自己的(实现 Fuzz\Auth\Policies\RepositoryModelPolicyInterface
可能会有所帮助)。通过扩展 Fuzz\Auth\Policies\RepositoryModelPolicy
将提供一些基本方法来简化为仓库编写策略。
一旦设置好策略并将其映射到其模型类,你可以根据你的策略检查用户的权限
if (policy(ModelClass::class)->index($user, $postRepository)) {
// Index stuff
}
解析当前用户
Laravel 的所有 Auth
方法都将工作,因此解析当前用户就像 $user = Auth::user()
一样简单。https://laravel.net.cn/docs/5.2/authentication
。
Auth
将使用你的默认保护,除非指定。为 OAuth 规范的 API 设置的典型保护将有一个用于客户端访问的用户保护,另一个用于仅客户端请求。目前只有 Fuzz\Auth\Guards\OAuthGuard
负责解析请求的用户。
待办事项
- 将
fuzz/laravel-oauth
从fuzz/magic-box
中分离出来 - 支持客户端请求在自己的保护中,并与当前用户
OAuthGuard
兼容