定制 fuzz/laravel-oauth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fuzz/laravel-oauth

Composer 安装命令:

composer require fuzz/laravel-oauth

包简介

auth

README 文档

README

An OAuth wrapper to bridge lucadegasperi/oauth2-server-laravel and Laravel's authentication system while providing optional support for fuzz/magic-box repositories

Setup

  1. Require the composer package

  2. Set up your project AuthServiceProvider to extend Fuzz\Auth\Providers\AuthServiceProvider

  3. Follow instructions in lucadegasperi/oauth2-server-laravel to set it up.

  4. Configure the grant_types array in config/oauth2.php to use the Fuzz grants (or extend/create your own)

    '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,
    	],
    ],
    
  5. Set up config/auth.php

    Set the default guard to api

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],
    

    Set the api guard to use \Fuzz\Auth\Guards\OAuthGuard::class as its driver

    'api' => [
        'driver' => \Fuzz\Auth\Guards\OAuthGuard::class,
        'provider' => 'users',
    ],
    

    Set Laravel to use the oauth user provider and set your project's User class

    'providers' => [
        'users' => [
            'driver' => 'oauth',
            'model' => \App\User::class,
            'token_key' => 'access_token',
        ],
    ],
    
  6. Create app/Http/Middleware/OAuthMiddleware.php and extend Fuzz\Auth\Middleware\OAuthenticateMiddleware. Add it to the $routeMiddleware array in `app/Http/Kernel.php

  7. Your User class should implement the Fuzz\Auth\Models\AgentInterface and Illuminate\Contracts\Auth\Authenticatable and their required methods

Usage

Protecting routes

Routes that require authentication can now be protected with the auth middleware:

$router->group(
    ['middleware' => 'auth'], function (Router $router) {
        $router->get('locations', 'LocationsController@index');
});

Within any authenticated route, you can use all the default Laravel Auth methods such as Auth::user() to resolve the currently authenticated user. lucadegasperi/oauth2-server-laravel provides a way to protect routes based on scope, but you can also use Fuzz\Auth\Policies\RepositoryModelPolicy@requireScopes to throw League\OAuth2\Server\Exception\AccessDeniedException exceptions when a user does not have the required scopes.

Protecting resources

Laravel OAuth comes with a base Fuzz\Auth\Policies\RepositoryModelPolicy but you may create your own (implementing the Fuzz\Auth\Policies\RepositoryModelPolicyInterface might be helpful). Extending Fuzz\Auth\Policies\RepositoryModelPolicy will provide some base methods to ease writing policies for repositories.

Once a policy is set up and mapped to its model class, you may use it to check user permissions according to your policy:

 if (policy(ModelClass::class)->index($user, $postRepository)) {
 		// Index stuff
 }

Resolving the current user

All of Laravel's Auth methods will work, so resolving the current user is as simple as $user = Auth::user(). https://laravel.com/docs/5.2/authentication.

Auth will use your default guard unless specified. A typical guard set up for an OAuth specced API would be having one for users accessing via a client and another for client-only requests. Currently there is only Fuzz\Auth\Guards\OAuthGuard which is responsible for resolving the user for a request.

TODOs

  1. Separate fuzz/laravel-oauth from fuzz/magic-box
  2. Support client requests in their own guard and be compatible with the current user OAuthGuard

fuzz/laravel-oauth 适用场景与选型建议

fuzz/laravel-oauth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.12k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 fuzz/laravel-oauth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fuzz/laravel-oauth 我们能提供哪些服务?
定制开发 / 二次开发

基于 fuzz/laravel-oauth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 27
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-07