binarygeotech/passport-booster

为Laravel Passport添加多认证功能

dev-master 2020-04-02 00:11 UTC

This package is auto-updated.

Last update: 2024-09-29 06:02:41 UTC


README

本包通过启用您使用多个用户模型(认证守卫)的功能来扩展Laravel Passport的功能。包含一个启用多认证的中间件,并自动注册为passport.guard

## 安装 composer require binarygeotech/passport-booster

设置

  • passport.guard添加到Laravel Passport的AuthServiceProvider.php文件中的boot方法中配置的路由设置。
use Laravel\Passport\Passport; // Import Laravel Passport
use BGS\PassportBooster\PassportBooster; // Import Passport Booster

public function boot()
{
	...
	Passport::routes(
		null,
		[
			'middleware' => [
				'passport.guard'
			]
		]
	);
	
	PassportBooster::enableMultiGuard(true); // Add this line to enable Multiple Authentication Guard feature.
	
	...
}

用法

  • config/auth.php中设置您的守卫
'guards' => [
    ...
    'admin' => [
        'driver' => 'passport',
        'provider' => 'admins',
        'hash' => false,
    ],
	'clients' => [
        'driver' => 'passport',
        'provider' => 'clients',
        'hash' => false,
    ],
],

'providers' => [
    ...
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
	'clients' => [
        'driver' => 'eloquent',
        'model' => App\Clients::class,
    ],
],

  • 将您的API路由包裹在一个带有passport.guard:{guard}中间件的组中

以下示例

	// Admin Route
	Route::group(
		[
			'prefix' => 'administrator',
			'middleware' => [
				'passport.guard:admin'
			]
		],
		function () {
			/*
				Using the proxy token generation method as documented here https://laravel.net.cn/docs/7.x/passport#requesting-password-grant-tokens,
				use the route below.
			*/
			Route::post('/access/login', 'Auth\AdminLoginController@login')
				->name('api.admin.login');

			// Define other routes by applying the guard middleware
			Route::group(
				[
					'middleware' => [
						'auth:admin',
					],
				],
				function () {
					Route::get('profile', 'Auth\AdminLoginController@profile')->name('api.admin.profile');
					
				}
			
			)
		}
	);
	
	// Client Route
	Route::group(
		[
			'prefix' => 'client',
			'middleware' => [
				'passport.guard:client'
			]
		],
		function () {
			/*
				Using the proxy token generation method as documented here https://laravel.net.cn/docs/7.x/passport#requesting-password-grant-tokens,
				use the route below.
			*/
			Route::post('/access/login', 'Auth\ClientLoginController@login')
				->name('api.client.login');

			// Define other routes by applying the guard middleware
			Route::group(
				[
					'middleware' => [
						'auth:client',
					],
				],
				function () {
					Route::get('profile', 'Auth\ClientLoginController@profile')->name('api.client.profile');
					
				}
			
			)
		}
	);
  • guard参数添加到所有您的令牌请求(oauth/token或使用guzzle)中

Guzzle示例(https://laravel.net.cn/docs/7.x/passport#requesting-password-grant-tokens

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'taylor@laravel.com',
        'password' => 'my-password',
        'scope' => '',
		'guard' => 'admin'
    ],
]);

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

额外自定义

  • 扩展类文件时,发布配置文件php artisan vendor:publish --tag=passport-booster-config
<?php

return [
    'client_repository' => \Laravel\Passport\ClientRepository::class,
    'client_repository_bridge' => \Laravel\Passport\Bridge\ClientRepository::class,
    'access_token_repository' => \BGS\PassportBooster\Bridge\AccessTokenRepository::class,
    'bearer_token_validator' => \BGS\PassportBooster\Validators\BearerTokenValidator::class,
    'token_guard' => \BGS\PassportBooster\Guards\TokenGuard::class,
    'token_repository' => \Laravel\Passport\TokenRepository::class,
];

配置文件中的所有文件都可以用类的正确实现或父类替换

致谢

感谢Mohamed Hamed为其包https://github.com/hamedov93/passport-multiauth做出的贡献,

注意

对于自定义授权,请使用Mohamed Hamed的passport-multiauth composer require hamedov/passport-multiauth

问题

请前往问题部分提交问题。

贡献

您可以为此包做出贡献,如果您发现了一个缺失的功能,请通知我并/或发起一个pull request。

许可

在MIT许可下发布,请参阅LICENSE